Why do Alpha \hfil\ Omega\par and Alpha \hfil\ Omega not flush "Omega" to the right?
- 1,721
2 Answers
First case: Alpha \hfil\ Omega\par
An example:
\documentclass[a5paper]{article}
\usepackage[hmargin=23mm,vmargin=10mm]{geometry}
\usepackage{parskip}
\usepackage{color}
\usepackage{lipsum}
\newcommand*{\lip}{\lipsum*[101]\unskip}
\newcommand*{\om}{\textcolor{red}{Omega}}
\begin{document}
\verb|\parfillskip=|\texttt{\the\parfillskip}
1. Alpha \hfil \om\par
2. Alpha \hfill \om\par
3. Alpha \hfil \om\hspace{-\parfillskip}\par
4. Alpha \hfil \om\hspace{-\parfillskip}\null\par
5. Alpha \hfil \om\hspace*{-\parfillskip}\par
6. Alpha \hfil \om{\setlength{\parfillskip}{0pt}\par}
7. \lip\hfill \om\par
8. \lip~\hfill \om\par
9. \lip\nobreak\hfill~\om\par
\end{document}
The example has reduced the space between "Alpha" and "Omega" to one space and one
\hfil(l). If the minimum space should be larger, then I would rather use\hspace*{2em plus 1fill}, for example.Case 1: At the end of the paragraph TeX also adds a fill component
\parfillskip. Its0pt plus 1filgets the same space as the\hfilbefore.Case 2: The second
lof\hfilloverrules1filof\parfillskip.Case 3: The trick with an added negative
\parfillskipdoes not work, because TeX removes a horizontal glue at the end of the paragraph before inserting\parfillskip.Case 4:
\nullprevents the removal of the previous glue at the end of the paragraph.Case 5: Also the space of
\hspace*{...}is not removed at the end of a paragraph, because the star form sets two glue commands, the second with zero width that can be removed at the end of the paragraph without harm.Case 6:
\parfillskipis locally reset to zero for this paragraph.Case 7: At the start of a line
\hfilis removed.Case 8/9: The no-break-space
~or\nobreakprevents a line break at\hfil.
Also to be considered: Omega could be subject to hyphenation. This can be prevented by \mbox. Robuster variants to put Omega at the end of a paragraph:
~\hfill\mbox{Omega}\par
\hspace*{.5em plus 1fill}\mbox{Omega}\par
Second case: Alpha \hfil\ Omega in the middle of a paragraph
If Omega should be at the end of a line, then the line should end right after Omega:
\documentclass[a5paper]{article}
\usepackage[hmargin=24.5mm,vmargin=10mm]{geometry}
\usepackage{parskip}
\usepackage{color}
\usepackage{lipsum}
\begin{document}
1. \lipsum*[101]Alpha~\hfil\mbox{\color{red}Omega}\\
\lipsum[103]
2. \lipsum*[101]Alpha~\hfill\mbox{\color{red}Omega}\\
\lipsum[103]
\end{document}
- Case 1:
\hfilis also added by the new line\\. - Case 2:
\hfilloverrules\hfilby\\.
- 271,626
-
1Ah! This is advantage of having Heiko here in the site!. Very nice tutorial! +1. – Jun 07 '14 at 00:29
Every paragraph has \parfillskip glue added to the end of the last line. Normally the value of \parfillskip is 0pt plus 1fil, which exactly balances \hfil. Therfore,
Alpha\hfil Omega\par
will have equal space before and after "Omega". On the other hand
Alpha\hfill Omega\par
has infinitely stronger stretch on the left side of "Omega" and so "Omega" is flush with the right edge.
If \hfil or \hfill is used in the middle of a paragraph, if there is no linebreak forced by the user, one will usually find that TeX fills up the line as full as it can without shrinking any spaces, and then simply stretches \hfil or \hfill only as much as needed to fill the line. In order to flush the "Omega" to the right edge, one can use either \hfil or \hfil with a line break right after "Omega":
Alpha\hfil Omega\break Beta Gamma
If you break the line with \\ (a natural way to do that) one should use \hfill
because \\ adds \hfil (usually).
Alpha\hfill Omega\\ Beta Gamma
If \centering, \flushleft or \raggedright is in effect, then there will be glue that is added at the end of every line. That glue is usually equivalent to \hfil, so one would then also need \hfill to ensure "Omega" is at the right.
In summary, to flush "Omega" to the right edge, use \hfill before it and put a linebreak (or the end of the paragraph) right after it.
For more details and examples, see Heiko's fine answer.
- 6,899


\hfillinstead. – Sigur Jun 06 '14 at 17:33\hfillwill flush "Omega" to the right but why won't\hfildo that, too? – SJU Jun 06 '14 at 17:38\hfillis more powerful. – Sigur Jun 06 '14 at 17:40\parfillskipglue added to the end of the last line. Normally the value of\parfillskipis0pt plus 1fil, which exactly balances\hfil. – Dan Jun 06 '14 at 17:40\hfilis not on the last line of the paragraph, then it will only stretch as far as needed after the line has been filled with text. – Dan Jun 06 '14 at 17:43\hfilldo anything different on a line not last in a paragraph? – SJU Jun 06 '14 at 17:52\parfillskip=0pt plus1fill(fill .. double el). Then\hfilbefore Omega shrinks to 0pt when it occurs at the last line of the paragraph. But I don't understand why do you need to write \hfil between Alpha and Omega. So, more simple answer: remove \hfil between these words. – wipet Jun 06 '14 at 19:02