1

Please I want to modify the following codes corresponding a multiple choice tabular for obtain a result as in the below image.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\answergrid}{ m m m }{
\begin{tikzpicture}[y=.6cm]
\seq_set_split:Nnn \splitted_seq{;}{#3}
\newcounter{answer}
\int_step_inline:nnnn {1} {1} {#2} {
\stepcounter{answer}
\node[fill=green!10,draw=black,minimum~width=6mm,minimum~height=4mm] at (##1+1,#1) {\Alph{answer}};}
\int_step_inline:nnnn {1} {1} {#1} {
\node[fill=black!50,text=white, minimum~width=6mm,minimum~height=4mm] at (1, #1-##1) {\bf ##1};
\int_step_inline:nnnn {1} {1} {#2} {
\int_compare:nNnTF {####1} = {\seq_item:Nn \splitted_seq {#1-##1+1}} 
{
\node[fill=red!10,draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};
}
{\node[draw=blue,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};}
}}
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\par
\answergrid {14} {4} {2;0;0;0;1;0;2;0;0;0;2;0;2;4}
\end{document}
2

A different approach.

I heavily edited code from How to generate a dynamic bubble answer sheet for multiple-choice exam, but the looping routines were particularly useful.

REVISED ANSWER

EDITED to provide \answergrid{<no.-choices>}{<answer-key>} interface. Thus the invocation would be \answergrid{5}{B;0;0;A;C;0;0;0;0;0;D;0;B;E} to indicate choices A-E, with 14 questions whose answers are given by the list (0 indicates no answer specified).

RE-EDITED to allow multiple correct answers for a given question, using the syntax {B;E} for a given answer to a question.

\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \ans=\expandafter{\the\ans #2}%
      \advance\i by 1%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \advance\i by 1%
      \ans=\expandafter{\the\ans #2}%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[1em]{\textcolor{white}{#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1.2em]{#2}}}
\newcounter{problem}
\newcounter{choice}
\makeatletter
\newcommand{\generateQs}[2][0]{%
  \getnextanswer#1;0\relax%
  \countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
    \numberbox{\theproblem}}%
    \setcounter{choice}{0}%
    \repeatntimes{\numexpr\NumberOfChoices-1}{\stepcounter{choice}%
      \if\Alph{choice}\nextanswer\relax%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
        \expandafter\getnextanswer\moreanswers;0\relax%
      \else%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
      \fi%
    }\Prob\kern7pt%
  }%
}
\def\getnextanswer#1;#2\relax{%
  \edef\nextanswer{#1}%
  \edef\moreanswers{#2}%
}
\makeatother
\def\NumberOfChoices{4}
\newcommand\answergrid[2]{\renewcommand\NumberOfChoices{#1}\makegrid #2;\relax\relax}
\def\makegrid#1#2#3\relax{%
  \generateQs[#1]{1}\ifx\relax#3\else\makegrid#3\relax\fi%
}
\begin{document}
\answergrid{5}{{B;E};0;0;{A;C;D};C;0;0;0;0;0;D;0;B;E}
\end{document}

enter image description here

ORIGINAL ANSWER

The operative macro is \generateQs[<answer>]{<count>}. When an <answer> is specified, the <count> will typically be 1, and the answer for that [those] question[s] will be shaded pink (the rest green). If no <answer> is specified, all the possible answers are shaded green and <count> can be used to generate a number of such answer blocks.

The number of choices (A-D) is hardwired into \generateQs as the argument 3 to \repeatntimes. If the problem count needs resetting, the counter problem is that which needs to be reset to zero.

\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \ans=\expandafter{\the\ans #2}%
      \advance\i by 1%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \advance\i by 1%
      \ans=\expandafter{\the\ans #2}%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[1em]{\textcolor{white}{#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1.2em]{#2}}}
\newcounter{problem}
\newcounter{choice}
\newcommand{\generateQs}[2][0]{%
  \countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
    \numberbox{\theproblem}}%
    \setcounter{choice}{0}%
    \repeatntimes{3}{\stepcounter{choice}% <--CHOICE OF {3} PRINTS A,B,C,D
      \if\Alph{choice}#1\relax%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
      \else%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
      \fi%
    }\Prob\kern7pt%
  }%
}
\begin{document}
\generateQs[B]{1}%
\generateQs{5}
\generateQs[C]{1}%
\generateQs{4}
\generateQs[A]{1}%
\generateQs{2}
\end{document}

enter image description here

Here is a version in which the font size is reduced (to \scriptsize), the answer box widths are reduced, the \fboxsep is reduced (to 1.5pt), and the inter-question \kern is reduced (from 7pt to 5pt).

\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \ans=\expandafter{\the\ans #2}%
      \advance\i by 1%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \advance\i by 1%
      \ans=\expandafter{\the\ans #2}%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\def\FS{\scriptsize}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[.8em]{\textcolor{white}{\FS#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1em]{\FS#2}}}
\newcounter{problem}
\newcounter{choice}
\newcommand{\generateQs}[2][0]{\fboxsep=1.5pt\relax%
  \countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
    \numberbox{\theproblem}}%
    \setcounter{choice}{0}%
    \repeatntimes{3}{\stepcounter{choice}% <--CHOICE OF {3} PRINTS A,B,C,D
      \if\Alph{choice}#1\relax%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
      \else%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
      \fi%
    }\Prob\kern5pt%
  }%
}
\begin{document}
\generateQs[B]{1}%
\generateQs{5}
\generateQs[C]{1}%
\generateQs{4}
\generateQs[A]{1}%
\generateQs{2}
\end{document}

enter image description here

  • Please have you a solution when a question has two or three ... good answers. I want if possible a solution as : \answergrid{5}{B;0;0;{A;D};C;0;0;0;0;0;D;0;B;E} – user105476 May 20 '16 at 11:52
  • @user105476 Please see revised answer. – Steven B. Segletes May 20 '16 at 12:42
1

add \newcommand{\rot}[1]{\rotatebox[origin=l]{450}{#1}} in the preamble and in the body type \rot{\answergrid {14} {4} {2;0;0;0;1;0;2;0;0;0;2;0;2;4}} and compile you will the following

gridsol

1

Here an other approach but I have a problem to repeat the following code 14 times:

\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-n) {\Alph{answer}}; from n=1 to n=14

enter image description here

\documentclass{article}
\usepackage{tikz}

\usepackage{xparse}



\ExplSyntaxOn
    \NewDocumentCommand{\answergrid}{ m m m }{
        \begin{tikzpicture}[x=.6cm, rotate=90]
            \seq_set_split:Nnn \splitted_seq{;}{#3}
            \newcounter{answer}
            \int_step_inline:nnnn {1} {1} {#2} {
                \stepcounter{answer}


%----> the problem!!! 
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-1) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm,] at (6-##1,#1-2) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-3) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-4) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-5) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-6) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-7) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-8) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-9) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-10) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-11) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-12) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-13) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-14) {\Alph{answer}};}




            \int_step_inline:nnnn {1} {1} {#1} {
                \node[fill=black!50,text=white, minimum~width=6mm,minimum~height=4mm] at (6, #1-##1) {\bf ##1};
                \int_step_inline:nnnn {1} {1} {#2} {

                    \int_compare:nNnTF {####1} = {\seq_item:Nn \splitted_seq {#1-##1+1}} {
                        \node[fill=red!10,draw,minimum~width=7mm,minimum~height=4mm] at (6-####1, ##1-1) {B};
                    }{
                        \node[draw=blue,minimum~width=7mm,minimum~height=4mm] at (6-####1, ##1-1) {};
                    }
                }
            }
        \end{tikzpicture}
    }
\ExplSyntaxOff

\begin{document}

    \par

    \answergrid {15} {5} {2;0;0;0;0;0;2;0;0;0;0;2;0;0;0;0;0}



\end{document}

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.