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?

2
  • 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. Apr 21, 2011 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. Apr 21, 2011 at 14:37

2 Answers 2

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}
2
  • Is there a way to make this work while inside of \section{} and \subsection{}?
    – EL_DON
    Feb 28, 2018 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 4 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?

2
  • 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, 2015 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, 2023 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .