6

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?

5
  • 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. Commented Nov 26, 2010 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
    Commented Nov 26, 2010 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. Commented Nov 26, 2010 at 14:05
  • Odd. I didn't leave that first comment. I guess voting to close does so automatically now?
    – TH.
    Commented Nov 26, 2010 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. Commented Nov 26, 2010 at 21:05

2 Answers 2

5

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

1

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.

1
  • 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? Commented Nov 26, 2010 at 14:34

Not the answer you're looking for? Browse other questions tagged .