Skip to main content

Passing arguments to the end part of \newenvironment

I recently found myself with repeated use of quote environments like this:

\begin{quote}{\sl
  To call two elements ``independent'' is to say the chances
  of one failing are not linked in any way to the chances of
  the other failing.
}\\\hspace*\fill\citep{downer-failure}
\end{quote}

So it's an ordinary quote plus

  • slanted style around the actual citation
  • a note on the author

By creating my own custom environment fancyquote I could reduce duplication and at the same time make it more readable. I wanted to be able to write it like this, instead:

\begin{fancyquote}[\citep{downer-failure}]
  To call two elements ``independent'' is to say the chances
  of one failing are not linked in any way to the chances of
  the other failing.
\end{fancyquote}

If it had worked I would had stopped at this:

\newenvironment{fancyquote}[1][]{%
  \begin{quote}%
    \begingroup\sl%
}{%
    \endgroup\\
    \hspace*\fill#1%
  \end{quote}%
}

What's the problem here? Arguments (#1) cannot be used in the second half of environment definitions.

It may not contain any argument parameters. [1]

What? No really. Now that I know that an environment definition makes two commands \begin envname and \end envname it even makes a bit of sense to me. On a side note: \begingroup and \endgroup are replacement for curly brackets that work in syntactically unbalanced manner. So still, I wanted that environment. In the end I came to using a savebox to give the second half access to my original value of #1:

\newsavebox{\fancyquotesavebox}
\newenvironment{fancyquote}[1][]{%
  \savebox{\fancyquotesavebox}{#1}
  \begin{quote}%
    \begingroup\sl%
}{%
    \endgroup\\
    \hspace*\fill\usebox{\fancyquotesavebox}%
  \end{quote}%
}

In contrast to the first approach now this does work.. as longs as you don't run into a need to nest it. If you know how to use a proper stack for this I'd be interested to hear.