如何生成数字均匀分布的编号表?

如何生成数字均匀分布的编号表?

我想生成一个具有以下要求的多项选择表

  1. 数字均匀分布在\linewidth
  2. 数字的点分隔符在行之间对齐
  3. 能够轻松自定义数字
  4. 能够确定每行有多少个条目(例如,一行有 1-5 个条目,下一行有 6-10 个条目)

在此处输入图片描述

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

上述代码可以工作,但不是最优的

答案1

改编

  • 添加包etoolboxpgffor
  • \createtabledata定义用于自动创建表内容的命令
  • \postMultipleChoiceTable
    • 现在有两个参数:
      • #1 = 列数 ->\numOfCols
      • #2 = 问题数量 ->\numOfQuestions
    • 使用以下方式定义表列*{\numOfCols}{ X[l, t] @{} }
    • \setcounter{choicenum}{0}在命令中使用

结果

在此处输入图片描述

代码

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

相关内容