Friday, December 28, 2012

Quizzes in $\rm{\LaTeX}$: Shuffling questions and answers

A problem when one wants to write a quiz with multiple-choice answers in $\rm{\LaTeX}$ is how to shuffle questions and their possible answers. One typically wants to produce different versions of the quiz, with the questions and their possible answers appearing in different random order from version to version. This is easy to do with some placeholders in the tex file and with the use of Python and a little bash scripting. Although there is probably an easier way to achieve what I describe below, this is the way I came up with and I like it!

First one has to take some care in preparing the quiz. As always, the tex file will be split in three parts:
  1. The preamble,
  2. The main body (which will include the problems),
  3. The ending.
In the preamble one typically starts with $\verb1\documentclass{}1$ and ends with $\verb1\begin{document}1$. Here, we enclose all that, and everything we want to print in the top of every version, in between a $\verb1%BeginPreamble1 \ldots \verb1%EndPreamble1$. In the main body we enclose each problem in a $\verb1%BeginProblem1 \ldots \verb1%EndProblem1$, and we follow the formatting in the example tex file you can download by following the link at the end of this post. Finally, we include the ending enclosed in $\verb1%BeginEnding1 \ldots \verb1%EndEnding1$.

Now, in order to produce the different versions, I wrote a bash script and a Python program. The script calls the Python program that does all the shuffling, and then it compiles the produced tex files (i.e. the different versions) and cleans up. The result is four versions of tex and the corresponding pdf files. Of course one can produce more or fewer versions by modifying the scripts. It is important that all the formatting of the example file is followed, otherwise the scripts will not run properly. By the way you will need Python 3 to run my scripts. (Python 2 will do too, but the bash script "shuffle" has to be modified by deleting the "3" from "python3".)

At the end of the each of the tex files, after the $\verb1\end{document}1$, the key is printed for convenience. Two pdf's are produced for each version: one with the correct answers indicated with a red checkmark, and one with no correct answers indicated. By the way, the correct answer is identified by typing $\verb1\answer1$ at the end of an item (see example file). I should also point out here that if you want a specific possible answer to not be included in the shuffling, you can make that happen with the flag $\verb1%dontshuffle1$. This is useful, for example, when one of the possible answers is "None of the above". Again, the example file in the link below includes all the relevant formatting that has to be followed.

So all one has to do after downloading the zip file below is unzip it, and run the shuffle script by opening a terminal and typing $\verb1./shuffle1$. It is important that only one tex file is in the folder of the files "shuffle" and "shuffle.py", but its name can be anything (it is read automatically by the script).

Download example by clicking here!