Question
I'm new to LaTeX and I must say that I am really struggling with it. I discovered the \newcommand command that is kind of like a function/method in regular programming languages. You can give it arguments and everything.
I was wondering though, can I somehow iterate in LaTeX? Basically, what I would like to do is create a table with N+1 columns where the first row just contains a blank cell and then the numbers 1, 2, ..., N in the other columns. I only want to give N as an argument to this 'function' (newcommand).
Here is an example of something that might look like what I'm looking for (although obviously this won't work):
\newcommand{\mytable}[2]{ \begin{tabular}{l|*{#1}{c|}} % table with first argument+1 columns for(int i = 1; i <= #1; i++) "& i" % 'output' numbers in different columns \\\hline letters & #2 % second argument should contain actual content for row \\\hline \end{tabular} }
Call it with:
\mytable{3}{a & b & c}
Output should be:
| 1 | 2 | 3 | --------+---+---+---+ letters | a | b | c | --------+---+---+---+
Does anyone know if something like this is possible?
Thanks!
Solution
Auntie Google says yes.
OTHER TIPS
Just make the following into a new command and be sure to use package ifthen
.
\begin{tabular}{l|*{10}{c|}}
\newcounter{count}
\whiledo{\value{count}<10}{
\ifthenelse{\value{count}=0}{}{\the\value{count}}
\ifthenelse{\value{count}<9}{&}{\\}
\stepcounter{count}
}
letters&a&b&c&d&e&f&g&h&i\\
\end{tabular}
You can use the \loop
or \repeat
tokens. Or the multido
package.
Sure it's possible. You can also recur. eplain has iteration macros in it, see, eg, here.
Another possibility (if you're lazy like me) is perltex