1

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:

image of situation described above, with attribution starting partway through a line

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

enter image description here

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. – egreg Apr 29 '18 at 22:00
2

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:

enter image description here

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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