# Is there a package to pull out Answer Keys from Exercises to the last page? [duplicate]

I am writing a book containing exercises for the readers.

To ease writing phase, I will type each question followed by its answer. When the input file is compiled by latex.exe, I want all answers to be moved to the last page.

Is there a package to do that?

-

## marked as duplicate by clemens, Stefan Kottwitz♦Oct 19 '12 at 10:13

This sounds like the job for a macro, not a whole package. I can't write the details, but I can imagine a macro that aggregates its arguments and then another macro that regurgitates this aggregate. You use the first macro at each question, and the second one at the end. At a flip of a definition, the first macro becomes the identity (meaning it places in-situ whatever you give it, rather than aggregate) and the second macro has nothing to regurgitate. –  Yossi Farjoun Nov 26 '10 at 12:40
The thmtools package can make lists of theorems and so on. I imagine a similar sort of thing could work here... –  Seamus Nov 26 '10 at 12:49
@TH.: I thought I'd seen something like this before. I agree that it's a duplicate. XPort: please take a look at the question that TH. links to and see if it answers your question. If it does, we'll close this one as a duplicate. If it doesn't, please explain why not so that it's clearer what you are looking for. Thanks. –  Loop Space Nov 26 '10 at 14:05
Odd. I didn't leave that first comment. I guess voting to close does so automatically now? –  TH. Nov 26 '10 at 14:10
@TH.: Yes, it seems to—I noticed the same thing on Stack Overflow the other day. It's annoying, because it's improperly capitalized/punctuated. –  Antal S-Z Nov 26 '10 at 21:05

Yes, the package "answers" does exactly this, and works well.

-

I tried putting my keyboard where my mouth was and failed...here's what I manged:

\edef\myanswers{\relax}


It works, but not when I put \begin{...}...\end{...} environments into it...I've set the answer CW...perhaps someone knows how to make it work so that you can put environments into that...or perhaps it isn't as simple as I thought.

Here's something that passes the \begin{...} ... \end{...} test:

\documentclass{article}

\makeatletter
\newcommand{\mysow}[1]{%
\@ifundefined{myreap}{%
\def\myreap{#1}%
}{%
}}

\makeatother

\begin{document}
You \mysow{reap} what \mysow{you} sow.

\mysow{%
\begin{enumerate}
\item You reap what you sow.
\end{enumerate}
}

\myreap
\end{document}


But there may well be more cases where it fails.

-
Thanks @Andrew. That works for the use-case I tried...now we can see what the OP thinks...BTW, is the \g@addto@macro just a global version of what I tried to do? –  Yossi Farjoun Nov 26 '10 at 14:34