alignment and groups in LaTeX: how to explain the behavior?
the initial markup is incorrect (but not flagged as an error by latex)
\raggedright
and \raggedleft
should be used before the paragraph, it sets ragged \rightskip
(which would take effect even if the declaration is used mid paragraph) but it also sets \parindent
to zero and defines \\
to give a ragged-compatible line break.
If as here you use \raggedleft
late then you get the settings of \rightskip
, \leftskip
and \parfillskip
in effect at the end of the paragraph, but you get a paragraph indent from whatever was in effect before the paragraph and similarly \\
will have expanded to be the "normal" definition.
So in your example the definition of \\
for the first paragraph is completely different to the definition for the second paragraph after \raggedright
,
You get a centered line because a "normal" \\
does essentially \hfil\linebreak
to make a short line, but if you use that definition in the scope of raggedleft you also have \hfil
on the left to give the raggedmargin, so you end up with a centred line.
So the example may be easier to see with \\
expanded out (and simplified a bit)
\documentclass{article}
\begin{document}
{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\unskip\nobreak\hfil\break % centered
exercitation ullamco laboris\unskip\nobreak\hfil\break % centered
nisi ut aliquip ex ea commodo consequat.\raggedleft
Lorem ipsum dolor sit amet,\par\noindent % ?! raggedleft
consectetur adipiscing elit,\par\noindent % ?! raggedleft
sed do eiusmod tempor incididunt. % justified
}
\end{document}
Under normal circumstances, \\
means “break a line here”, issuing \hfil\break
(it does other things when the *
or an optional argument is added, but these are irrelevant for the explanation).
However, \raggedright
and \raggedleft
change its definition to \@centercr
which, in your cases, does \unskip\par
. So, let's translate your code:
{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\hfil\break
exercitation ullamco laboris\hfil\break
nisi ut aliquip ex ea commodo consequat.\raggedleft
Lorem ipsum dolor sit amet,\unskip\par
consectetur adipiscing elit,\unskip\par
sed do eiusmod tempor incididunt.
}
The first paragraph is typeset normally and there's no problem with that.
The second paragraph ends when \raggedleft
has already been executed. This command, among other things, sets \leftskip
to \hfil
, \rightskip
to zero and also \parfillskip
.
At this point we should note that TeX uses the values of \leftskip
, \rightskip
and \parfillskip
current when the paragraph ends.
Hence, each line in the second paragraph, except for the last, has effectively \hfil
glue on either side, ending up centered. The last line only has \hfil
glue at its left, so it ends up flush right.
The first line also has the indentation box, so the centering is visually not perfect, but it is insofar as boxes in the line are taken into account.
The next three line are three separate paragraphs; the first two lines are flush right, as expected, because \raggedleft
is still in force.
The last line has the \par
following the closing brace, so the effect of \raggedleft
is nullified and the paragraph is typeset justified. Where is the indentation box? Nowhere, because the previous \raggedleft
has set \parindent
to zero and the indentation box is inserted as soon as the paragraph starts (in this case when TeX has scanned the letter s
).