How to defer content to a later part of the document?

I want to create a collection of problems, each problem consisting of the problem itself, optional hints and optional solutions. I wanted to know what would be the best way to do it ?

I could use some help defining an interface that let's me lay down the problems at one place, and print the according hints in different parts (or chapters) for hints and solutions, respectively.

I have been hinted, that package exsheets could be halfway to the solution.

\documentclass{report}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{blindtext}

\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\newcounter{problem}
\newenvironment{problem}[1]{
{\bfseries\large{#1}\par}
}{}
\begin{document}
\part{Problems}
\section{Puzzle 1}
\begin{problem}{Pythagoras}
\blindtext
%\begin{hints}
%   \blindtext
%\end{hints}
%\begin{solution}
%   \blindtext
%\end{solution}
\end{problem}
\section{Puzzle 2}

\part{Hints}
\section{Puzzle 1 hint}
\section{Puzzle 2 hint}

\part{Solutions}
\section{Puzzle 1 solution}
\section{Puzzle 2 solution}

\end{document}


I also asked this question at LaTeX community.

• @ethanbolker It is not really replicating content, it is about inputting it in one location and outputting it at another. – Johannes_B Jan 24 '15 at 20:15
• @Johannes_B True, but solutions to questions tagged content-replication may in fact suggest solutions here. – Ethan Bolker Jan 24 '15 at 20:17
• @EthanBolker Ok, fair enough. :-) Did you get a ping, or did you just see your name? – Johannes_B Jan 24 '15 at 20:18

Like you say in your question: this is doable with exsheets:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{exsheets}

% new property hint':
\DeclareQuestionProperty{hint}

% a command to print all given hints
\NewDocumentCommand\printquestionhints{}{%
\ForEachQuestion
{%
\IfQuestionPropertyT{hint}{##1}{%
{\QuestionNumber{##1}}% number
{0}                   % points -- need to be zero here
{0}                   % bonus points -- need to be zero here
{##1}%                % ID
\GetQuestionProperty{hint}{##1}%
\par
\medskip
}%
}%
}

\begin{document}

\section{Problems}
\begin{question}[subtitle=Pythagoras]
This is the first problem.
\SetQuestionProperties{ hint = This is a hint to the first problem. }
\end{question}
\begin{solution}
This is the solution to the first problem.
\end{solution}

\begin{question}[subtitle=Another Problem]
This is the second problem.
\end{question}
\begin{solution}
This is the solution to the second problem.
\end{solution}

\begin{question}[subtitle=Yet Another Problem]
This is the third problem.
\SetQuestionProperties{ hint = This is a hint to the third problem. }
\end{question}
\begin{solution}
This is the solution to the third problem.
\end{solution}

\section{Hints}
\printquestionhints

\section{Solutions}
\printsolutions

\end{document}


Customizations are possible (font shape/size of the headings, ...)

• Very nice :-) It looks much easier than i thought. – Johannes_B Jan 23 '15 at 13:12
• Yeah, thanks a lot cgnieder and Johannes..:) – CutestPenguin Jan 23 '15 at 16:41

Store the hints and solutions in a storage bin.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{environ}

\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\newcounter{problem}
\newenvironment{problem}[1]
{\refstepcounter{problem}%
{\textbf{Problem \theproblem\ (#1)}\par}\nopagebreak\noindent\ignorespaces}
{}

\makeatletter
\newtoks\late@hintstoks
\newtoks\late@solutiontoks

\NewEnviron{hints}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@hintstoks % the previous contents
\noexpand\late@hint{\theproblem}{\the\toks@}%
}%
\global\late@hintstoks=\expandafter{\@tempa}%
}
\NewEnviron{solution}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@solutiontoks % the previous contents
\noexpand\late@solution{\theproblem}{\the\toks@}%
}%
\global\late@solutiontoks=\expandafter{\@tempa}%
}
\newcommand{\late@hint}[2]{%
\paragraph*{Hints for problem #1}#2%
}
\newcommand{\late@solution}[2]{%
\subsubsection*{Solution for problem #1}#2%
}

\newcommand{\printhints}{%
\part{Hints}
\the\late@hintstoks
}
\newcommand{\printsolutions}{%
\part{Solutions}
\the\late@solutiontoks
}
\makeatother

\begin{document}

\part{Problems}
\section{Puzzle 1}
\begin{problem}{Pythagoras}
This problem is about Pythagoras' theorem
\begin{hints}
These are the hints about Pythagoras.
\end{hints}
\begin{solution}
Pythagoras' theorem is easy!
\end{solution}
\end{problem}
\section{Puzzle 2}
\begin{problem}{Riemann}
Prove the zeros of the $\zeta$ functions are on the critical line.
\begin{hints}
Try first with a few cases.
\end{hints}
\begin{solution}
Oh, well!
\end{solution}
\end{problem}

\printhints

\printsolutions

\end{document}


• You can surely see a tick mark symbol next to each answer, just below the voting marks; clicking on one of them marks the answer as accepted. – egreg Jan 23 '15 at 16:57

tcolorbox is not devoted to create problems+solution collections but can be used for it.

Next code is almost copied from tcolorbox documentation. It shows an example using savelowerto and recording tools: record, \tcbstartrecording, \tcbstoprecording, \tcbinputrecords.

savelowerto saves lower box part for further processing. In this case all solutions are declared in lower parts which are recorded and later on readed with \tcbinputrecords.

\documentclass{article}

\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\NewTColorBox[auto counter,number within=section]{exercise}{+O{}}{ %
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~\thetcbcounter:},
label={exercise@\thetcbcounter},
after upper={\par\hfill\textcolor{green!40!black} %
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}},
#1
}
\NewTotalTColorBox{\solution}{mm}{ %
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={solution@#1},
attach title to upper=\par,
}{\input{#2}}
\tcbset{no solution/.style={no recording,after upper=}}

\begin{document}
\section{Some problems}

\tcbstartrecording

\lipsum[1]
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f’(x) &= \left( \sin((\sin x)^2) \right)’
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
100\end{exercise}

\lipsum[2]
\begin{exercise}[no solution]
It holds:
\begin{equation*}
\frac{d}{dx}\left(\ln|x|\right) = \frac{1}{x}.
\end{equation*}
\end{exercise}

\lipsum[3]
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=(\sin(\sin x))^2
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f’(x) &= \left( (\sin(\sin x))^2 \right)’
=2\sin(\sin x)\cos(\sin x)\cos x.
\end{align*}
\end{exercise}

\tcbstoprecording

\section{Some solutions to previous problems}

\tcbinputrecords

\end{document}


• To be quite honest, you could have tortured me, i would have never even thought of tcolorbox` to solve the above problem. Thanks for this solution. :-) – Johannes_B Jan 23 '15 at 13:22