¿Cómo puedo alinear a la derecha la atribución de una cotización, en la misma línea que la cotización si es posible?

¿Cómo puedo alinear a la derecha la atribución de una cotización, en la misma línea que la cotización si es posible?

A menudo las citas se establecen con la atribución alineada a la derecha:

Cita con atribución alineada a la derecha en la línea debajo de la cita

La atribución está en una nueva línea después de la cita. Sin embargo, supongamos que hubiera suficiente espacio vacío en la última línea de la cita para contener la atribución. Entonces es posible que deseemos que esté en la misma línea para evitar espacios en blanco excesivos.

Cita con atribución alineada a la derecha en la misma línea

Nos gustaría definir un comando (llámelo quoteattr) que calcule si la cita encajará y la colocará en consecuencia, automáticamente.

Código sin definición de comando:

\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}

Respuesta1

Parece que el tabtopaquete puede ayudar en este sentido.

\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}

ingrese la descripción de la imagen aquí

Respuesta2

Esto se puede manejar al estilo clásico de TeX con una macro deEl libro de texto, p106, simplemente jugando con penalizaciones, saltos y casillas:

\def\signed #1 (#2){{\unskip\nobreak\hfil\penalty50
    \hskip2em\hbox{}\nobreak\hfil\sl#1\/ \rm(#2)
    \parfillskip=0pt \finalhyphendemerits=0 \par}}

Esto le permitirá ingresar sus cotizaciones de la siguiente manera:

\begin{quote}Here is one way to solve the problem.
    \signed Donald E. Knuth (Berkeley, CA)
\end{quote}

Si hay espacio para 2em y la firma en la misma línea, ahí va; de lo contrario, pasa a una nueva línea. Por supuesto, tal vez sólo quieras un nombre, en lugar de nombre y ubicación, o tal vez simplemente te guste un poco más al estilo LaTeX; en ese caso, podrías hacer algo como esto:

\def\signed#1{{\unskip\nobreak\hfil\penalty50
    \hskip2em\hbox{}\nobreak\hfil\sl#1
    \parfillskip=0pt \finalhyphendemerits=0 \par}}

Y lo ingresarías como arriba, pero con \signed{Donald E. Knuth}en su lugar.

Respuesta3

Para implementar esto, usaremos \newsaveboxpara encontrar la longitud de llenado de la línea vacía. No podemos usar \hfill, ya que siempre tiene una longitud nominal de 0. En su lugar, usaremos el linegoalpaquete.

También usaremosxifthen para permitirnos escribir la condición.

Serán necesarios dos pases.

\documentclass[12pt]{book}

\usepackage{linegoal}
\usepackage{xifthen}

\newsavebox{\quotebox}
\newlength{\emptywidth}% To save the length of empty space
\newlength{\attrwidth}% To save the length of the attribution

% Define the new command
\newcommand{\quoteattr}[2]{
  \begin{quote}#1%
  \begin{lrbox}{\quotebox}\hspace{\linegoal}\end{lrbox}
  \setlength{\emptywidth}{\linegoal}
  \settowidth{\attrwidth}{\textit{#2}}
  % The following line can be tweaked to force attributions
  % onto a new line if it would be a very tight squeeze
  \addtolength{\attrwidth}{0em}
  % Compare the two widths.
  \ifthenelse{\lengthtest{\emptywidth>\attrwidth}}
    % If it fits:
    {\null\hfill\textit{#2}}
    % If it doesn't:
    % We need to provide a stretch in this way because TeX
    % might compress a slightly-too-long line to prevent
    % a loose line followed by a lonely word. However, then
    % on the next run, TeX won't compress it because of the
    % following text on a new line.
    {\hspace{0pt plus \textwidth minus 3pt}\null\nopagebreak\linebreak\null\hfill{\textit{#2}}}%

  % For debugging, display the lengths.
  Empty space: \the\emptywidth
  \\Attribution space: \the\attrwidth
  \end{quote}
}

\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}

información relacionada