4

I'm looking for a way to create a command with which Latex can automatically generate a table with a given amount of columns, and automatically fills in the column numbers as headers.

I'm looking for something like this:

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

Where the following command creates the following table:

\CountBox{6}

6 Column Table

And the following command creates the following table:

\CountBox{3}

3 Column Table

I seem to get stuck on having to manually input the ampersands into the multicolumn command (have tried \loop, \while, and \expandafter)

Thanks for the help!


Just in case the original code for the 6 column table:

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

    \end{tabular}   
\end{flushright}
Phlemp
  • 43

3 Answers3

5

Here is an expl3 implementation. With the help of array and its w column specifier, all columns can be made as wide as the last (with the largest number).

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

enter image description here

egreg
  • 1,121,712
  • Thanks for the quick response! It produced a lot of illegal pream-tokens on my side though (working in Overleaf). Also did not allow the _ outside of math-mode, and an undefined control sequence on " \int_step_function:nN ". Is that something I can fix or have to install something for? Much appreciated. – Phlemp Jan 05 '19 at 14:23
3

The following uses multido to generate the column numbering sequence and blank row. A similar approach to egreg's uses array's w{<align>}{<width>} to set the column width to the widest element:

enter image description here

\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}
Werner
  • 603,163
  • Thanks for the reply! When I try compiling it produces illegal pream tokens on the "w" and on the "\boxCountwd" in the tabular. I don't know how to fix it. Working in Overleaf. Really appreciate your help! – Phlemp Jan 05 '19 at 14:25
  • @Phlemp: Did you add \usepackage{array} to your preamble like I did? Probably not. You don't have to use w{c}{..} as a column specification. It just aids in making the column widths the same if you have 10 or more columns. Alternatively, just use c, l or r. – Werner Jan 05 '19 at 17:22
2

With \foreach in TikZ:

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

enter image description here

CarLaTeX
  • 62,716