0

I'd like to generate an empty table, with underlines in the cells for someone to fill out by hand. I can generate a blank table row with a loop without issue, but once I try to add some cmidrules to the row, the behaviour does not go as expected. My loop seems to add the very last cmidrule, but I actually overwrite it with the last expression in the genrow command.

The image below shows what I'm trying to generate, the red lines are what'd missing but what I expect to draw.

enter image description here

\documentclass{scrartcl}

\usepackage{booktabs}


\newcounter{iter}  \setcounter{iter}{0}
\newtoks\tabRtok  % token for table row
\newcommand\addtabRtok[1]{\tabRtok\expandafter{\the\tabRtok#1}}
\newcommand*\resettabRtok{\tabRtok{}}
\newcommand*\printtabRtok{\the\tabRtok}

\newcounter{itb}  \setcounter{itb}{0}


\newcommand{\genrow}[1]{  % generates a row
    \newcounter{cols}
    \setcounter{cols}{#1}
    \addtocounter{cols}{-1}

    \resettabRtok
    \setcounter{iter}{0}
    \loop\ifnum\theiter<\value{cols}
        \stepcounter{iter}
        \addtabRtok{. & .}
    \repeat
    \addtabRtok{\\}

    \addtabRtok{\cmidrule(r){1-1}}
    \setcounter{iter}{2}  %
    \loop\ifnum\value{iter}<#1
        \addtabRtok{\cmidrule(lr){\the\value{iter}-\the\value{iter}}}  % THIS ONLY GENERATES cmidrule on last column...?
%       \addtocounter{iter}{1}  % this doesn't work
        \stepcounter{iter}
    \repeat
    \addtabRtok{\cmidrule(l){#1-#1}}    
}

\begin{document}

\genrow{4}
\begin{tabular}{llll}
    \toprule
    \printtabRtok
    \bottomrule
\end{tabular}

\end{document}
2
  • Your problem is that \the\value{iter} is not expanded when you add it to your token register.
    – Skillmon
    Commented Feb 4, 2020 at 22:28
  • How do I expand it? Commented Feb 4, 2020 at 22:37

1 Answer 1

1

You have to expand \the\value{iter} when you add it to your token register, else it will contain something like the following for each column except the first and last one:

\cmidrule(lr){\the\value{iter}-\the\value{iter}}

And the value of iter will be 4 (in the example) when it will actually be expanded. The following just adds a small expansion helper macro (I called it \addtabRtokhelper) that gets the expanded \the\value{iter} as its argument and adds the \cmidrule to the tokens.

There are a few other issues with your code that I fixed.

  • Don't do \newcounter{cols} inside a macro that is executed multiple times, it will throw an error on second usage, and in plainTeX's equivalent syntax the counter would be created each time and take up a register (that's not a big deal nowadays as we usually have plenty registers, but back in the days TeX was pretty limited here)

  • Don't leave spaces at the end of lines inside a macro definition (or empty lines), this will add spurious spaces or \par tokens. If you need an empty line to get more overview, add a % in it, too. Exceptions are spaces at the end of number arguments, as they end TeX's parsing for numbers (see the \ifnum lines in the code).

The code:

\documentclass{scrartcl}

\usepackage{booktabs}


\newcounter{iter}  \setcounter{iter}{0}
\newtoks\tabRtok  % token for table row
\newcommand\addtabRtok[1]{\tabRtok\expandafter{\the\tabRtok#1}}
\newcommand*\resettabRtok{\tabRtok{}}
\newcommand*\printtabRtok{\the\tabRtok}

\newcommand*\addtabRtokhelper[1]{\addtabRtok{\cmidrule(lr){#1-#1}}}

\newcounter{itb}  \setcounter{itb}{0}

\newcounter{cols}


\newcommand{\genrow}[1]{% generates a row
    \setcounter{cols}{#1}%
    \addtocounter{cols}{-1}%
    \resettabRtok
    \setcounter{iter}{0}%
    \loop\ifnum\the\value{iter}<\value{cols}
        \stepcounter{iter}%
        \addtabRtok{. & .}%
    \repeat
    \addtabRtok{\\}%
    \addtabRtok{\cmidrule(r){1-1}}%
    \setcounter{iter}{2}%
    \loop\ifnum#1>\value{iter}
        \expandafter\addtabRtokhelper\expandafter{\the\value{iter}}%
        \stepcounter{iter}%
    \repeat
    \addtabRtok{\cmidrule(l){#1-#1}}%
}

\begin{document}

\genrow{4}
\begin{tabular}{llll}
    \toprule
    \printtabRtok
    \bottomrule
\end{tabular}

\end{document}

enter image description here

You must log in to answer this question.

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