4

Following this post, I would like to do a checkbox list, with enumerate elements. Before each enumerate label, an empty checkbox is present. In the MWE below, I'd like to have the box in front of the third element checked. (But still be numbered c.)

\documentclass{article}

\usepackage{pifont,amssymb} % for the symbols \usepackage[shortlabels]{enumitem} \newlist{answerlist}{enumerate}{2} \setlist[answerlist]{label={$\square$ \hspace{1pt}\alph.}} \newcommand{\cmark}{\ding{51}}% \newcommand{\correct}{ \rlap{$\square$ \hspace{1pt}\alph{\theenumi}.}{\raisebox{2pt}{\large\hspace{1pt}\cmark} *a}% \hspace{-2.5pt} } \newcommand{\correctanswer}{\item[\correct] }

\begin{document}

\begin{answerlist} \item One \item Two \correctanswer Test \end{answerlist}

\end{document}

But the \correctanswer above generates an error because the counter \theenumi seems to be zero at that point.

Peutch
  • 2,182

2 Answers2

4

\alph has special meaning inside the definitions of enumitem; it defines \alph*to be an alphabetic representation of the current counter level. You're using it outside the list setup and therefore \alph* is actually the same as \alph{*}... and there exists no * counter.

The following example provides what you're after though:

enter image description here

\documentclass{article}

\usepackage{pifont,amssymb} % for the symbols \usepackage[shortlabels]{enumitem}

\newlist{answerlist}{enumerate}{2} \setlist[answerlist]{label={\alph.\makebox[0pt][r]{\noexpand\emptysquare\hspace{2em}}},ref=\alph}

\newcommand{\emptysquare}{$\square$} \newcommand{\checkedsquare}{\makebox[0pt][l]{\raisebox{1pt}[0pt][0pt]{\large\hspace{1pt}\cmark}}$\square$} \newcommand{\cmark}{\ding{51}}% \newcommand{\correctanswer}{{\renewcommand{\emptysquare}{\checkedsquare}\item\leavevmode}}

\begin{document}

\begin{answerlist} \item One \item Two \correctanswer Test \item Three \end{answerlist}

\end{document}

Werner
  • 603,163
  • Thank you for your explanations. I can't claim to perfectly understand the solution, but I'll play around with it until I do! – Peutch Oct 02 '20 at 14:02
  • \ref doesn't seem to work for me anymore, or at least only intermittently (I'm having reproducing exactly when it breaks) after changing an \item to a \correctanswer. Doesn't matter for people not referencing the lists, but worth noting probably. – hasManyStupidQuestions Apr 15 '22 at 21:13
0

I copy-paste-modified the accepted answer to this question https://tex.stackexchange.com/a/564221/212071 as well as this answer to a related question: https://tex.stackexchange.com/a/155051/212071

Basically the key "innovation" is that apparently enumitem has a before= option, that we can use instead of \renewcommand ... \leavemode in the accepted answer. This seems to make references work better (in particular addressing this comment).

Anyway below is copy-paste-modified a minimal working example.

\documentclass{article}

\usepackage{enumitem}

% https://tex.stackexchange.com/a/564221/212071 copy-paste \newlist{answerlist}{enumerate}{2}

\newif\ifmoditem \newcommand{\setupmodenumerate}{% \global\moditemfalse \let\origmakelabel\makelabel \def\moditem##1{\global\moditemtrue\def\mesymbol{##1}\item}% \def\makelabel##1{% \origmakelabel{\ifmoditem\llap{\mesymbol\enspace}\fi##1}% \global\moditemfalse}% }

\setlist[answerlist]{ref={\arabic},label={\arabic},before=\setupmodenumerate}

\usepackage{amssymb} % needed for square \usepackage{pifont} % needed for checked square

\newcommand{\emptysquare}{$\square$} \newcommand{\cmark}{\ding{51}}% \newcommand{\checkedsquare}{\makebox[0pt][l]{\raisebox{1pt}[0pt][0pt]{\large\hspace{1pt}\cmark}}$\square$}

\newcommand{\todoitem}{\moditem{\emptysquare}} \newcommand{\doneitem}{\moditem{\checkedsquare}}

\begin{document}

\begin{answerlist} \todoitem One \todoitem Two \doneitem Test \todoitem Three \end{answerlist}

\end{document}