# Arranging Questions

I have a set of question papers of Mathematics to be written in TeX grouping them according to their topics such as Abstract algebra, Real analysis, Differential Equations, etc. Can the following be achieved?

Type the questions appearing in question papers in the order of questions there, and add some label to each question according to its topic. In the output, the questions from all the question papers on the same topic are arranged together.

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
\catcode\^^M\active
\def\verbatim@processline{%
\immediate\write\ans@out
{\the\verbatim@line}}%
\verbatim@start}
\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{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.

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.
biblatex plus oscola does what you want, but the learning curve is tall. Each question would be defined as a biblatex item, with a keyword field to define the topic class of the question. oscola has the tools to construct a separate table for each topic.
Or, if it is a one time task, I would likely do this: put all the questions in a plain text file, one per line; prefix each question with \item \vphantom{\hphantom{keyword}} where keyword is replaced by a keyword for each topic; grep the file to send all questions of a certain topic into their own file, then include those output files in the master LaTeX document.
• For the grep solution, simpler would be to put the bare keyword before the LaTeX code, ie geom \item What is the circumference of ...`, then strip out the keyword.