10

I'm trying to define an environment test that stores its contents unexpanded in a macro \testcontents.

For example, the code

\newcommand{\cmd}{bad}

\begin{test}
\cmd
\end{test}

\renewcommand{\cmd}{okay}

\testcontents

should print out "okay".

How would I define such an environment? I know that the environ package could be used to store the contents after expansion, but I need them unexpanded.

  • 1
    environ doesn't expand the body as far as I can see? – David Carlisle Dec 10 '12 at 2:10
13

environ works as you require, it collects the body without expansion, the following typesets okay

\documentclass{article}

\usepackage{environ}


\NewEnviron{test}
{\global\let\testcontents\BODY}

\begin{document}


\newcommand{\cmd}{bad}

\begin{test}
\cmd
\end{test}

\renewcommand{\cmd}{okay}

\testcontents

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