Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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 S-Z Nov 26 '10 at 21:05
show 1 more comment

marked as duplicate by cgnieder, Stefan Kottwitz Oct 19 '12 at 10:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 5 down vote accepted

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

share|improve this answer
add comment

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

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

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}%
    }{%
  \g@addto@macro{\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.

share|improve this answer
    
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
add comment

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