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*{\PrintToks}{\the\toks@}
\unitlength=1mm
\def\BOX{\framebox(4.55,3){}}
\newcount\rowNo
\newcommand*{\dynTable}[1]{%
\begingroup
\InitToks
\rowNo=0 %
\loop\ifnum\rowNo<#1\relax
\the\rowNo &
\BOX & \BOX & \BOX & \BOX %& \BOX & \BOX & \BOX & \BOX & \BOX & \BOX%
\tabularnewline}%
\repeat
\PrintToks
\endgroup}
\makeatother

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

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


Output:

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

• Could someone help me to manage that. please?

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}


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

\ExplSyntaxOn
\begin{tikzpicture}[y=.6cm]
\seq_set_split:Nnn \l_tmpa_seq{;}{#3}
\int_step_inline:nnnn {1} {1} {#2} {
}
\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

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


to get 40 questions.

\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

\end{document}

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

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


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