There might be a better way to handle this, but I did something similar with exercise answers in my LaTeX book by building on the extensions available in the verbatim
package that allowed me to put the questions and answers together even though the answers would be printed in a separate section at the end of each chapter.
This is the code that I used (this was all in the .cls
file so it uses @
as a letter). I'll annotate the listing to make things clearer
First create a new write (you'll need one for each category, but remember that there are only 16 write streams available and LaTeX uses some of those on its own).
\newwrite\ans@out
\immediate\openout\ans@out=\jobname.ans
Define the answer environment This is based on the verbatimwrite
example environment from the verbatim
package documentation. The lines beginning with \immediate\write
are used to add additional text beyond what's in the input file.
\def\answer{%
\@bsphack
\let\do\@makeother\dospecials
\immediate\write\ans@out{\string\preans}
\immediate\write\ans@out{\string\par\string\noindent
{\string\sc\space Exercise\string~\thequestion.}\quad}%
\immediate\write\ans@out{\string\vadjust{\string\nobreak}\relax}
\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\ans@out
{\the\verbatim@line}}%
\verbatim@start}
\def\endanswer{%
\immediate\write\ans@out{}
\@esphack}
This is one of the macros I used for formatting answers. I'm actually a little embarrassed about how ugly my intermediately generated output is marked up.
\def\preans{\if@nobreak\global\@nobreakfalse\else\bigfilbreak\fi}
Define a command to actually print the answers We close the write and then read the file that we wrote back in.
\def\printanswers{\immediate\closeout\ans@out
\@input{\jobname.ans}
}
Macros for typesetting question These are less interesting and only appear here for completeness.
\newcounter{question}[chapter]
\def\thequestion{\thechapter-\arabic{question}}
\def\question{\par\refstepcounter{question}\noindent
{\sc Exercise \thequestion.}
\ignorespaces}
\def\endquestion{\par}
With all these definitions made, we could then do something like this in the input file:
\begin{question}
What \LaTeX\ commands would you type at the beginning of the input file for
an article which had the title ``Birds \& Bees of North
America,'' was written by ``Dr.~H.T. Jones'' and had today's date
printed for the date field?
\end{question}
\begin{answer}
\begin{verbatim}
\documentclass{article}
\title{Birds \& Bees of North America}
\author{Dr.~H.T. Jones}
\begin{document}
\maketitle
...
\end{verbatim}
Note that \& is input as \verb+\&+ and a \verb+~+ is used after
``Dr.''\ to prevent an end-of-sentence space from being printed
there (a \verb*+\ + could have been used as well). Today's date
was supplied by omitting the date field.
\end{answer}
Which produces
at the point where the question
and answer
environments appear and
where the \printanswers
command appears.
In your case, you'll need to emulate the answers
environment for each of your categories and then have a \printallofit
command or somesuch that closes and re-reads all the generated input files.