[Tex/LaTex] \hfill after linebreak

horizontal alignmentline-breaking

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?

Best Answer

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}
Related Question