Mcq Answer Grid with Tikz

I rewrote the code to generate the answer grid. I hope that's not a problem.

I made a command \answergrid that takes 3 arguments: the number of questions, the number of possible answers per question and the third argument is a semi-colon separated list of answers.

\answergrid {10} {4} {1;2;4;3;2;3;1;2;0;0}

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
    \NewDocumentCommand{\answergrid}{ m m m }{
        \begin{tikzpicture}[y=.6cm]
            \seq_set_split:Nnn \l_tmpa_seq{;}{#3}
            \newcounter{answer}
            \int_step_inline:nnnn {1} {1} {#2} {
                \stepcounter{answer}
                \node at (##1+1,#1) {\Alph{answer}};
            }
            \int_step_inline:nnnn {1} {1} {#1} {
                \node[fill=black,minimum~width=6mm,minimum~height=4mm] at (0, #1-##1) {};
                \node at (1, #1-##1) {##1};
                \int_step_inline:nnnn {1} {1} {#2} {
                    \node[draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};
                    \int_compare:nNnTF {####1} = {\seq_item:Nn \l_tmpa_seq {#1-##1+1}} {
                        \node[fill,draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};
                    }{
                        \node[draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};
                    }
                }
            }
        \end{tikzpicture}
    }
\ExplSyntaxOff

\begin{document}

    \sffamily\small
    PREFERENCE\par

    \answergrid {10} {4} {1;2;4;3;2;3;1;2;0;0}

\end{document}

It's important for the semi-colon separated list to have at least as much elements as the number of questions. If you don't want to mark any answers, just give a list of zeros {0;0;0;...;0;0;}

EDIT: according to the changes requested in the comments I added an optional argument. This argument is the first number of the question list. So

\answergrid [21] {20} {4} {1;2;4;3;2;3;1;2;0;0;1;2;4;3;2;3;1;2;0;0}

generates 20 questions numbered 21 to 40. That way you can use

\answergrid      {20} {4} {1;2;4;3;2;3;1;2;0;0;1;2;4;3;2;3;1;2;0;0}
\answergrid [21] {20} {4} {1;2;4;3;2;3;1;2;0;0;1;2;4;3;2;3;1;2;0;0}

to get 40 questions.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\answergrid}{ O{1} m m m }{
    \begin{tikzpicture}[y=.5cm]
        \seq_set_split:Nnn \l_tmpa_seq{;}{#4}
        \int_step_inline:nnnn {1} {1} {#3} {\node at (##1+1,#2) {\int_to_Alph:n{##1}};}
        \int_step_inline:nnnn {#1} {1} {#1+#2-1} {
            \node at (0, #1+#2-##1-1) {Question}; \node at (1, #1+#2-##1-1) {##1};
            \int_step_inline:nnnn {1} {1} {#3} {
                \node[draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-#1) {};
                \int_compare:nNnTF {####1} = {\seq_item:Nn \l_tmpa_seq {#1+#2-##1}} {\node[fill,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-#1) {};}{}
            }
        }
    \end{tikzpicture}
}
\ExplSyntaxOff

\begin{document}

    \sffamily\small

    \answergrid      {20} {4} {1;2;4;3;2;3;1;2;0;0;1;2;4;3;2;3;1;2;0;0}
    \answergrid [21] {20} {4} {1;2;4;3;2;3;1;2;0;0;1;2;4;3;2;3;1;2;0;0}

\end{document}

There is no automatic way. You have at least to mark all the black boxes.

\documentclass[10pt]{article}
\usepackage{array}
\pagestyle{empty}
\baselineskip=6.35mm
\parskip=0pt

\unitlength=1mm
\def\BOX{\framebox(4.55,3){}}
\def\BBOX{\framebox(4.55,3){\rule{4.2mm}{2.8mm}}}
\newcounter{rowNo}
\def\strobe{\rule{0pt}{4mm}\rule{3mm}{2.54mm}}

\begin{document}
\sffamily\small
PREFERENCE\par

\tabcolsep=0pt
\begin{tabular}{@{\strobe\stepcounter{rowNo}\quad\makebox[1em][r]{\therowNo}\quad} 
*4{>{\centering}p{6.35mm}}}
\BBOX & \BOX  & \BOX  & \BOX \tabularnewline
\BOX  & \BBOX & \BOX  & \BOX \tabularnewline
\BOX  & \BOX  & \BOX  & \BBOX \tabularnewline
\BOX  & \BOX  & \BBOX & \BOX \tabularnewline
\BOX  & \BBOX & \BOX  & \BOX \tabularnewline
\BOX  & \BOX  & \BBOX & \BOX \tabularnewline
\BBOX & \BOX  & \BOX  & \BOX \tabularnewline
\BOX  & \BBOX & \BOX  & \BOX \tabularnewline
\BOX  & \BOX  & \BOX  & \BOX \tabularnewline
\BOX  & \BOX  & \BOX  & \BOX \tabularnewline
\end{tabular}
\end{document}

enter image description here

Tags:

Tikz Pgf