如果可能的話,如何將引文歸屬右對齊到與引文同一行?

如果可能的話,如何將引文歸屬右對齊到與引文同一行?

通常,引用的屬性右對齊:

引用下方行中帶有右對齊屬性的引用

出處位於引用後的新行。然而,假設引文的最後一行有足夠的空白來保存屬性。然後我們可能希望它位於同一行以避免過多的空白。

同一行上帶有右對齊屬性的引用

我們想要定義一個命令(稱為quoteattr)來計算報價是否適合併自動相應地放置它。

沒有命令定義的程式碼:

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

答案1

看來該tabto軟體包可以在這方面提供幫助。

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

在此輸入影像描述

答案2

這可以透過經典的 TeX 方式使用巨集來處理電子書,p106,只需擺弄懲罰、跳過和框框:

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

這將允許您按如下方式輸入報價:

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

如果同一行有 2em 的空間和簽名,就這樣;否則,它將換行。當然,也許您只是想要一個名稱,而不是名稱和位置,或者您可能只是喜歡它更像 LaTeX 風格;在這種情況下,你可以這樣做:

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

您可以像上面那樣輸入它,但用\signed{Donald E. Knuth}代替。

答案3

為了實現這一點,我們將使用\newsavebox來尋找填充空白行的長度。我們不能使用\hfill,因為它的標稱長度始終為 0 linegoal

我們也將使用xifthen允許我們編寫條件。

需要兩次通過。

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

相關內容