I happily use the exam document class to create exams and quizzes. I often make at least part of my exams multiple choice, and I'd like to be able to

  1. generate multiple versions of the exam by permuting the answer choices (automatically, rather than by hand as I do now), and
  2. generate an answer key for each version when compiling the exam with answers on.

Do any of you do that or have suggestions about how I might be able to implement that?

  • Welcome to tex.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting.
    – Thorsten
    May 11, 2011 at 14:25

6 Answers 6


I know this problem well. I have my own package that does it, along with several documents full of crufty code. I can tell you my implementation:

Permuting answer choices:

  • Use a pgfmath list to store the answer choices
  • Do a Knuth shuffle on that list to permute them
  • Walk over the list printing out each choice

You might find the \pgfmathsetseed command useful because you want randomized choices but the same randomized choices each time you compile! You might also find http://random.org/ good for getting sufficiently random seeds.

For generating multiple versions and answer keys I use docstrip. My main file is a docstrip file like exam.dtx and I use guards like

%<A>\pgfmathsetseed{1001} % seed for A version
%<B>\pgfmathsetseed{1010} % seed for B version
    % commands that alter whether the key is shown

to \generate files like exam-A.tex, exam-A-key.tex, etc. Then you have one document per file without manually repeating code.

I don't use the exam class but I think this could work in that.


In ConTeXt, the itemize environment (which is equivalent to LaTeX's itemize and enumerate) supports a random key. So, to get random permutation of options use. But, you have to use \startitem ... \stopitem instead of \item to capture the items.


    \startitem First item \stopitem
    \startitem Second item \stopitem
    \startitem Third item \stopitem


I haven't checked how good the randomizing algorithm is. You could, of course, implement this in LuaTeX very easily. (The ConTeXt solution predates lua, and is implemented in eTeX)

  • Thanks Aditya - very intesting - I'm not at all familiar with ConTeXt, but it's very cool that itemize has a random key. Now I'm curious to look into it.
    – Jason
    May 12, 2011 at 16:44

Although this is a relatively old question, a possible answer, without the use of any packages or classes, is described here. It relies on Python and a little bash scripting to produce the different versions. Questions and their possible answers are automatically shuffled in each version, and the key is printed at the end of the produced tex files (after the \end{document}).

  • +1 as this was how I had planned to solve this problem myself...before I found your codes! Thanks -- they saved me a lot of time. Mar 17, 2015 at 21:47
  • The code is not the prettiest (I prefer LaTeX and less dense python), but the results are really nicely formatted. For real exams, I added an option to set the random seed so I can replicate the process if needed.
    – dirkjot
    Sep 23, 2015 at 5:18
  • A realistic solution easy to use! Apr 26, 2019 at 10:00

You can also use the automultiplechoice package from AMC software, which also allows to get the marks from the completed answer sheets scans.


The package exam-randomizechoices is designed for that. From the documentation:

This package is an extension to the exam document class. It provides the user with four new multiple choice typesetting environments which place their content in a random order. It can (only) be used in combination with the exam class. It can only randomize the placement of choices in multiple choice questions. The questions themselves cannot be randomized with this package.

Furthermore, the package provides a simple answer key table typesetter and has a command for writing the answer keys to an external file.


You can try package dethi.sty in Vietnamese at https://nhdien.wordpress.com/2016/11/05/dethi-sty-3-0-goi-lenh-lam-de-thi-va-bai-tap/

You must log in to answer this question.

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