# Solutions to problem sheets

For writing problem sheets I have a customized theorem environment called pr for the problem and a specialcomment called solution for the solution. See example below. Now my question is, if it is possible to get automatically the following:

First page(s): Problem sheet without solutions
next page(s): Problem sheet with solution below each problem

I would strongly prefer solutions which don't use a special documentclass like exam.

\documentclass[a4paper, 11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %
\usepackage{nccmath}
\usepackage{amsfonts} %
\usepackage{enumerate}
\usepackage[amsmath]{ntheorem}%

\makeatletter
\newtheoremstyle{mystyle}%
##1\ ##2\theorem@separator}\hbox{\strut}}}]}%}
\hfill{\normalfont(##3)}\newline}
\theorembodyfont{\normalfont}
\theoremindent0cm
\makeatother
\theoremstyle{mystyle}

\newtheorem{pr}{Problem}

\usepackage{comment}
\specialcomment{solution}{\noindent\small Solution: }{\\\normalfont \normalsize}

%\excludecomment{solution}

\begin{document}

\begin{pr}
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\begin{pr}
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\end{document}

-
For this feature you must store the contents of pr and solution in a separate savebox to use pr twice. – Marco Daniel Dec 29 '11 at 13:38
Regular TeX is an interpreted language and TeX engine buffers enough content for one paragraph before it makes tokenification and produces the content (actually if I recall correctly it has to pass five times to produce the finished product dvi file). I would be surprised it you could do above with out storing things as suggested. I am not aware that TeX can do goto statements (I am recently thinking of it as a general programming language) which could be used in a very ugly way to accomplish above. – Predrag Punosevac Dec 29 '11 at 14:11
@Predrag: I don't think anything in your first sentence is right. TeX is a macro language, not interpreted (okay, a small distinction); it doesn't buffer at all, but interprets "as it goes", though it does produce paragraphs one at a time. It also tokenizes the input as it goes, though you may be thinking that it does line breaks only after reading a whole paragraph (true). It also does only one pass; it never reinterprets the same code unless you put it back in the input yourself. LaTeX can do multiple passes, but a) LaTeX, and b) I've never seen five. – Ryan Reich Dec 29 '11 at 15:38
@user40111: Why not use exam? You're entitled to your preferences, of course, and you can just say "so I can learn how to do this", but if you just want results, that's the right answer. Otherwise, it's like saying you don't want to use TeX to write your document. – Ryan Reich Dec 29 '11 at 15:45
@RyanReich Perhaps you are right... I will have a closer look on the exam class if I have more time... – student Dec 29 '11 at 17:00

You can store away the contents of the pr and solution environments and print only the former in the first pass. Then you can print automatically the stored text. Here's a way:

\documentclass[a4paper, 11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %
\usepackage{nccmath}
\usepackage{amsfonts} %
\usepackage{enumerate}
\usepackage[amsmath]{ntheorem}%

\makeatletter
\newtheoremstyle{mystyle}%
##1\ ##2\theorem@separator}\hbox{\strut}}}]}%}
\hfill{\normalfont(##3)}\newline}
\theorembodyfont{\normalfont}
\theoremindent0cm
\makeatother
\theoremstyle{mystyle}

\newtheorem{prinn}{Problem}
\newenvironment{solinn}

\usepackage{environ}
\newtoks\prsoltoks
\NewEnviron{pr}[1][]{%
\global\prsoltoks=\expandafter{\the\prsoltoks\prinn}%
\if\relax\detokenize{#1}\relax\else
\global\prsoltoks=\expandafter{\the\prsoltoks[#1]}%
\fi
\global\prsoltoks=\expandafter{\the\expandafter\prsoltoks\BODY\endprinn}%
\showthe\prsoltoks
\expandafter\prinn\if\relax\detokenize{#1}\relax\else[#1]\fi\BODY\endprinn
}
\NewEnviron{solution}{%
\global\prsoltoks=\expandafter{\the\prsoltoks\begin{solinn}}%
\global\prsoltoks=\expandafter{\the\expandafter\prsoltoks\BODY\end{solinn}}%
}

\AtEndDocument{\clearpage\begin{center}\Large Solutions\end{center}
\setcounter{prinn}{0}\the\prsoltoks}

\begin{document}

\begin{center}\Large Problems\end{center}

\begin{pr}[2 points]
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\begin{pr}
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\end{document}


The environ package is very useful as it gathers the environment's contents and allows to do what's wanted with it, by storing it in the macro \BODY. Each time a pr environment appears, its contents is stored in the token register \prsoltoks, preceded and followed by \begin{prinn} and \end{prinn}; similarly for solution, but the problems are also printed out.

Notice how I got the automatic printing, by hooking to \AtEndDocument: now also the solutions will be printed.

Notice also a different definition for the solution environment (which is, internally solinn, but it's irrelevant from a user's point of view): whenever an environment puts text at its beginning, its "begin part" should end with \ignorespaces. For getting a vertical space, it's better to use \par\medskip or even

\par\addvspace{\medskipamount}


Use whatever skip you prefer, for instance \addvspace{\bigskipamount} or \addvspace{3cm}.

-
@user4011 See edit – egreg Dec 29 '11 at 15:15
I'm trying to use your code in this answer (using MiKTeX), but I keep getting the following error: No file [document name].aux. Apparently this error is thrown at line 51 with \end{pr}. Any idea why this might be happening by any chance? – Daniel W. Farlow Aug 6 '15 at 7:54
@DanielW.Farlow I don't think it's a problem with the code, but rather with file permissions. – egreg Aug 6 '15 at 8:33

During I developed a solution egreg answered :-( -- However I will show my approach.

\documentclass[a4paper, 11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %
\usepackage{nccmath}
\usepackage{amsfonts} %
\usepackage{enumerate}
\usepackage[amsmath]{ntheorem}%

\usepackage{storebox}
\newcounter{problemcount}
\newsavebox\problembox
\newsavebox\solutionbox
\newenvironment{pr}{%
\refstepcounter{problemcount}
\global\setbox\problembox\vbox\bgroup%
\trivlist\item\relax%
\subsubsection*{Problem~\theproblemcount}%
}{\endtrivlist\egroup%
\copy\problembox%
}
\newenvironment{solution}{%
\global\setbox\solutionbox\vbox\bgroup
\trivlist\item\relax%
\textit{Solution:}~\ignorespaces
}{\endtrivlist\egroup%
\clearpage
\box\problembox
\box\solutionbox
\clearpage
}

\begin{document}

\begin{pr}
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\begin{pr}
Problem
\end{pr}

\begin{solution}
Solution
\end{solution}

\end{document}

-
This will print two pages for each problem, though. – egreg Dec 29 '11 at 14:30
@egreg: I understand the question in this way. – Marco Daniel Dec 29 '11 at 14:56
@MarcoDaniel The egreg's solution is in principle what I want, however it seems to break mystyle, see comment above. – student Dec 29 '11 at 15:10