Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
environ doesn't expand the body as far as I can see? –  David Carlisle Dec 10 '12 at 2:10

1 Answer 1

up vote 13 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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