如何将可选参数作为可选参数传递?

如何将可选参数作为可选参数传递?

我的文档中使用了以下环境。如您所见,我使用 进行晦涩难懂的可选参数处理增加了很多不必要的复杂性\IfValueTF。有没有办法简化此代码,使其expl3“按照我的想法做”?我希望能够用类似 的方式来表达逻辑\begin{displayquote}[#1]#1当它传递给 时,保留其值/无值状态displayquote

\documentclass{article}
\usepackage{expl3,xparse,csquotes} \ExplSyntaxOn

\NewDocumentEnvironment { citeverse } { o } {
  \IfValueTF{#1}
    {\begin{displayquote}[#1]}
    {\begin{displayquote}}
} {
  \end{displayquote}
}

\NewDocumentEnvironment { citeverse* } { o } {
  \IfValueTF{#1}
    {\begin{citeverse}[#1]}
    {\begin{citeverse}}

  % make square paragraph
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{.5\baselineskip}
  \setlength{\parfillskip}{0pt}
  \setlength{\emergencystretch}{.5\textwidth}
} {
  \end{citeverse}
}

\NewDocumentEnvironment { pageverse } { o } {
  \thispagestyle{empty}
  \null
  \vfill
  \IfValueTF{#1}
    {\begin{citeverse}[#1]}
    {\begin{citeverse}}
} {
  \end{citeverse}
  \vfill
  \null
}

\NewDocumentEnvironment { pageverse* } { o } {
  \IfValueTF{#1}
    {\begin{citeverse*}[#1]}
    {\begin{citeverse*}}
} {
  \end{citeverse*}
}

\ExplSyntaxOff
\begin{document}
\begin{citeverse}
Easy come, easy go.
\end{citeverse}

\begin{citeverse}[Winston Churchill]
You have enemies?  Good.
That means you've stood up for something,
  sometime in your life.
\end{citeverse}

\begin{citeverse*}
A bird in the hand beats two in the bush.
In other news, this needs to be made longer
  to really show off the square paragraph functionality.
\end{citeverse*}

\begin{citeverse*}[Benjamin Disraeli]
Action may not always bring happiness;
  but there is no happiness without action.
\end{citeverse*}

\begin{pageverse}
Coming with quotations is hard.
\end{pageverse}

\begin{pageverse}[Abraham Lincoln]
Tact is the ability to describe others as they see themselves.
\end{pageverse}

\begin{pageverse*}
This is supposed to be a really long quotation---one that fills an entire page,
  but I've completely run out of axioms to use!
I can only take comfort in the trend that axioms are short and pithy.
\end{pageverse*}

\begin{pageverse*}[Albert Einstein]
Only one who devotes himself to a cause with
  his whole strength and soul can be a true master.
For this reason mastery demands all of a person.
\end{pageverse*}
\end{document}

答案1

你可以抽象出这样的结构:

\documentclass{article}
\usepackage{expl3,xparse,csquotes}

\ExplSyntaxOn

\NewDocumentCommand{\DefineSubEnvironment}{ m m m m m m }
 {
  \NewDocumentEnvironment { #1 } { o }
   {
    #3
    \IfValueTF{##1}{\begin{#2}[##1]}{\begin{#2}}
    #4
   }
   {
    #5
    \end{#2}
    #6
   }
 }

\DefineSubEnvironment { citeverse } { displayquote }
  { } % pre begin
  { } % after begin
  { } % pre end
  { } % after end

\DefineSubEnvironment { citeverse*} { citeverse } 
  { } % pre begin 
  {   % after begin
   % make square paragraph
   \setlength{\parindent}{0pt}
   \setlength{\parskip}{.5\baselineskip}
   \setlength{\parfillskip}{0pt}
   \setlength{\emergencystretch}{.5\textwidth}
  }
  { } % pre end
  { } % after end

\DefineSubEnvironment { pageverse } { citeverse }
  {   % prebegin
   \clearpage
   \thispagestyle{empty}
   \vspace*{\fill}
  }
  { } % after begin
  { } % pre end
  {   % after end
   \vspace*{\fill}
   \clearpage
  }

\DefineSubEnvironment { pageverse* } { citeverse* }
  { } % pre begin
  { } % after begin
  { } % pre end
  { } % after end

\ExplSyntaxOff

\begin{document}
\begin{citeverse}
Easy come, easy go.
\end{citeverse}

\begin{citeverse}[Winston Churchill]
You have enemies?  Good.
That means you've stood up for something,
  sometime in your life.
\end{citeverse}

\begin{citeverse*}
A bird in the hand beats two in the bush.
In other news, this needs to be made longer
  to really show off the square paragraph functionality.
\end{citeverse*}

\begin{citeverse*}[Benjamin Disraeli]
Action may not always bring happiness;
  but there is no happiness without action.
\end{citeverse*}

\begin{pageverse}
Coming with quotations is hard.
\end{pageverse}

\begin{pageverse}[Abraham Lincoln]
Tact is the ability to describe others as they see themselves.
\end{pageverse}

\begin{pageverse*}
This is supposed to be a really long quotation---one that fills an entire page,
  but I've completely run out of axioms to use!
I can only take comfort in the trend that axioms are short and pithy.
\end{pageverse*}

\begin{pageverse*}[Albert Einstein]
Only one who devotes himself to a cause with
  his whole strength and soul can be a true master.
For this reason mastery demands all of a person.
\end{pageverse*}
\end{document}

相关内容