Is it possible to reliably right-align the last word of a paragraph?

Using \hfill works most of the time, looking like

Left stuff                             Right stuff.

but fails if the rest after \hfill doesn't fit into the line. In that case the result looks like

Left stuff. Bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla
Right stuff.

but I want to get

Left stuff. Bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla
                                        Right stuff.

Actual example document

enter image description here

\documentclass[12pt]{article}

\begin{document}

\noindent\textbf{In real document, I would use \texttt{\string\hfill} instead of \texttt{\string\hrulefill}. The latter just demonstrates it better in an example document.}

.

\noindent\textbf{The command \texttt{\string\hfill} is useful for aligning short text to left and right easily.}

\noindent Bla bla.
\hrulefill Yada yada.

.

\noindent\textbf{If the parts get too long, they are not correctly aligned.}

\noindent Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla.
\hrulefill Yada  Yada Yada  Yada Yada  Yada Yada  Yada Yada.

.

\noindent\textbf{Boxes don't help.}

\noindent \hbox{Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla.}
\hrulefill \hbox{Yada  Yada Yada  Yada Yada  Yada Yada  Yada Yada.}

.

\noindent\textbf{What I am actually trying to do.}

\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla 
\hrulefill (statement:1)

.

\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla Bla Bla
\hrulefill (statement:1)

.

\noindent\textbf{An approximation of what it should look like, if the last line before ``(statement:1)'' is full.}

\noindent Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla bla Bla Bla Bla
\\.\hrulefill (statement:1)


\end{document}
up vote 6 down vote accepted

There is a nice example in The TeXbook to do this. See chapter 14, page 106.

\documentclass{article}
\def\signed #1 (#2){{\unskip\nobreak\hfil\penalty50
  \hskip2em\hbox{}\nobreak\hfil\sl#1\/ \rm(#2)
  \parfillskip=0pt \finalhyphendemerits=0 \par}}
\begin{document}
\hsize 9cm
This is a case where the name and address fit in nicely with the review.
\signed A. Reviewer (Ann Arbor, Mich.)

But sometimes an extra line must be added. \signed N. Bourbaki (Paris)
\end{document}

output

You could make it a bit more standard for LaTeX:

\documentclass{article}
\newcommand{\signed}[1]{%
  {\unskip\nobreak\hfil\penalty50
   \hskip2em\hbox{}\nobreak\hfil#1
   \parfillskip=0pt \finalhyphendemerits=0 \par}}
\begin{document}
\hsize 9cm
This is a case where the name and address fit in nicely with the review.
\signed{\emph{A. Reviewer} (Ann Arbor, Mich.)}

But sometimes an extra line must be added. \signed{\emph{N. Bourbaki} (Paris)}
\end{document}

And the explanation of how it works from The TeXbook:

If a line break occurs at the \penalty50, the \hskip2em will disappear and the empty \hbox will occur at the beginning of a line, followed by \hfil glue. This yields two lines whose badness is zero; the first of these lines is assessed a penalty of 50. But if no line break occurs at the \penalty50, there will be glue of 2 em plus 2 fil between the review and the name; this yields one line of badness zero. TEX will try both alternatives, to see which leads to the fewest total demerits. The one-line solution will usually be preferred if it is feasible.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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