自动计算表格列数和列号

自动计算表格列数和列号

我正在寻找一种方法来创建一个命令,通过该命令 Latex 可以自动生成具有给定数量列的表格,并自动填充列号作为标题。

我正在寻找这样的东西:

\newcommand{\CountBox}[1]{
    \begin{flushright}
        \begin{tabular}{*{#1}{|l}}
        \hline
        \multicolumn{1}{|c|}{1} #1  \\ \hline
                                #1   \\ \hline
        \end{tabular}   
    \end{flushright}
}

其中以下命令创建下表:

\CountBox{6}

6列表格

以下命令将创建下表:

\CountBox{3}

3列表格

我似乎陷入了必须手动将“&”符号输入多列命令的困境(已经尝试过 \loop、\while 和 \expandafter)

谢谢您的帮助!


以防万一,6列表格的原始代码如下:

\begin{flushright}
    \begin{tabular}{*{7}{|l}}
    \hline
    \multicolumn{1}{|c|}{1} & 2 & 3 & 4 & 5 & 6 \\ \hline 
                            &   &   &   &   &   \\ \hline

    \end{tabular}   
\end{flushright}

答案1

\foreachTikZ 中:

\documentclass{article}       
\usepackage{tikz}
\usepackage{etoolbox}
\usetikzlibrary{
    positioning,
    shapes.multipart
    }
\tikzset{
    mynode/.style={
        draw,
        rectangle split,
        rectangle split parts=2,
        text centered,
    },
}
\newcommand{\CountBox}[1]{%
    \begin{tikzpicture}
        \node[mynode] (1) {1};
        \ifnumcomp{#1}{=}{1}{}{%
            \foreach \mynum 
                [evaluate=\mynum as \myprev using int(\mynum-1)]  
                in {2,...,#1}
            \node[mynode,xshift=-\pgflinewidth,anchor=west] (\mynum) at (\myprev.east) {\mynum};
            }%
    \end{tikzpicture}%
    }

\begin{document}
\CountBox{1} 

\CountBox{3}

\CountBox{6}

\CountBox{2} 
\CountBox{4}
\CountBox{7} 
\end{document}

在此处输入图片描述

答案2

这是一个expl3实现。借助array及其w列说明符,可以使所有列的宽度与最后一列(具有最大数字)的宽度相同。

\documentclass{article}

\usepackage{xparse,array}

\ExplSyntaxOn
\NewDocumentCommand{\CountBox}{m}
 {
  % measure the wider number
  \hbox_set:Nn \l_tmpa_box { #1 }
  \dim_set:Nn \l_tmpa_dim { \box_wd:N \l_tmpa_box }
  % do as many columns as specified
  \begin{tabular}{|*{#1}{w{c}{\l_tmpa_dim}|}}
  \hline
  % do '<number> &' one less than specified, then add the last number
  \int_step_function:nN { #1 - 1 } \__phlemp_countbox_head:n #1 \\
  \hline
  % generate as many &'s as necessary to fill the second row
  \prg_replicate:nn { #1 - 1 } { & } \\
  \hline
  \end{tabular}
 }

% auxiliary function for adding the & after the number
\cs_new_protected:Nn \__phlemp_countbox_head:n { #1 & }
\ExplSyntaxOff

\begin{document}

\CountBox{3} \CountBox{10}

\end{document}

在此处输入图片描述

答案3

以下用途multido生成列编号序列和空白行。与 egreg 的使用方法类似arrayw{<align>}{<width>}列宽设置为最宽元素:

在此处输入图片描述

\documentclass{article}

\usepackage{multido,array}

\newcounter{boxCount}
\newlength{\boxCountwd}

\makeatletter
\newcommand{\CountBox}[1]{%
  \setcounter{boxCount}{0}% Reset boxCount
  \settowidth{\boxCountwd}{#1}% Measure widest element
  \def\CountBoxSeq{\@gobble}%
  \def\CountBoxSeqPhantom{\@gobble}%
  \multido{\i=1+1}{#1}{%
    \xdef\CountBoxSeq{\CountBoxSeq & \i}%
    \xdef\CountBoxSeqPhantom{\CountBoxSeqPhantom &}%
  }%
  \noindent
  \begin{tabular}{ *{#1}{|w{c}{\boxCountwd}} | }
    \hline
    \CountBoxSeq \\
    \hline
    \CountBoxSeqPhantom \\
    \hline
  \end{tabular}%
}
\makeatother

\begin{document}

\CountBox{5}

\end{document}

相关内容