2

For modelling a list of questions with yes/no answers I wanted to define an environment where I can setup a question with a command: \clO for an option that's not applicable and \clX for those that are applicable.

Based on the solution in How can I lay out multiple choice answers as a matrix with exam? I wanted to use tabularx instead of tabular, as the right column has to be multiline and fill up all the space.

Everything works as planned until I want to finally set one element as \CorrectChoice in the \clO or \clX command, resulting in a weird error message: mwe.tex:41 Something's wrong--perhaps a missing \item. \end{choicelist}

MWE:

MWE

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

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

\newcommand{\clO}[1]{
    \begin{varwidth}{\textwidth}\begin{checkboxes}\CorrectChoice\end{checkboxes}\end{varwidth}
    & \begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}
    & #1\\
}
\newcommand{\clX}[1]{
    \begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}
    & \begin{varwidth}{\textwidth}\begin{checkboxes}\choice\end{checkboxes}\end{varwidth}
    & #1\\
}
\newenvironment{choicelist}{
    \renewcommand{\arraystretch}{1.4}
    \renewcommand{\checkboxeshook}{
    \setlength{\labelwidth}{0pt}
    \setlength{\labelsep}{0pt}
    \setlength{\leftmargin}{0pt}
    }
  \tabularx{\textwidth}{ccX}
    applicable & not applicable & \\
}{
    \endtabularx
}

\begin{document}
\begin{questions}
    \question
    Lorem ipsum dolor sit amet?

    \begin{choicelist}
        \clO{This is a multiline description to be answered, so we have to use a long text to ensure we can see a line break in the example.}
        \clX{foo}
        \clO{bar}
    \end{choicelist}
\end{questions}
\end{document}
5
  • It's not a problem of tabularx: if I change tabularx to tabular, the problem is the same and is due to the fact that a group ends up straddling two alignment cells, which is not allowed.
    – egreg
    May 25, 2015 at 15:47
  • What is meant by 'group' here? The command? or the start & end of the environment? 'alignment cells' are the table columns? Why does /CorrectChoice trigger that problem and /choice does not? How could I workaround this?
    – hoeni
    May 25, 2015 at 18:53
  • The command \CorrectChoice issues a \begingroup command, whose matching \endgroup` occurs much later, at \end{checkboxes}, which happens in another alignment cell; this is not allowed. The \choice command doesn't issue this \begingroup command.
    – egreg
    May 25, 2015 at 19:56
  • I also tried putting both ends together in a command with no success. So I really will have to paste those two checkboxes manually over and over again for each question? (...but I'll start the long way of trying to understand what happens inside exam.cls :-))
    – hoeni
    May 30, 2015 at 9:38
  • @hoeni \begin{checkboxes}\CorrectChoice\end{checkboxes} should be \begin{checkboxes}\CorrectChoice\ \end{checkboxes} Jun 14, 2015 at 15:11

1 Answer 1

1

I just had the same problem and redefined \choice and \CorrectChoice, so that I can use them inside a tabular environment and without adding checkboxes environment.

\usepackage{amssymb} 
\usepackage{xspace}

\newcommand{\CorrectChoice}{% true
 \ifprintanswers
   $\boxtimes$%
 \else%
   $\square$%
 \fi%
 \xspace%
}
\newcommand{\choice}{% false
   $\square$%
   \xspace%
}

You must log in to answer this question.

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