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}