我正在尝试配置和优化我的自定义引用命令。我喜欢这样设置它,将引用的文本作为强制参数,将引用作者和来源/说明作为两个可选参数。
这是我的 MWE:
% !TEX program = xelatex
\documentclass[12pt, oneside]{book}
\setlength{\parindent}{0pt}
% \chapquote command for quotes with author or source
\usepackage{xparse}
\NewDocumentCommand\chapquote{mO{}O{}}{
\begin{quotation}
\noindent \itshape{#1}
\IfValueTF {#2}{
\vspace*{-0.5\baselineskip}
\begin{flushright}
\textemdash \quad \normalfont{#2} \par \footnotesize\itshape{#3}
\end{flushright}
}{}
\end{quotation}
}
% \quote command for quotes with no author or source
\RenewDocumentCommand\quote{m}{
\begin{quotation}
\noindent \itshape{#1}
\end{quotation}
}
\begin{document}
Example 1. % Quote with both author and source
\chapquote{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}[Quote Author][Quote Source]
Example 2. % Quote with author only. Here the source line (#3) space should be removed.
\chapquote{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}[Quote Author][]
Example 3. % Quote with source only. Here the author line (#2) space should be removed and the emdash should be added to the source line.
\chapquote{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}[][Quote Source]
Example 4. % Quote without author and source
\chapquote{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
Example 5. % Quote without author and source
\quote{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
\end{document}
我想设置\chapquote
引文 ( #1
) 只显示来源 ( #3
),不显示作者 ( #2
)。我的输出Example 3
应该是这样的:
Example 3.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. (#1)
— Quote Source (#3)
当没有指定可选参数时,我希望通过\quote
和\chapquote
( )获得相同的输出。Example 4 & 5
答案1
例如,您可以\chapquote
像这样定义宏:
\def\chapquote#1{%
\begin{quotation}
\noindent \itshape{#1}
\futurelet\next\chapquoteA
}
\def\chapquoteA{\ifx\next[\expandafter\chapquoteB\else\expandafter\chapquoteC\fi}
\def\chapquoteB[#1][#2]{%
\vskip.2\baselineskip
\hfill ---\quad
\ifx^#1^\else \normalfont #1\par \hfill \fi
\footnotesize\itshape #2\par
\chapquoteC
}
\def\chapquoteC{\end{quotation}}