0

I'm trying to create beautiful Mcq (Multiple Choice Question) Answer Grid so could someone help me please to improve it with the following needs:

  • Add Arguments to get automatic row-column numbers in grid table
  • to mark correct answer automatic with \ding{53}

The input would resemble \McqAnswerGrid{<number of questions>}{<number of choices>}{<correct cells>} where the <number of choices> should be alphabetically enumerated and <correct cells> is a list of lists containing possible correct choices.

Here is an example of input of arguments you can make your own or adapt with that one:

  • Indicate all the answers that apply

    \McqAnswerGrid{6}{6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4},{2,4} }
    

    implies we have 6 question with 6 choices A, B, C, D, E, and F where answers that apply to

    • Question 1 are 1, 2, and 3

    • Question 2 are 1, and 6

    • Question 3 are 1, and 3

    • Question 4 are 2, and 3

    • Question 5 are 2, and 4

    • Question 6 are 2, and 4

    enter image description here

  • Select one answer choice

    \McqAnswerGrid{5}{4}{ {1},{1},{3},{2},{4} }
    

    implies we have 5 question with 4 choices A, B, C, and D where the correct answer for

    • Question 1 is 1

    • Question 2 is 1

    • Question 3 is 3

    • Question 4 is 2

    • Question 5 is 4

    enter image description here

Note that most books provide 4 choices for Mcq math, while others may provide 5 or 6 at most, so the maximum number of choices will be 6 at least and for the maximum number of questions 80 at least.

At first I tried with tikz and after that tabular but it takes lot of time to fill it manually.

Here is my code:

\documentclass{article}
\usepackage{tikz}

\newcounter{row}
\newcounter{col}
\usepackage{tabu}
\usepackage{pifont}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.4]
  \begin{scope}
    \draw (0, 0) grid (5,10);
  \end{scope}
    \end{tikzpicture}
        \end{center}
        \begin{tabular}{|c|c|c|c|c|c|c|}
\hline
N & A & B & C & D & E & F \\ \hline
1 & \ding{53}  & \ding{53}  &  \ding{53}   & & &   \\ \hline
2 &  \ding{53} &   &   &   & & \ding{53}\\ \hline
3 & \ding{53}   &   & \ding{53}  &  & &   \\ \hline
4 &   &  \ding{53} & \ding{53}   &  & &  \\ \hline
5 &   & \ding{53}   &  & \ding{53}  & &  \\ \hline
6 &   & \ding{53}   &  & \ding{53}  & &  \\ \hline
\end{tabular}

\begin{tabular}{|c|c|c|c|c|}
\hline
N & A & B & C & D  \\ \hline
1 & \ding{53}  &  &   &    \\ \hline
2 &  \ding{53} &   &   &   \\ \hline
3 &   &   & \ding{53}  &    \\ \hline
4 &   &  \ding{53} &   &    \\ \hline
5 &   &   &  & \ding{53}   \\ \hline
\end{tabular}

\end{document}

If someone can't make it with this input of arguments you can come up with your one solution to cover that problem.

  • You provided 6 "correct answers", yet there's only 5 questions/columns in \McqAnswerGrid{10}{5}{1,1,3,2,4,2}. Also, what would your the macro look like that would result in your picture? \McqAnswerGrid{12}{4}{?}... – Werner Dec 23 '15 at 20:49
  • Thanks but it was just an example but to be accurate should be '\McqAnswerGrid{10}{5}{1,1,3,2,4,2,...}' . for the macro you can suggest me one – Educ Dec 23 '15 at 20:51
  • So 10 is the number of questions (N in your picture, running down) and 5 is the number of choices (A, B, C, D, E, for example)? You need to be very clear what you want. Specifics are important. – Werner Dec 23 '15 at 20:53
  • nope there is only 4 choices A B C D but i add one another colum for numbers – Educ Dec 23 '15 at 20:53
  • 1
    I agree with @cfr. My suggestion would be to show us two things: An example of an input and the associated output. Thus far you've shown some input, and a completely unrelated output. – Werner Dec 23 '15 at 22:06
3

The following example provides \McqAnswerGrid{<rows>}{<columns>}{<answer spec>} and a duplicate \McqOneAnswerGrid (purely because they may mean different things). They're a duplicate of one another since they both use the same style input:

enter image description here

\documentclass{article}

\usepackage{multido,xparse}

\DeclareRobustCommand{\correctsymbol}{$\times$}

\makeatletter
\NewDocumentCommand{\row@column@element}
  { m }
  {\@namedef{row@element@\therow@cnt @#1}{\correctsymbol}}

\DeclareDocumentCommand{\process@columns}
  { > { \SplitList { , } } m }
  {\stepcounter{row@cnt}\ProcessList{#1}{\row@column@element}}%

\DeclareDocumentCommand{\process@rows}%
  { > { \SplitList { , } } m }
  {\ProcessList{#1}{\process@columns}}

\newcounter{row@cnt}
\newcommand{\McqAnswerGrid}[3]{{%
  \def\row@elements{N}% Top left entry
  \let\tabularline\relax% Create non-expandable \tabularline (will be used later)
  \multido{\iColumn=1+1}{#2}{% Create first row
    \protected@xdef\row@elements{\row@elements & \@Alph{\iColumn}}}%
  % Construct the top row of answer grid
  \g@addto@macro\row@elements{ \\ \tabularline}% Add line
  \setcounter{row@cnt}{0}%
  \process@rows{#3}%
  \multido{\iRow=1+1}{#1}{%
    \protected@xdef\row@elements{\row@elements \iRow}
    \multido{\iColumn=1+1}{#2}{%
      \protected@xdef\row@elements{\row@elements & 
        \ifcsname row@element@\iRow @\iColumn\endcsname
          \csname row@element@\iRow @\iColumn\endcsname
        \fi}%
    }%
    \g@addto@macro\row@elements{ \\ \tabularline}%
  }%
  % Set actual answer grid
  \begin{tabular}{ | *{\number\numexpr#2+1}{c|} }
    \hline
    \global\let\tabularline\hline% Make sure \tabularline is equivalent to \hline
    \row@elements
  \end{tabular}
}}

\let\McqOneAnswerGrid\McqAnswerGrid

\makeatother

\begin{document}

\McqAnswerGrid{6}{6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4},{2,4} }

\bigskip

\McqOneAnswerGrid{5}{4}{ {1},{1},{3},{2},{4} }

\end{document}

We construct the tabular using a series of loops that information from a list-processor. The list processor creates a macro \row@element@<row>@<column> containing the \correctsymbol if it is specified. Else, the macro doesn't exist. During expansion of the tabular construction, the existence of \row@element@<row>@<column> is tested and subsequently used.

  • +1 Thank you so much for your work but could you make it breakable because when i put 80 for rows doesn't show all the rows – Educ Dec 24 '15 at 7:25
  • Could you add version which column is row – Educ Dec 29 '15 at 12:57
2

Here's a LaTeX 3 solution.

It has the requested 3 arguments for the number of rows, the number of columns and the cells to be marked. It also has an optional argument that specifies the number of the first question in the table. That way you can "split" long tables in multiple shorter ones and place them next to each other.

\McqAnswerGrid     {5} {6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4} }
\hspace{1cm}
\McqAnswerGrid [6] {5} {6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4} }

enter image description here

\documentclass{article}
\usepackage{xparse}
\usepackage{pifont}

\ExplSyntaxOn
\NewDocumentCommand{\McqAnswerGrid}{ O{1} m m m }{
    \begin{tabular}{|c*{#3}{|c}|}
        \hline
        \tl_clear:N \l_tmpa_tl
        \tl_put_right:Nn \l_tmpa_tl {N}
        \int_step_inline:nnnn {1} {1} {#3} {\tl_put_right:Nn \l_tmpa_tl {& \int_to_Alph:n{##1}}}
        \tl_put_right:Nn \l_tmpa_tl {\\\hline}
        \int_step_inline:nnnn {#1} {1} {#1+#2-1} {
            \seq_set_split:Nnn \splitted_seq{,}{#4}
            \exp_args:NNnx \seq_set_split:Nnn \splitted_again {,} {\seq_item:Nn \splitted_seq {##1-#1+1}}
            \tl_put_right:Nn \l_tmpa_tl {##1}
            \int_step_inline:nnnn {1} {1} {#3} {
                \seq_if_in:NnTF \splitted_again {####1} {
                    \tl_put_right:Nn \l_tmpa_tl {& \ding{53}}
                } {
                    \tl_put_right:Nn \l_tmpa_tl {& }
                }
            }
            \tl_put_right:Nn \l_tmpa_tl {\\\hline}
        }
        \tl_use:N \l_tmpa_tl
    \end{tabular}
}
\ExplSyntaxOff

\begin{document}

    \McqAnswerGrid     {5} {6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4} }
    \hspace{1cm}
    \McqAnswerGrid [6] {5} {6}{ {1,2,3},{1,6},{1,3},{2,3},{2,4} }

\end{document}
  • +1 Thank you but could you make the argument like what you did for that question tex.stackexchange.com/questions/285074/… – Educ Jan 4 '16 at 17:50
  • What do you mean? You can pefectly use \McqAnswerGrid [7] {6} {6} {6,5,4,3,2,1} if you want only one solution per question. I don't see a difference in arguments. The first one is optional and is the starting number, the second is the number of questions, the third is the number of possible answers and the fourth is a list of correct answers. The only difference I see is that this questions uses a comma separated list of solutions while the other question has a semi-colon separated list of solutions. But you could easily edit the code yourself to change this. – Maarten Dhondt Jan 4 '16 at 19:28
  • i'm talking about the list of correct answer like that \Mcqanswergrid {10} {4} {1;2;4;3;2;3;1;2;0;0} – Educ Jan 4 '16 at 20:08
  • The only difference between \McqAnswerGrid {10} {4} {1,2,4,3,2,3,1,2,0,0} from this question and \answergrid {10} {4} {1;2;4;3;2;3;1;2;0;0} from the other question is the comma instead of the semi-colon. Is that what you want different? Then change the comma on line 14 and 15 to a semi-colon. If you want something else, you'll have to be more specific because right now \McqAnswerGrid {10} {4} {1,2,4,3,2,3,1,2,0,0} works exactly as expected. – Maarten Dhondt Jan 4 '16 at 20:15

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.