The \def
macro doesn't accept \begingroup
...\endgroup
instead of { }
.
No macro does. Some macros (like \hbox
) accept \bgroup
and \egroup
which are implicit braces, i.e. created using \let\bgroup={
and \let\egroup=}
(the =
is optional).
To store the content of an environment use the environ
package which was created for this purpose. The content is presented as \BODY
which can be copied to \answertext
. Keep in mind that the code is executed inside the environment, i.e. in a group, therefore \global\let\answertext\BODY
or \xdef\answertext{\BODY}
must be used. However, this only allows one answer text to be stored. If you want to accumulate answers use either \g@addto@macro<macro>{<code>}
, e.g. \expandafter\g@addto@macro\expandafter\answertext\expandafter{\BODY}
or the \xappto
macro of the etoolbox
package.
Some example (without the exam
class):
\documentclass{article}
\usepackage{environ}
\newcommand{\answertext}{}
\NewEnviron{answer}{\global\let\answertext\BODY}
% or
\makeatletter
\NewEnviron{answer}{\expandafter\g@addto@macro\expandafter\answertext\expandafter{\BODY}}%
\makeatother
% or
\usepackage{etoolbox}
\NewEnviron{answer}{\xappto\answertext{\expandonce{\BODY}}}%
\begin{document}
The question
\begin{answer}
The answer text
\end{answer}
\newpage
% Some time later
\answertext
\end{document}