Possible Duplicate:
How can I produce exercises in one part of a LaTeX document with selected answers in another?

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?

  • 2
    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. – Andrew Stacey 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
  • 1
    @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 Spector-Zabusky 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:

\def\mysow#1{\edef\myanswers{\myanswers\ #1} }

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:


  \g@addto@macro{\myreap}{,\ #1}%


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

\item You reap what you sow.


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.