末尾附有作者参考的引文

末尾附有作者参考的引文

我已经重新定义了引用环境以允许我将作者放在引用的末尾,但我的代码中某处有一个错误,有人可以检查一下:

%Add source argument to quote
\let\oldquote\quote
\newsavebox\first
\newsavebox\second
\renewenvironment{quote}[2][0]
  {\savebox\second{#1}
   \savebox\first{#2}
  \begin{oldquote}}
  {\smallskip\par\hfill(\usebox\first ~---~\usebox\second)\bigskip\break
  \end{oldquote}}

如果我尝试使用超过 6 个,我会收到“嵌套太深”错误,所以我认为我没有正确结束环境,但我找不到错误。

答案1

当 LaTeX 发现\end{oldquote}它时,它只会结束该组,而不会结束(原始)quote环境,因为\endoldquote没有定义。

同时添加

\let\endoldquote\endquote

删除最后的\break,其唯一的作用是发出“Underfull \hbox(badness 10000)”警告。


目前还不清楚为什么更新的quote环境有一个可选参数:将默认值设置0

\begin{quote}{Somebody}
<text>
\end{quote}

打印“(Somebody — 0)”,这似乎不是我想要的。

也可以看看引用环境并在末尾引用对于一个非常相似的问题。


可能的改进:

\documentclass{article}

\let\oldquote\quote
\let\endoldquote\endquote
\renewenvironment{quote}[2][]
  {\if\relax\detokenize{#1}\relax
     \def\quoteauthor{#2}%
   \else
     \def\quoteauthor{#2~---~#1}%
   \fi
   \oldquote}
  {\par\nobreak\smallskip\hfill(\quoteauthor)%
   \endoldquote\addvspace{\bigskipamount}}

\begin{document}

\begin{quote}{Somebody}
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
\end{quote}

\begin{quote}[Somewhere]{Somebody}
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
\end{quote}

\end{document}

\par请注意和之间的反转\smallskip,并添加了\nobreak以避免在引用和归因之间出现分页符。无需将参数存储在框中;将它们存储在宏中更容易,当缺少可选参数时,宏将包含不同的内容。此外,最好\bigskip将 放在关闭(原始) 之后quote

在此处输入图片描述

一种更简单的方法xparse,可以在“结束部分”中使用参数:

\documentclass{article}
\usepackage{xparse}

\let\oldquote\quote
\let\endoldquote\endquote

\RenewDocumentEnvironment{quote}{om}
  {\oldquote}
  {\par\nobreak\smallskip
   \hfill(#2\IfValueT{#1}{~---~#1})\endoldquote 
   \addvspace{\bigskipamount}}

\begin{document}

\begin{quote}{Somebody}
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
\end{quote}

\begin{quote}[Somewhere]{Somebody}
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
This is a quote This is a quote This is a quote
\end{quote}

\end{document}

相关内容