1

I have a question on how to make multiple choice questions and then generate a table with correct answers for grading. For example:

1) What's the result of 2X4:

A. 8

B. 6

C. 4

D. None of the above

2) What's the result of 2/4:

A. 2

B. 4

C. 6

D. 0.5

Then when I compile this exam, I get a table on a separate sheet with the correct answers like this:

enter image description here

Please help me as I am new with Latex.

Thanks in advance.

  • 1
    There are specific document classes for that like exam. However, until you make your question more concrete by adding a concrete LaTeX document you may not necessarily get a more specific answer. – user121799 May 17 at 0:14
1

The code below will let you write your questions as:

\begin{questions}
  \item What's the result of $2\times 4$:
    \begin{choices}
      \item* 8
      \item 6
      \item 4
      \item None of the above
    \end{choices}
  \item What's the result of $\tfrac{2}{4}$:
    \begin{choices}
      \item 2
      \item 4
      \item 6
      \item* 0.5
    \end{choices}
\end{questions}

Use \item* for the correct choices - you can, if you want, have more than one correct choice for each question. The output is:

enter image description here

The "answer table" is as requested, although I probably would not do it this way. The different questions can have a different number of choices but the answer table will be missing a few cells in that case.

Here's the full code:

\documentclass{article}
\usepackage{amsmath}

\usepackage{xparse}
\ExplSyntaxOn
% save \item so we can override it in the choices environment
\let\real_item\item
\int_new:N \g_question_int
\int_new:N \g_max_choices_int
\NewDocumentCommand\start_choices{}{
  \let\item\choices_item
  \seq_new:c {g_choices\arabic{questionsi}_seq}
}
\NewDocumentCommand\choices_item{s}{
  \real_item
  \IfBooleanTF{#1}
    { \seq_gput_right:cx {g_choices\arabic{questionsi}_seq} {*\arabic{choicesi}} }
    { \seq_gput_right:cx {g_choices\arabic{questionsi}_seq} { \arabic{choicesi}} }
  % now update the maximum number of columns
  \int_compare:xNVT {\arabic{choicesi}} > \g_max_choices_int {
       \int_gset:Nn \g_max_choices_int {\arabic{choicesi}}
  }
}
\NewDocumentCommand\print_answer{ sm } {
  \IfBooleanTF{#1}{\textbf{\int_to_Alph:n {#2}}}{\int_to_Alph:n {#2}}
}
\NewDocumentCommand\print_answers{}{
  \int_compare:VNnT \g_max_choices_int > {0} {
    \newpage
    \int_set:Nn \g_question_int {0}
    \begin{tabular}{|c|*{\int_use:N \g_max_choices_int}{c|}}\hline
    \int_while_do:nn { \g_question_int < \arabic{questionsi} } {
      \int_gadd:Nn \g_question_int {1}
      \int_use:N \g_question_int.
      \seq_map_inline:cn {g_choices \int_use:N \g_question_int _seq} {&\print_answer##1}
      \\\hline
    }
    \end{tabular}
  }
}
\cs_generate_variant:Nn \int_compare:nNnT {xNVT}
\cs_generate_variant:Nn \int_compare:nNnT {VNnT}
% define enumitem environments for questions and choices
\usepackage{enumitem}
\newlist{questions}{enumerate}{1}
\setlist[questions]{
   label=\bfseries\arabic*.,
   after=\print_answers
}
\newlist{choices}{enumerate}{1}
\setlist[choices]{
   label=\Alph*).,
   before=\start_choices,
}
\ExplSyntaxOff

\begin{document}

\begin{questions}
  \item What's the result of $2\times 4$:
    \begin{choices}
      \item* 8
      \item 6
      \item 4
      \item None of the above
    \end{choices}
  \item What's the result of $\tfrac{2}{4}$:
    \begin{choices}
      \item 2
      \item 4
      \item 6
      \item* 0.5
    \end{choices}
\end{questions}

\end{document}

If you plan on using this then I'd recommend putting the code from the preamble in a style file, say choices.sty, and then loading it with \usepackage{choices}.

0

You can also have a look to the esami package, which produces the string of correct choices for multiple choice questions (although they are not contained in a table as yours) or at the acrotex package by D.P. Story, on which the previous one is based. acrotex produces the table of correct choices as you want it.

Edit: the esami package has a structure wich does not allow a MWE to be posted here (two files are needed, at least: the master file and the exercises file). You can have a look to the example files which are included in the doc folder of the package.

  • 1
    Could you extend your answer to provide a small example document showing how this could be done using one of those packages? – siracusa May 17 at 23:05
  • In addition to siracusa's comment, this is just to inform you that please do not retag the question to fit the answer. esami maybe a solution to the problem, but the problem is not an esami problem. – user156344 May 18 at 8:00

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.