3

Consider the following MWE:

\documentclass{article}
\usepackage{tikz}

\usepackage{xparse}

\ExplSyntaxOn \NewDocumentCommand{\answergrid}{ m m m }{ \begin{tikzpicture}[x=0.75cm,baseline] \seq_set_split:Nnn \splitted_seq{;}{#3} \newcounter{answer} \int_step_inline:nnnn {1} {1} {#2} { \stepcounter{answer} \node[draw,circle,inner~sep=1pt,anchor=base] at (##1,0) {\Alph{answer}};}

        \int_step_inline:nnnn {1} {1} {#1} {
            \int_step_inline:nnnn {1} {1} {#2} {

                \int_compare:nNnTF {####1} = {\seq_item:Nn \splitted_seq {#1-##1+1}} {
                    \node[fill=black!10,draw,circle,inner~sep=1pt,anchor=base] at (####1, 0) {\Alph{answer}};
                }{
                }
            }
        }
    \end{tikzpicture}
}

\ExplSyntaxOff

\begin{document}

\par
\begin{enumerate}
    \item \answergrid {1} {4} {2}
    \item \answergrid {1} {4} {2}
\end{enumerate}

\end{document}

enter image description here

Taking inspiration from Multiple Choice Questions with xparse package, I tried to edit the code so that I can produce the answer to a bubble sheet. It works with one line but if I try to add a second line I get an error. I know am missing something about the counter, but am not sure what is it.

Note that it is not printing the correct answer and multiple used of the command gives weird interactions.

azetina
  • 28,884
  • Change \newcounter{answer} to \setcounter{answer}{0} and place \newcounter{answer} before \NewDocumentCommand{\answergrid}{ m m m }{, that is, outside of the defintion of \answergrid. Why does this solve the problem? With your current code, you tell TeX to call \newcounter{answer} everytime you use \answergrid, but you cannot initiate a counter more than once. – Jasper Habicht Jul 15 '23 at 19:18
  • @JasperHabicht Thanks for the comment. It does address one of the issues but I still cannot manage to print the correct answer. – azetina Jul 15 '23 at 19:25
  • Using TikZ seems like overkill for something like this? Though it's probably what I'd do, too. :) I thought mixing expl3 and pgf code was a recipe for trouble? – cfr Jul 16 '23 at 12:51

1 Answers1

4

I have the feeling that the code is a bit too complicated. It seems that you want \item \answergrid {1} {4} {2} to output four circles (A) to (D) and mark the second one.

Then, I am not fully sure, but it looks as if the linked code also provides for the possibility to mark multiple answers, so \item \answergrid {1} {4} {1;2} would mark the first two of the four circles. However, instead of nesting several \int_step_inline:nnnn as you do, you could make use of \seq_if_in:NnTF, I think, which simplifies the code a lot.

The following does exactly this. Also, as a kind of add-on, if you say \item \answergrid {2} {4} {3}, the code would output three circles (B), (C) and (D) and mark the one in the middle.

Note that you should probably adhere to the expl3 naming conventions:

\documentclass{article}
\usepackage{tikz}

\ExplSyntaxOn \newcounter{answer} \NewDocumentCommand{\answergrid}{ m m m }{ \begin{tikzpicture}[x=0.75cm, baseline] \seq_set_split:Nnn \l_azetina_answers_split_seq { ; } { #3 } \setcounter{answer}{ \int_eval:n { #1 - 1 } } \int_step_inline:nnnn { \int_eval:n { \value{answer} + 1 } } { 1 } { #2 } { \stepcounter{answer} \seq_if_in:NnTF \l_azetina_answers_split_seq { ##1 } { \tikzset{answer~node/.style={fill=gray!20}} }{ \tikzset{answer~node/.style={fill=none}} } \node[answer~node, draw, circle, inner~sep=1pt, anchor=base] at (##1, 0) {\Alph{answer}}; } \end{tikzpicture} } \ExplSyntaxOff

\begin{document} \begin{enumerate} \item \answergrid {1} {4} {1;2} \item \answergrid {1} {4} {2} \item \answergrid {2} {4} {3} \end{enumerate} \end{document}

enter image description here

  • 1
    Note that it is not really necessary to load the xparse package, except if you use a fairly old TeX distribution (what you probably should not do). – Jasper Habicht Jul 16 '23 at 08:51