2

I have the following environments which are being used in my document. As you can see, a lot of unnecessary complexity is being added by my arcane optional argument handling using \IfValueTF. Is there a way to simplify this code such that expl3 'does what I'm thinking'? I'd like to be able to express the logic with something like \begin{displayquote}[#1], where #1 retains its value/no-value status when it's passed to 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}
  • In my opinion you can use the argument specification O{} instead of o. So you don't need any tests. – Marco Daniel Nov 3 '13 at 16:09
  • @MarcoDaniel That would be a good option in some cases, but I'm not sure about this one. An empty argument will still produce the citation punctuation with displayquote, if I'm understanding you correctly. – Sean Allred Nov 3 '13 at 16:13
  • Oh sorry I didn't test it with displayquote. I thought the environment itself tests whether the argument is empty or not. – Marco Daniel Nov 3 '13 at 16:17
  • What do you think about: \usepackage{letltxmacro} \ExplSyntaxOn \LetLtxMacro{\citeverse}{\displayquote} \LetLtxMacro{\endciteverse}{\enddisplayquote} \NewDocumentEnvironment { citeverse* } { o } { \setlength{\parindent}{0pt} \setlength{\parskip}{.5\baselineskip} \setlength{\parfillskip}{0pt} \setlength{\emergencystretch}{.5\textwidth} \citeverse } { \endciteverse } – Marco Daniel Nov 3 '13 at 18:27
  • 1
    @MarcoDaniel Daisy chaining? Seems like a good idea to me for this application of the general principle, but if a solution exists for the general case (perhaps you might need to duplicate the optional arguments) that'd be ideal. – Sean Allred Nov 3 '13 at 18:52
3

You can abstract the construction:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.