# Left-align the attribution for a quotation if it is more than a full line long

This question is closely related to How can I right-align a quotation attribution, on the same line as the quotation if possible?

I am currently using the TeXBook's excellent solution, among those described in that question, except for a minor change in spacing (a 1em gap between the quotation and the attribution instead of 2em):

\def\attr#1{{\unskip\nobreak\hfil\penalty50
\hskip1em\hbox{}\nobreak\hfil\itshape---#1
\parfillskip=0pt \finalhyphendemerits=0 \par}}


However, an additional problem has arisen: I am setting this text in two columns, and some attributions end up breaking onto multiple lines. This tends to look silly right-aligned:

In general, I would much rather have the attribution begin left-aligned on a new line, should it require more than one line:

I need an automated solution because I'm generating the text programmatically. I will be doing some manual tweaking afterwards, but as I'm typesetting over 10,000 of these entries, anything that can be done to get most of them right automatically is a huge time-saver.

• It would be possible to typeset the quote in a box, measure the width of the last line, then measure the attribution and decide what's the thing to do. Apr 29 '18 at 22:00

Here's my shot at it, using etoolbox and calc:

\usepackage{etoolbox}
\usepackage{calc}

\newlength\attrlength
\def\attr#1{
\setlength{\attrlength}{\widthof{#1}}
\ifdimcomp{\attrlength}{<}{\columnwidth}
{\shortattr{#1}}
{\longattr{#1}}}

\def\shortattr#1{{\unskip\nobreak\hfil\penalty50
\hskip1em\hbox{}\nobreak\hfil\emph{---#1}
\parfillskip=0pt \finalhyphendemerits=0 \par}}
\newcommand\longattr[1]{\par\noindent\emph{---#1}}
\newcommand\longattrnobreak[1]{\hspace{1em}\emph{---#1}}


We set \attrlength to the width of the argument (which is the text we want to set as the attribution), then if it's less than the width of a column we use the TeXBook's macro described in the question; otherwise we start a new paragraph and set the text there.

The final macro, \longattrnobreak, is used in cases where the entry looks better when the attribution starts on the same line as the quote but is left-aligned thereafter:

I can't think of a clean, easy-to-implement heuristic for this (from my five minutes thinking about it, it involves both the width of the last line of the quotation and the width of the last line of the attribution, plus how that line's width compares to the one above it), so I'll choose this one manually at proof time if the automatic choice of the first two options looks bad.