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

I'm writing a book and want to add some math problems and solutions to this book. Is it possible to have the following in LaTeX?

  1. In the LaTeX source file, the source of the solution will be directly after the problem.
  2. In the typeset book, the section with solutions will be typeset at the end.
  • The answer to "is it possible" is Yes. Hopefully someone else will come and explain how. [You can also look at the source of the TeXbook to see how Knuth does it in plain TeX, but it will be easier to wait. :-)] – ShreevatsaR Jul 18 '10 at 7:58
  • 2
    Have you ever heard of acrotex? Their education bundle is free and has lots of tools to design exercises. I am not sure it gives you exactly what you want, but it is definitely worth looking into. acrotex.net/aeb_index.php – Vivi Jun 6 '11 at 0:42

There's a package called answers which appears to do something like this (do texdoc answers, or the equivalent on your platform, for the documentation).

If that doesn't work, or isn't really suitable, then I've got a hand-rolled solution to exactly this problem which I could post here.

  • 2
    Always good to use a pre-existing package where possible. +1 – Will Robertson Jul 19 '10 at 8:27

You can put material at the end of the document (regardless of its location in the source) with \AtEndDocument{...}.

Incorporating that into your macro for writing questions/answers is left as an exercise :)

  • I think it's a rather hard exercise. Could you elaborate? What if you want to have an index (for instance) to appear after the solutions? – ShreevatsaR Jul 18 '10 at 22:33
  • Write \AtEndDocument{\PrintIndex} at the end of your document. – Will Robertson Jul 19 '10 at 6:39

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