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.

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}%
  {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont%
          ##1\ ##2\theorem@separator}\hbox{\strut}}}]}%}
  {\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\theorem@separator]%
    \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}
share|improve this question
    
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

2 Answers 2

up vote 7 down vote accepted

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}%
  {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont%
          ##1\ ##2\theorem@separator}\hbox{\strut}}}]}%}
  {\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\theorem@separator]%
    \hfill{\normalfont(##3)}\newline}
  \theorembodyfont{\normalfont}
  \theoremindent0cm
\makeatother
\theoremstyle{mystyle}

\newtheorem{prinn}{Problem}
\newenvironment{solinn}
  {\noindent\small Solution: \ignorespaces}{\par\addvspace{\medskipamount}}

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

share|improve this answer
    
@user4011 See edit –  egreg Dec 29 '11 at 15:15

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

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.