I've wandered into uncharted territory with a package I'm writing. At one point I need to create a table of 'n' columns (n supplied by user in doc). Creating a variable in the preamble of the tabular declaration will handle the number of rows and the justification, but I can't seem to figure out how to create a row of labels with 'n' cells. Hopefully this MWE example will clarify (I've massively simplified my code to focus on, what I think, is the problem). This will generate the error.
\documentclass{memoir}
\begin{document}
\ newcounter{n}
\newcommand{\repeatntimes}[2]{ % usage:\repeatntimes{c}{3} -> ccc
\setcounter{n}{0}%
\loop\addtocounter{n}{1}{#1}%
\ifnum\value{n}<#2\repeat}
\newcommand{\sublabels}{\repeatntimes{blah &}{2}}
\begin{tabular}[t]{ccc}
\sublabels blah \\
\end{tabular}
\end{document}
I need \sublabels to expand such that what gets typeset is
\begin{tabular}[t]{ccc}
blah & blah & blah \\
\end{tabular}
I know that this is an expansion problem, but there's another problems also, which is that the '&' is one of the 10 special characters that do funny things. The error message is
! Missing } inserted.
<inserted text>
}
I recall Knuth talking about this in the TeXbook, but I spend a long time today trying to find it and couldn't. I've tried all sorts of ways to sequence the expansion, but none work, I think because of the way '&' functions, but I'm not sure.
I would appreciate any ideas. TIA