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}
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.


\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