[Tex/LaTex] How to right-align a quotation attribution, on the same line as the quotation if possible

line-breakingquoting

Often quotations are set with the attribution aligned right:

Quotation with aligned-right attribution on line below quotation

The attribution is on a new line after the quotation. Suppose however, that there were enough empty space on the last line of the quotation to hold the attribution. Then we might want it to be on the same line to avoid excessive whitespace.

Quotation with aligned-right attribution on same line

We would want to define a command (call it quoteattr) that calculates whether the quotation will fit and will place it accordingly, automatically.

Code without command definition:

\documentclass[12pt]{book}

\begin{document}

\quoteattr{Early to bed and early to rise,\\
Makes a man healthy, wealthy, and wise.}{Benjamin Franklin, 1706--1790}

\quoteattr{Seek not the favor of the multitude; it is seldom got by honest and lawful means. But seek the testimony of few; and number not voices, but weigh them.}{Immanuel Kant, 1724--1804}

\end{document}

Best Answer

It appears that the tabto package can help in this regard.

\documentclass[12pt]{book}
\usepackage{tabto}
\def\quoteattr#1#2{\setbox0=\hbox{#2}#1\tabto{\dimexpr\linewidth-\wd0}\box0}
\parskip 1em
\begin{document}
\quoteattr{Early to bed and early to rise,\\
Makes a man healthy, wealthy, and wise.}{Benjamin Franklin, 1706--1790}

\quoteattr{Seek not the favor of the multitude; it is seldom got by honest and lawful means. But seek the testimony of few; and number not voices, but weigh them.}{Immanuel Kant, 1724--1804}

\quoteattr{In attempting to make this not give enough space... Seek not the favor of the multitude; it is seldom got by honest and lawful means. But seek the testimony of few; and number not voices, but weigh them.}{Immanuel Kant, 1724--1804}
\end{document}

enter image description here

Related Question