2

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}
Mark
  • 1,393
  • 2
    Please attribute code! This is actually required, I think, but mostly it is just rather rude not to. – cfr May 13 '16 at 02:13
  • Related: http://tex.stackexchange.com/questions/285074/mcq-answer-grid-with-tikz/285086#285086, http://tex.stackexchange.com/questions/284495/mcq-answer-grid/285952#285952, http://tex.stackexchange.com/questions/219425/how-to-generate-a-dynamic-bubble-answer-sheet-for-multiple-choice-exam. What have you tried by way of adapting the code? What is the problem exactly? – cfr May 13 '16 at 02:14

3 Answers3

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

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

murugan
  • 1,669
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}
wilx
  • 2,339