Adding text to a different part of the document

I'm writing a LaTeX document at the moment which has problems (which are created in a {theorem} environment) for the reader in it. I'd like to put solutions to those problems in the end of the document. Of course, I could just add another section/chapter called “solutions” in the end and write them there. But, since the problems are very likely to be reordered and new problems will be added I would have to change the order of the solutions manually which could be quite a hassle. I was wondering whether it is possible to write LaTeX code in some part of the document which then appears in a different part of the final document. Ideally I would like to write something like

\begin{problem} \label{someProblem}
Here is some problem.
\end{problem}
Solution to problem \ref{someProblem}
}


which typesets the problem right in place and appends the text in the parentheses to the section in the back.

When I read the question, I first thought of an external file, which is somehow a standard solution. But, with the macro design, you proposed, this is not necessary. A possible (and possibly the most simple but not the fastest -- see the answer of @egreg) implementation with a small test could like like:

\documentclass{book}
\usepackage{amsmath,amsthm}
\newtheorem{problem}{Problem}[chapter]

\makeatletter
\def\solutionsname{Solutions}
\let\@sol=\@empty
\newcommand{\printsolutions}[1][chapter]{\@nameuse{#1}{\solutionsname}\@sol}
\protected@xdef\@sol{%
\@sol {\noindent\bfseries\theproblem}\space#1\par\vskip 1em plus 4pt minus 4pt\par
}%
}
\makeatother

\begin{document}
\chapter{Groups}

\begin{problem}
Let $(G,\circ)$ be a finite group and $g\in G$. Prove that the order of $g$ divides the cardinality of $G$.
Because $G$ is finite there is $\operatorname{ord}_G(g)=:m<\infty$. Hence, there exists a cyclic subgroup $U_g=\lbrace e,g,g^2,\ldots,g^{m-1}\rbrace$. Obviously $\lvert U_g\rvert=m=\operatorname{ord}_G(g)$ holds. Therefore the claim follows from Lagrange theorem.
}
\end{problem}

\begin{problem}
Let $(G,\circ)$ be a group. Show that $m\circ G=G\quad\forall m\in G$.
The inclusion $m\circ G\subseteq G$ is evident due to the closure of $\circ$ on $G$. Let $m,n\in G$. By calculating
$n=e\circ n=(m\circ m^{-1})\circ n=m\circ(m^{-1}\circ n)\in m\in G,$
we obtain $G\subseteq m\circ G$.
}
\end{problem}

\appendix
\printsolutions
\end{document}


The synopsis is exactly as described in the OP: (1) \addtosolutions gathers the solution inside the problem environment and (2) the solutions will be printed with the \printsolutions macro at the point you want them to be. (\printsolutions can be called with an optional argument that sets the sectioning level in terms of the sectioning command; default: 'chapter'.)

Note that the \appendix call in my example can be dropped. I just find it well suited for this situation.

• Why defining \csname sol\thesol\endcsname when you expand it in the next stage? Add \protected before \expandafter, to store in \@sol only the control sequence and not its expansion. I wouldn't use \vspace* because it won't disappear at a page break, which is instead wanted. – egreg Oct 27 '13 at 21:33
• @egreg: This must come from a previous implementation I designed with a slightly different aim. What the code does now, can be acomplished way easier. I will deal with it tomorrow. (At least it is still a working solution.) The \vspace* of course should be \vspace... – Ruben Oct 27 '13 at 22:11
• Thanks @egreg! Note that by tomorrow I meant 649 days... ;) – Ruben Aug 7 '15 at 13:49

This is a variation on Ruben's solution that uses a token register instead of relying on \protected@xdef (it should be a bit faster). The main difference is that no expansion is done on the argument to \solution.

\documentclass{book}
\usepackage{amsmath,amsthm}

\theoremstyle{definition}
\newtheorem{problem}{Problem}[chapter]
\providecommand\solutionsname{Solutions}

\makeatletter
\newtoks\@soltoks
\newcommand\printsolutions[1][\solutionsname]{%
\chapter{#1}
\the\@soltoks}
\newcommand\solution[1]{%
\edef\@soltemp{%
\the\@soltoks % the previously stored solutions
\noexpand\solutionpreamble{\theproblem}% we only want \theproblem to be expanded
\unexpanded{#1}%
}
\global\@soltoks=\expandafter{\@soltemp}
}
\newcommand{\solutionpreamble}[1]{%
\par\medskip\noindent\textbf{#1}\enspace\ignorespaces
}
\makeatother

%A small test
\begin{document}
\chapter{Groups}

\begin{problem}
Let $(G,\circ)$ be a finite group and $g\in G$. Prove that the order
of $g$ divides the cardinality of $G$.

\solution{Because $G$ is finite there is $\operatorname{ord}_G(g)=:m<\infty$.
Hence, there exists a cyclic subgroup $U_g=\lbrace e,g,g^2,\ldots,g^{m-1}\rbrace$.
Obviously $\lvert U_g\rvert=m=\operatorname{ord}_G(g)$ holds. Therefore the
claim follows from Lagrange theorem.}
\end{problem}

\begin{problem}
Let $(G,\circ)$ be a group. Show that $m\circ G=G\quad\forall m\in G$.
\solution{The inclusion $m\circ G\subseteq G$ is evident due to the
closure of $\circ$ on $G$. Let $m,n\in G$. By calculating
$n=e\circ n=(m\circ m^{-1})\circ n=m\circ(m^{-1}\circ n)\in m\in G,$
we obtain $G\subseteq m\circ G$.}
\end{problem}

\printsolutions
\end{document}


One can type \printsolutions[Solutions of selected exercises] instead of modifying the standard \solutionsname. The formatting of the solutions can be defined in the macro \solutionpreamble that takes as argument the problem number.