Text alignment (center / ragged / justified)

Why do LaTeX paragraphs look calmer than word-processor paragraphs? The answer sits one layer below text alignment: a word processor fills one line at a time, whereas LaTeX weighs the whole paragraph before deciding where any single line ends. Justified is the default, and two routes lead away from it — declarations such as \centering and environments such as center, which produce exactly the same alignment but not the same vertical space. This page follows one thread: why justification is the default, how to choose between \centering and center, why \raggedright quietly stops hyphenating and what ragged2e does about it, and how to answer an Overfull \hbox warning.

Why justified is the default: the whole-paragraph line breaker

Unless you say otherwise, LaTeX sets paragraphs justified, flush to both margins. That default is not mere convention. TeX does not fill lines from the top down; it picks the combination of breakpoints that costs the whole paragraph the fewest demerits. The algorithm was published by Donald Knuth and Michael Plass as "Breaking Paragraphs into Lines" (Software: Practice and Experience, vol. 11, 1981, pp. 1119–1184). A greedy line-at-a-time filler banks its gains early and hands the bill to the last line; TeX can do the opposite and make an early line slightly worse on purpose so that nothing collapses further down. That optimisation only has room to move when the spaces between words may stretch and shrink — which is precisely what justification provides. Hence justified as the starting point.

The machinery is concrete. For each line TeX turns the amount of stretching or shrinking into a badness, and the penalty at the break point into p; with l for \linepenalty and b for the badness, the line's demerits are d = (l+b)^2 + p^2. A paragraph's demerits are the sum of those, plus surcharges for visually mismatched neighbouring lines (\adjdemerits) and for consecutive lines ending in a hyphen (\doublehyphendemerits). Knuth and Plass themselves called the formulas "quite arbitrary" — and they work anyway. The other thing worth knowing is that the line breaker makes up to three passes. The first attempts the paragraph with no hyphenation at all and accepts it only if every line stays within \pretolerance; only the second brings in the hyphenation patterns and the looser \tolerance. So a paragraph that fits comfortably is never hyphenated in the first place. Only if that also fails does a third pass run, using \emergencystretch.

\centering vs the center environment

The short answer: the alignment is exactly the same, and the only difference is the vertical space added above and below. Open latex.ltx and center is defined as \trivlist \centering\item\relax — the environment is nothing but \centering wrapped in an invisible list, and being a list it contributes \topsep of separation from the surrounding text. flushleft wraps \raggedright the same way, and flushright wraps \raggedleft. Measured in a 10pt article, the center environment adds roughly 8pt above and below; the declaration adds nothing at all.

DeclarationEnvironmentFlush edgeVertical space
\centeringcenterneither edge flush; lines sit in the middlenone from the declaration, \topsep from the environment
\raggedrightflushleftleft edge flush, right edge raggedsame as above
\raggedleftflushrightright edge flush, left edge raggedsame as above
\justifyingjustifyboth edges flush — justifiedneeds ragged2e; the declaration adds none
latex
\begin{figure}[htbp]
  \centering                 % declaration: adds no vertical space
  \includegraphics[width=0.6\textwidth]{plot}
  \caption{Measured values against the model}
  \label{fig:plot}
\end{figure}

This is why a figure should use \centering, not \begin{center}. The float already carries its own space above and below; stacking the center environment's \topsep on top of that leaves the image and its caption looking padded out. The same reasoning applies to a tabular cell, a \parbox, a minipage, and the body of a \caption — align those with the declaration. Conversely, when you drop a single display line into running text and want the surrounding space as part of the deal, the center environment is the shorter way to say it.

Declarations have one more property that trips people up: a declaration does not start a new paragraph. The alignment is settled at the moment the paragraph closes — when \par (a blank line) is read — and whatever is in force then applies to the entire paragraph. Which makes the following line behave in a way most people do not expect.

latex
ww {\centering xx \\ yy} zz
% result: the line "ww xx" comes out centred, "yy zz" does not

Typeset it and ww xx comes out centred while yy zz starts at the left margin. Two things are happening. First, \centering quietly redefines \\ to \@centercr, which is not a line break but a \par — the end of a paragraph. So xx closes its paragraph while \centering is still in force, and that paragraph is centred. yy then opens a new paragraph, but the closing } restores \rightskip and its relatives before that paragraph ends, so it is set justified. The one rule for declarations follows from this: the end of the paragraph you want aligned — a blank line, or an environment's \end — must fall inside the group.

Why \raggedright stops hyphenating, and what ragged2e does

The cause is one line of code. \raggedright sets \rightskip — the glue that fills out the end of a line — to 0pt plus 1fil, that is, to infinitely stretchable space. With something infinitely stretchable at the end of every line, every line's badness is zero. From TeX's point of view, splitting a word and adding a hyphen can then only raise the demerits and buy nothing. So it stops hyphenating altogether and the right edge comes out violently ragged. Set the same sentence to a 4cm measure and \raggedright inserts no hyphen at all, except where a single word will not fit on a line by itself, leaving the line lengths wildly uneven.

The ragged2e package fixes exactly that. It replaces the infinite stretch with a finite 0pt plus 2em, so a too-short line once again carries real badness and TeX starts weighing hyphenation again. It supplies the capitalised commands \RaggedRight, \RaggedLeft and \Centering, the environments FlushLeft, FlushRight and Center, plus \justifying and a justify environment for switching back to justified text. To set a whole document flush left while keeping hyphenation, load \usepackage[document]{ragged2e}: it runs \RaggedRight at \begin{document} and extends the same setting to \parboxes and to tabular p-columns. The package is by Martin Schröder and is now maintained by Marei Peischl.

latex
\usepackage[document]{ragged2e}   % whole document flush left, hyphenation kept

% or locally, only where it is needed:
\begin{FlushLeft}
  A paragraph set flush left with hyphenation still available,
  so the right edge stays gently ragged instead of jagged.
\end{FlushLeft}

\justifying                        % back to justified text

So when is ragged right actually the right call? The measure decides it — how long the line is. In multi-column layouts, in marginal notes, and in the narrow p-columns of a tabular, lines get short, and forcing justification on them stretches the interword spaces a long way. Those stretched spaces line up from row to row and open pale channels down the page — typographers call them rivers — which hurt reading rather than help it. The shorter the line, the greater the risk, so a narrow column really does read better flush left. A common compromise in a two-column paper is to leave the body justified and apply \RaggedRight only to captions and footnotes. In a single column with a comfortable measure, leave justification alone.

Fixing an Overfull \hbox warning: \sloppy and \emergencystretch

A log line such as Overfull \hbox (84.8932pt too wide) in paragraph at lines 10--10 is TeX reporting that it could not find any set of breakpoints that kept the paragraph within tolerance, so it gave up and let a line stick out into the margin. The usual culprit is something long and unbreakable: a URL, a compound identifier, a long word inside \texttt, or simply a narrow column. Suspect that one word before you suspect the alignment.

There is an order to the remedies. Fix the one word first — teach LaTeX where to split it with \hyphenation{...}, insert a discretionary \-, or hand a URL to \url from url or hyperref. That is the real fix. If the warning survives, reach for \sloppy, which relaxes \tolerance to 9999, \emergencystretch to 3em and \hfuzz to 0.5pt: it trades loose interword spacing for no overflow. Applied to a whole document it slackens the look of the body text, so it is gentler to wrap just the offending paragraph in the sloppypar environment, or to set \emergencystretch=3em on its own. \fussy restores the strict values (\tolerance 200, \emergencystretch 0). And if the warning keeps recurring in a narrow column, take it as a signal that the column wants ragged right rather than justification.

latex
% 1. tell LaTeX where the offending word may be split
\hyphenation{Bib-lio-the-ken-ver-bund}

% 2. relax the line breaker for one paragraph only
\begin{sloppypar}
  A paragraph containing a very long unbreakable token.
\end{sloppypar}

% 3. or globally, without going all the way to \sloppy
\emergencystretch=3em

Why \\ is the wrong way to end a paragraph

\\ only breaks a line; it does not end a paragraph. A blank line — \par — does. The distinction matters here because, as the previous sections showed, alignment is decided at the moment a paragraph closes. Separate two blocks of text with \\ and the second is not treated as a new paragraph: no indent, no \parskip, just a line break in the middle of one paragraph. Worse, put \\ at the end of a paragraph and follow it with a blank line, and the log fills with Underfull \hbox (badness 10000) in paragraph at lines 3--4, because an empty extra line has been created and then stretched across the full measure.

So where does \\ belong? Wherever you want a line break and nothing more. Splitting lines inside center or flushright, ending a row in tabular, separating verses in verse, folding a long \caption — those are line boundaries, not paragraph boundaries. Incidentally, a trailing \\ after the last line of a center block costs nothing: no extra space and no warning, because \@centercr cancels it with \addvspace{-\parskip}. Blank line to end a paragraph, \\ to end a line — holding that one boundary removes a large share of alignment bugs that otherwise look inexplicable.

Which alignment for which job

Start not from taste but from what the material is. Body paragraphs stay justified; short objects — a title, a caption, a signature line, a table cell — get aligned locally. And the choice between declaration and environment comes down to a single question: do you want the surrounding vertical space or not? Decided in that order, the question rarely comes up twice.

  • Inside figures, table cells, \parbox and minipage, align with a declaration such as \centering; an environment stacks its \topsep on space that is already there.
  • For a one-line display or motto in running text, the center environment is the natural choice, because the space around it is part of what you want.
  • For flush-left or flush-right Western text, use ragged2e's \RaggedRight and \RaggedLeft rather than the standard commands, so hyphenation stays alive.
  • In a narrow column (multi-column layouts, marginal notes, tabular p-columns) do not cling to justification — the shorter the line, the more ragged right wins.
  • When using a declaration, put the end of the paragraph inside the group. Without a blank line or an \end in there, the alignment either fails or spills into text you never meant to touch.
  • Blank line to end a paragraph, \\ to end a line. Confusing the two brings Underfull \hbox and vanished indentation at the same time.