# How to duplicate content/vboxes?

I am trying to create exercise and answer environments for lecture notes, where I type \begin{exercise}... \end{exercise} followed by \begin{answer}...\end{answer} within the text. I want to include the exercise statements within the text, but have the exercise and its answer also appear at the end of the document. (Even better would be if they were written to a separate .tex file, but this has been a total failure, unless I stick to very basic TeX inside the environments.)

The following code works, if I remove the final \unvbox\temp@Exercisebox on line -8, which I am trying to use to copy the exercise environment before the answer. pdflatex dies with

! LaTeX Error: This may be a LaTeX bug.


Does anyone have an idea how to fix this, or is duplicating vboxes even possible? (There could be problems with references, etc.)

\makeatletter

\newtheorem{@exercise}{Exercise}
\numberwithin{@exercise}{chapter}
\newtheorem{@exercisestar}[@exercise]{Exercise*}
\newtheorem{@exerciselight}[@exercise]{Exercise(Light)}

\newbox\@Exercisebox
\newbox\temp@Exercisebox
\def\@unknown{UNKNOWN}
\let\@currentexercise=\@unknown
\def\@bex#1{%
\global\let\@currentexercise=\@unknown%
\let\@oldlabel=\label%
\def\label##1{\def\@args{##1}%
\global\let\@currentexercise=\@args%
\@oldlabel\@args
\let\label=\@oldlabel}%
\global\setbox\@Exercisebox\vbox\bgroup\begin{#1}}
\def\@eex#1{\ifx\@currentexercise\@unknown\label{ex:\the@exercise}\else\fi%
\end{#1}\egroup\global\setbox\temp@Exercisebox\copy\@Exercisebox%
\unvbox\@Exercisebox}
\newenvironment{exercise}{\@bex{@exercise}}{\@eex{@exercise}}
\newenvironment{exerciselight}{\@bex{@exerciselight}}{\@eex{@exerciselight}}
\newenvironment{exercisestar}{\@bex{@exercisestar}}{\@eex{@exercisestar}}

\newbox\all@Answerbox
\newbox\temp@Answerbox
\newbox\@Answerbox
\newcounter{@prevchapter}
\def\tmp@ref#1{\ref#1}%
\newenvironment{answer}{%
\global\setbox\@Answerbox\vbox\bgroup%
\par%
\topsep=0pt%
\itemsep=0pt%
\begin{trivlist}%
%\color{SkyBlue}%
\def\@args{\@currentexercise}
\item[\hskip \labelsep {\bfseries Answer to Exercise \expandafter\tmp@ref\@args.}]%
}{%
\qed\end{trivlist}%
\egroup%@Answerbox
\def\@tmp{}
\ifnum \value{@prevchapter} < \value{chapter} \def\@tmp{\section*{Exercises of chapter \thechapter}\par}\fi%
\global\setbox\temp@Answerbox\vbox{%
\unvbox\all@Answerbox\par%
\@tmp%
\unvbox\temp@Exercisebox\par\unvbox\@Answerbox\vskip\z@%
}%
\global\setbox\all@Answerbox\copy\temp@Answerbox%
\setcounter{@prevchapter}{\value{chapter}}
}

\newcommand{\shipoutAnswer}{\unvbox\all@Answerbox}
\makeatother


The code is modelled after the exercise package from CTAN, but I want to use theorem-style environments, and do the copying of the exercise statement with the answers. The package does not do this. My code also adds section headings for answers within each chapter, and working hyperlinks (when hyperref is loaded) from the answer heading to the exercise.

• »I want to use theorem-style environments, and do the copying of the exercise statement with the answers« This is possible with the xsim package, see for example tex.stackexchange.com/q/380959 (different layout but otherwise the same question) – clemens Mar 5 '18 at 11:00