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.

2 Answers 2


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).


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.

       {\string\sc\space Exercise\string~\thequestion.}\quad}%

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.


Define a command to actually print the answers We close the write and then read the file that we wrote back in.


Macros for typesetting question These are less interesting and only appear here for completeness.

   {\sc Exercise \thequestion.}

With all these definitions made, we could then do something like this in the input file:

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?

\title{Birds \& Bees of North America}
\author{Dr.~H.T. Jones}
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 Output of the question environment at the point where the question and answer environments appear and Output of the answer environment 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.
    – Una
    Sep 19, 2020 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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