8

Why do Alpha \hfil\ Omega\par and Alpha \hfil\ Omega not flush "Omega" to the right?

  • Use \hfill instead. – Sigur Jun 6 '14 at 17:33
  • 1
    @Sigur I know that \hfill will flush "Omega" to the right but why won't \hfil do that, too? – SJU Jun 6 '14 at 17:38
  • 1
    They have different power of stretchability. \hfill is more powerful. – Sigur Jun 6 '14 at 17:40
  • 4
    As to "why": 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. – Dan Jun 6 '14 at 17:40
  • 1
    Related (duplicate?): tex.stackexchange.com/questions/21022/… – Mario S. E. Jun 6 '14 at 17:40
8

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.

| improve this answer | |
10

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}

Result

  • 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. Its 0pt plus 1fil gets the same space as the \hfil before.

  • Case 2: The second l of \hfill overrules 1fil of \parfillskip.

  • Case 3: The trick with an added negative \parfillskip does not work, because TeX removes a horizontal glue at the end of the paragraph before inserting \parfillskip.

  • Case 4: \null prevents 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: \parfillskip is locally reset to zero for this paragraph.

  • Case 7: At the start of a line \hfil is removed.

  • Case 8/9: The no-break-space ~ or \nobreak prevents 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}

Result

  • Case 1: \hfil is also added by the new line \\.
  • Case 2: \hfill overrules \hfil by \\.
| improve this answer | |
  • 1
    Ah! This is advantage of having Heiko here in the site!. Very nice tutorial! +1. – user11232 Jun 7 '14 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.