2

I want to generate a table for multiple choices with the following requirements

  1. Numbers are distributed uniformly on \linewidth
  2. Dot separators of numbers are aligned between rows
  3. Ability to customize the numbers easily
  4. Ability to determine how many entries for each row (e.g., 1-5 on one row then 6-10 on the next)

enter image description here

\documentclass[11pt, a4paper]{article}

\usepackage{geometry}

\usepackage{nicematrix}
\usepackage{booktabs}

\newcounter{choicenum}
\setcounter{choicenum}{0}

\newcommand{\postMultipleChoiceTable}{

\noindent%
{\bfseries Answers to Multiple Choice Questions}

\noindent%
\begin{NiceTabularX}{\linewidth}{@{}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t] @{\stepcounter{choicenum}}X[l, t]@{}}
    
    \toprule
    
    \stepcounter{choicenum}\phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \phantom{1}\arabic{choicenum}. & \arabic{choicenum}.
    \\
    
    \stepcounter{choicenum}\arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}. & \arabic{choicenum}.
    \\
    
    \stepcounter{choicenum}\arabic{choicenum}. & & & & & &  & & &
    \\
    
    \bottomrule
    
\end{NiceTabularX}

}

\begin{document}

\postMultipleChoiceTable

\end{document}

The code above works, but not optimal

1 Answer 1

1

Adaptations

  • added packages etoolbox and pgffor
  • defined command \createtabledata that is used for automatically creating the table content
  • \postMultipleChoiceTable
    • has now two arguments:
      • #1 = number of columns -> \numOfCols
      • #2 = number of questions -> \numOfQuestions
    • defined table columns with *{\numOfCols}{ X[l, t] @{} }
    • use \setcounter{choicenum}{0} within the command

Result

enter image description here

Code

\documentclass[11pt, a4paper]{article}

\usepackage{geometry}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{pgffor}

\newcounter{choicenum}

\makeatletter
\newcommand{\createtabledata}{%
    \def\tabledata{}%
    \pgfmathparse{ceil(\numOfQuestions/\numOfCols)}% calculate number of lines
    \foreach \j in {1, ..., \pgfmathresult} {% lines
        \foreach \i in {1, ..., \numOfCols} {% columns
            \global\stepcounter{choicenum}%
            \protected@xappto\tabledata{%
                \ifnumless{\arabic{choicenum}}{\numOfQuestions+1}{%
                    \ifnumless{\arabic{choicenum}}{10}{%
                        \phantom{1}%
                    }{}%
                    \arabic{choicenum}.%
                }{}%
                \ifnumless{\i}{\numOfCols}{&}{\\}%
            }%
        }%
    }%
    \tabledata%
}
\makeatother

\newcommand{\postMultipleChoiceTable}[2]{
    % #1 = number of columns
    % #2 = number of questions
    \def\numOfCols{#1}
    \def\numOfQuestions{#2}
    %
    \setcounter{choicenum}{0}
    
    \medskip
    \noindent%
    {\bfseries Answers to Multiple Choice Questions}
    
    \noindent%
    \begin{NiceTabularX}{\linewidth}{@{} *{\numOfCols}{ X[l, t] @{} } @{}}
        \toprule
        \createtabledata
        \bottomrule
    \end{NiceTabularX}
}

\begin{document}

\postMultipleChoiceTable{10}{21}
\postMultipleChoiceTable{6}{20}
\postMultipleChoiceTable{5}{3}

\end{document}
1

You must log in to answer this question.

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