# \hfill after linebreak

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?

• 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
• 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

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}

• exactly what I needed... – yolo Dec 14 '11 at 12:17
• Is there a way to make this work while inside of \section{} and \subsection{}? – EL_DON Feb 28 '18 at 21:45

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?

• 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