5

I would like to generate an automatic table using loop. As suggested here before, it is best to first generate the lines in a toks and then pass them to tabular. However, I have problems with accessing the line counting variable inside the line as in the following example:

\documentclass{article}

\newcount\it
\newcount\tot

\newtoks\tablines
\def\addtomytablines#1{\tablines\expandafter{\the\tablines#1}}
\it=2
\tot=7
\loop
  \addtomytablines{\the\it&&&\\\cline{2-3}}
  \advance\it 1
\ifnum \it<\tot
\repeat

\def\maketable{%
  \begin{tabular}{r|p{7mm}|p{7mm}|p{5mm}} \cline{2-3}
    & foo & foo & \\ \cline{2-3}
    \the\tablines
    foofoo & & & \\ \cline{2-3}
  \end{tabular}
}

\begin{document}
\maketable
\end{document}

The desired output would be that each line number starts with the current line number (so from 2 to 6), however the output is output so always writes the last value of \it.

Any ideas on what to change to make it work? Simply changing the line inside the loop to

  \addtomytablines\expandafter{\the\it&&&\\\cline{2-3}}

gives the correct numbers but not a table anymore.

3 Answers 3

3

You have to expand the row number as far as you can before adding it to the \tablines token list. Here's one way of achieving that:

enter image description here

\documentclass{article}

\newcounter{it}
\newcounter{tot}

\newtoks\tablines
\newcommand{\addtomytablines}[1]{\tablines\expandafter{\the\tablines#1}}
\setcounter{it}{2}
\setcounter{tot}{7}
\loop
  \expandafter\addtomytablines\expandafter{\number\arabic{it} & & & \\ \cline{2-3}}
  \stepcounter{it}
\ifnum \value{it}<\value{tot}
\repeat

\def\maketable{%
  \begin{tabular}{r|p{7mm}|p{7mm}|p{5mm}}
    \cline{2-3}
    & foo & foo & \\
    \cline{2-3}
    \the\tablines
    foofoo & & & \\
    \cline{2-3}
  \end{tabular}
}

\begin{document}
\maketable
\end{document}

\expandafter the \number of \arabic{it} (LaTeX counter usage) expands the value of the it counter before passing it to \addtomytablines.

1
  • Thank you very much. Works fine, even when changing the counters back to counts...
    – senegrom
    Jan 22, 2015 at 1:56
2

You can do this with two counters, one inside the \addtomytablines command, and one for the main loop. (I don't fully understand why.)

I took the liberty of cleaning up your code and writing it in LaTeX.

\documentclass{article}

\newtoks\tablines
\def\addtomytablines#1{\tablines\expandafter{\the\tablines#1}}

\newcounter{row}
\newcounter{loop}
\newcounter{maxrow}

\setcounter{row}{2}
\setcounter{loop}{\value{row}}
\setcounter{maxrow}{7}

\loop
    \addtomytablines{%
        \therow & & & \\%
        \cline{2-3}%
        \stepcounter{row}
    }
    \stepcounter{loop}%
\ifnum\value{loop} < \value{maxrow}
\repeat

\def\maketable{%
  \begin{tabular}{r|p{7mm}|p{7mm}|p{5mm}} 
    \cline{2-3}
    & foo & foo & \\ \cline{2-3}
    \the\tablines
    foofoo & & & \\ \cline{2-3}
  \end{tabular}
}

\begin{document}
\maketable
\end{document}

enter image description here

4
  • I would like to avoid LaTeX, only TeX, as mentioned in the comment above.
    – senegrom
    Jan 22, 2015 at 1:45
  • 1
    If you want to use the Plain TeX format then you can't use \documentclass or tabular and you should use \newcount\it (still would be good to use unambiguous variable names) and \advance\it by 1. Jan 22, 2015 at 1:47
  • The algorithm is exactly the same, just different syntax in Plain TeX. \newcount\row \row=2 \advance\row by 1 vs. \newcounter{row} \setcounter{row}{2} \stepcounter{row}. Jan 22, 2015 at 1:50
  • Yes thank you very much. Got it now. The other solutions seems to need only one counter though.
    – senegrom
    Jan 22, 2015 at 1:57
1

The LaTeX kernel has some non publicized programming tools, such as while loops. The more well-know \loop is not appropriate here inside the tabular for a number of reasons, one being that it makes a local definition which will get lost, the other one is that \cline also uses \loop and this would clash.

Here is a one-liner to get user access to one LaTeX while loop.

\documentclass[border=12pt]{standalone}

\newcounter{index}

\makeatletter
   \def\WHILENUM #1{\@whilesw\ifnum#1\relax\fi}%
\makeatother

\begin{document}

\begin{tabular}{r|p{7mm}|p{7mm}|p{5mm}} 
\cline{2-3}
    & foo & foo & \\ 
\cline{2-3}
\setcounter{index}{2}%
\WHILENUM 
 {\value{index}<7}
 {\theindex\stepcounter{index}&&&\\\cline{2-3}}%
    foofoo & & & \\ 
\cline{2-3}
\end{tabular}

\end{document}

Blockquote

You must log in to answer this question.

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