32

I have a simple text. Right after the end of the text I use \hfill to get some words right aligned.

 \documentclass[11pt]{article}

    \begin{document}
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est
        \hfill
        \textbf{some words}
    \end{document}

The problem is now that when the text ends at the end of the line, the words after \hfill won't be right aligned anymore.

\documentclass[11pt]{article}

\begin{document}
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    \hfill
    \textbf{some words}
\end{document}

How can I achieve the words to be right aligned even if the text fits the line?

Martin Scharrer
  • 262,582
kraeki
  • 333
  • 4
    In the second case the \hfill might be part of the previous line, not the some words line. Adding \hspace*{0pt} before \hfill helps, but I'm not 100% sure why exactly. I would also place \textbf{some words} inside a \mbox{...} to avoid line breaks there. However, this might not catch every possibility. Ulrike's answer taken from The TeXBook seems to do this. – Martin Scharrer Apr 21 '11 at 14:07
  • 3
    Ulrike's answer is good. here's the reason: spaces at the beginning of a line are ignored by TeX. when you have a line that ends at the right margin, the \hfill becomes space at the beginning of the next line, and disappears -- no flush right. there are several ways to get around this problem. the \hspace*{0pt} forces that space to not disappear, and the \hfill overwhelms the space normally inserted at the end of a paragraph. the TeXbook exercise takes another approach, forcing the paragraph to end flush right, so only \hfil is needed. – barbara beeton Apr 21 '11 at 14:37

2 Answers2

27

That's an exercise in the TeXbook.

\documentclass[11pt]{article}
\newcommand\quelle[1]{{%
      \unskip\nobreak\hfil\penalty50
      \hskip2em\hbox{}\nobreak\hfil\textbf{#1}%
      \parfillskip=0pt \finalhyphendemerits=0 \par}}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    \hfill
    \textbf{some words}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
\quelle{some words}


\end{document}
Ulrike Fischer
  • 327,261
  • Is there a way to make this work while inside of \section{} and \subsection{}? – EL_DON Feb 28 '18 at 21:45
  • This solution doesn't work in places where you don't want \par at the end of the logical paragraph, for instance at the end of a footnote. – richard Feb 04 '24 at 19:58
11

It seems rather hackish. You could use something like

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.   
At vero eos et accusam et justo duo dolores et ea rebum. \mbox{}~\hfill \textbf{some~words}

Or maybe just use rightflush?

Rasmus
  • 1,256
  • 1
    One issue with this is that it doesn't let you wrap the some words part in an mbox; if you do and the mbox needs to be on the next line, the last lipsum line will be stretched to reach the right margin. – Clément Sep 29 '15 at 13:39
  • I ran into the issue when trying to fit "Hello World \hfill{} alpha - beta" onto two lines. The "alpha - beta" would go on the second line, at the start. I used the above and "Hello" would be on the first line, "World" on the second at the start and "alpha - beta" at the end. Using \hfill{}\mbox{}~\hfill{} put the text correctly for me. – Peilonrayz Jul 11 '23 at 15:03