1

I am currently working on a document class for creating worksheets for school in which I want to include a multiplechoice environment. For this I basically used the itemize environment and changed the default bullet to a big square drawn with tikz. I also want to be able to easily create solutions for these worksheets and so I introduced a boolean solution which determines if the correct answers are ticked or not.

Now my problem is how to define which ansers are correct and which ones are not. In the end I want this to look something like this:

\begin{multiplechoice}
    \item[correct] Correct answer
    \item Incorrect answer
\end{multiplechoice}

But I am not sure how to add this parameter to the items. So far, this is what the multiplechoice environment looks like:

\newenvironment{multiplechoice}{
    \renewcommand{\labelitemi}{
        \tikz[baseline=-0.3em]{
            \draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
            \ifsolution
                \draw[black, thick] (-0.2, -0.2) -- (0.2, 0.2);
                \draw[black, thick] (-0.2, 0.2) -- (0.2, -0.2);
            \fi
        }
    }

    \itemize
    \bgroup
}{
    \egroup
    \enditemize
}

Currently, every anser will be ticked if solution is true.

3
  • I would probably prefer an input syntax like \choice / \choice* over \item / \item[Correct]. Are you using the "expl" LaTeX3 macro formats at all? Oct 1 '20 at 7:44
  • @DonaldArseneau First of all, thank you very much for your quick answer! I must admit that I am fairly new to TeX and LaTeX, so I did not know about the * sytanx, but I like it more than the [\correct] way. I am also not sure what you mean with "expl" LaTeX3 macro formats, nor do I know if I am using them...
    – Roysten R.
    Oct 2 '20 at 15:21
  • You are welcome. The * modifier is used on many LaTeX commands (but not on \item). The "expl" (experimental) definitions provide a cleaner syntax for defining such "star" commands, but no matter--the "messy" version of \choice is still easy to read. Oct 2 '20 at 23:22
0

Here is a solution using \item as requested, with only a tiny change from the question: Instead of [correct], use [\correct]. That change makes the solution much easier, since it matches the regular optional parameter that \item always takes.

\documentclass[12pt]{article}
\usepackage{tikz}

\newif\ifsolution

\newcommand{\choicebox}{%
    \tikz[baseline=-0.3em]{%
       \draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
    }%
}
\newcommand{\correct}{%
    \ifsolution
        \tikz[baseline=-0.3em]{%
            \draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
            \draw[black, thick] (-0.2, -0.2) -- (0.2, 0.2);
            \draw[black, thick] (-0.2, 0.2) -- (0.2, -0.2);
        }%
    \else
        \choicebox
    \fi
}

\newenvironment{multiplechoice}{%
    \renewcommand{\labelitemi}{\choicebox}%
    \itemize
}{%
    \enditemize
}
% The previous \bgroup and \egroup were bad.

\begin{document}

\begin{multiplechoice}
    \item[\correct] Correct answer
    \item Incorrect answer
\end{multiplechoice}

\solutiontrue


\begin{multiplechoice}
    \item[\correct] Correct answer
    \item Incorrect answer
\end{multiplechoice}

\end{document}

I would probably prefer to use an input syntax of \choice and \choice*, and a definition of \choice could be layered over the solution with \item.

\makeatletter
\newcommand\choice{\@ifstar{\item[\correct]}{\item}}
\makeatother

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.