# How can I lay out multiple choice answers as a matrix with exam?

For creating a test I use the exam class, which worked out fine so far. Now I need to implement a question with the possible answers beeing a matrix like this:

However I can't manage to put the \choice command (of the exam class) in that form (I want to use exam for printing automatic solution papers also). When I simply put it inside a tabular it does not seem to work out. How could I realize this?

PS: The example above (just as a visual explanation, it is not using the exam class) is:

\documentclass{exam}
\usepackage{amssymb}
\begin{document}
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{r|ccc}
& Orange & Green & Aubergine \\
\hline
Fruit & $\Box$ & $\Box$ & $\Box$ \\
Vegetable & $\Box$ & $\Box$ & $\Box$ \\
Colorname & $\Box$ & $\Box$ & $\Box$ \\
\end{tabular}\
\end{document}


[Edit: add example code] [Edit2: Clarify question]

• Please add a minimal working example (MWE) – user11232 Apr 6 '15 at 9:04
• I would have, but I didn't have the slightest idea, what kind of example would be helpful for that kind of question... – hoeni Apr 6 '15 at 9:08
• @HarishKumar Do you think the source of the above would be helpful? – hoeni Apr 6 '15 at 9:16
• Certainly. It will save us some typing (atleast). – user11232 Apr 6 '15 at 9:47

The exam documentation states:

The checkboxes environment creates a list environment with the choices as the items in the list.

So every \begin{checkboxes}...\end{checkboxes} creates a new list. You can't put different items from the same list in different cells in a table. So you'll need to create a new one-item list for every cell in your table. And you can't put a list in a tabular without having a predefined column width. So either use alignment options (p, m, b, ...) that give you a fixed column width or put the list in a minipage-like environment such as varwidth (which will also horizontally center its content).

As our lists are one-item lists and we don't really want them to look like a list, we'll need some customization. In the exam class this is done width \checkboxeshook. So go for:

\renewcommand{\checkboxeshook}{
\setlength{\labelwidth}{0pt}
\setlength{\labelsep}{0pt}
\setlength{\leftmargin}{0pt}
}


Now make a macro so you don't need to copy paste too much code. This will also increase the readability of your code. Here are 2 macros. One for a regular checkbox option and one for the correct choice checkbox:

\newcommand{\tablechoice}{\begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}}
\newcommand{\tablecorrectchoice}{\begin{varwidth}{\textwidth}\begin{checkboxes}\CorrectChoice\ \end{checkboxes}\end{varwidth}}


All these things put together get you:

\documentclass[answers]{exam}
\usepackage{amssymb}
\usepackage{varwidth}

\renewcommand{\arraystretch}{1.4}

\newcommand{\tablechoice}{\begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}}
\newcommand{\tablecorrectchoice}{\begin{varwidth}{\textwidth}\begin{checkboxes}\CorrectChoice\ \end{checkboxes}\end{varwidth}}

\checkboxchar{$\Box$}
\checkedchar{$\blacksquare$}

\renewcommand{\checkboxeshook}{
\setlength{\labelwidth}{0pt}
\setlength{\labelsep}{0pt}
\setlength{\leftmargin}{0pt}
}

\begin{document}

\begin{questions}

\question Answer the following question: \\
\begin{tabular}{r|*3{c}}
& Orange & Green & Aubergine \\ \hline
Fruit & \tablechoice & \tablechoice & \tablechoice \\
Vegetable & \tablechoice & \tablecorrectchoice & \tablechoice \\
Colorname & \tablechoice & \tablechoice & \tablechoice \\
\end{tabular}

\end{questions}

\end{document}


• Thanks for your explanation, that looks exactly like solving my problems. Need some time now to play around and understand ;-) – hoeni Apr 6 '15 at 18:58
• @Marteen: Will do soon, there were issues not letting me find time for my latex stuff these days. Sorry... – hoeni Apr 19 '15 at 10:04
• Really, really sorry for my huge delay... I start to get the point of of what you're doing. However, I can't build your full code (TeXLive-2014): test2.tex:22 Package array Error: Illegal pream-token (R): c' used \begin{tabular}{R{1.7cm}|*3{C{2cm}}} test2.tex:22 Package array Error: Illegal pream-token (1.7cm): c' used \begin{tabular}{R{1.7cm}|*3{C{2cm}}} – hoeni Apr 23 '15 at 18:26
• @hoeni Don't worry about delays :) The reason why you couldn't compile was because there was a typo in my code (a remnant from a previous edit). Anyway, it's fixed now. I also tried to give a better explanation as to why I do what I do. And I also added code for the correct choice checkbox to use in case you want to print solutions. – Maarten Dhondt Apr 23 '15 at 21:49
• Works like a charm! Thanks for your helpful explanation! – hoeni Apr 25 '15 at 17:48

Based on Maarten Dhondt's helpful answer I built myself an environment with the number of horizontal options to avoid repeating myself for other questions:

\documentclass[answers]{exam}
\usepackage{amssymb}
\usepackage{varwidth}

\newenvironment {choicetable} [1] {
\renewcommand{\arraystretch}{1.7}
\renewcommand{\checkboxeshook}{
\setlength{\labelwidth}{0pt}
\setlength{\labelsep}{0pt}
\setlength{\leftmargin}{0pt}
}
\newcommand{\CO}{\begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}}
\newcommand{\CX}{\begin{varwidth}{\textwidth}\begin{checkboxes}\CorrectChoice\ \end{checkboxes}\end{varwidth}}
\begin{tabular}{r*{#1}{c}}
} {
\end{tabular}
}
\checkboxchar{$\Box$}
\checkedchar{$\blacksquare$}

\begin{document}
\begin{questions}
\question Answer the following question: \\
\begin{choicetable}{3}
& Orange & Green & Aubergine \\ \hline
Fruit & \CO & \CO & \CO \\
Vegetable & \CO & \CX & \CO \\
Colorname & \CO & \CO & \CO \\
\end{choicetable}
\end{questions}
\end{document}


Something like this?

\documentclass[11p,twoside,a4paper,leqno]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage[x11names]{xcolor}
\usepackage{array, makecell, eqparbox}
\newcommand\tsq{$\square$}
\setcellgapes{8pt}

\begin{document}
\begin{center}
\makegapedcells
\begin{tabular}{@{}r! {\vrule width 0.8pt}*{3}{c}}%{>{\centering $}m{2cm}<{$}}

• I didn't understand you want to use the \choice command. There was no code when I saw the question, and I didn't check before posting. – Bernard Apr 6 '15 at 11:31