13

i found the following nice code in this question Is there a package to create Optical Mark Reader answer-sheets?

Code :

%%\listfiles
\documentclass[10pt]{article}
\usepackage{array}
\pagestyle{empty}
\parindent=0pt
\baselineskip=6.35mm
\parskip=0pt
\makeatletter
\newcommand*{\InitToks}{\toks@={}}
\newcommand{\AddToks}[1]{\toks@=\expandafter{\the\toks@ #1}}
\newcommand*{\PrintToks}{\the\toks@}
\unitlength=1mm
\def\BOX{\framebox(4.55,3){}}
\newcount\rowNo
\newcommand*{\dynTable}[1]{%
  \begingroup
    \InitToks
    \AddToks{\tabcolsep=0pt\begin{tabular}{c*{5}{>{\centering}p{6.35mm}}}}%
    \AddToks{&&A&B&C&D \tabularnewline}
    \rowNo=0 %
    \loop\ifnum\rowNo<#1\relax
      \advance\rowNo by 1
      \AddToks{\strobe & }
      \expandafter\AddToks\expandafter{%
         \the\rowNo & 
         \BOX & \BOX & \BOX & \BOX %& \BOX & \BOX & \BOX & \BOX & \BOX & \BOX%
        \tabularnewline}%
    \repeat
   \AddToks{\end{tabular}}%
    \PrintToks
  \endgroup}
\makeatother

\def\strobe{\rule{0pt}{4mm}\rule{3mm}{2.54mm}}

\begin{document}
\sffamily\small
 PREFERENCE\par
%\bigskip
\dynTable{40}
\end{document}

Output:

enter image description here

but i would like to mark correct answer automatic to looks like

enter image description here

  • Could someone help me to manage that. please?

2 Answers 2

15

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}
8
  • Could you please take look to that question too tex.stackexchange.com/questions/284495/mcq-answer-grid by using idea to mark correct answer
    – Educ
    Commented Dec 29, 2015 at 15:56
  • another question when i put 40 question the figure will be out of the margin could you fix that please
    – Educ
    Commented Dec 29, 2015 at 16:02
  • Maybe... What would you consider fixed? Should it be splitted on multiple pages? Or in multiple columns? Or ... Commented Dec 29, 2015 at 16:05
  • Multiple columns please btw its works by changing '\begin{tikzpicture}[y=.6cm]' to \begin{tikzpicture}[y=.45cm]
    – Educ
    Commented Dec 29, 2015 at 16:06
  • 1
    change \node[fill=black,minimum~width=6mm,minimum~height=4mm] at (0, #1-##1) {}; to \node at (0, #1-##1) {Question}; Commented Dec 29, 2015 at 16:10
6

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

1
  • +1 thank you this is work too but seems @Maarten Dhondt found the solution with 'tikz'
    – Educ
    Commented Dec 29, 2015 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .