Problem with dynamic table creation

You can fix \repeatntimes like this:

\newcommand{\repeatntimes}[2]{ % usage:\repeatntimes{c}{3} -> ccc
   \newcount\i
   \newtoks\ans
   \loop
      \ans=\expandafter{\the\ans#1}
      %\showthe\ans% uncomment to watch this work
      \advance\i by 1
      \ifnum\i<#2\repeat
   \the\ans
}

Then

\newcommand{\sublabels}{\repeatntimes{blah &}{2}}
\begin{tabular}[t]{ccc}
\repeatntimes{blah &}{2} blah \\
\end{tabular}

gives you what you said you wanted.

But maybe there's something more elegant, depending on what you really want. Do you want a row of cells in a table to all be the same thing? Maybe you could read the column specification to find the correct n.


I'd use the expandable \prg_replicate:nn function from expl3:

\documentclass{memoir}
\usepackage{expl3}
\begin{document}
  \ExplSyntaxOn
  \newcommand*{\sublabels}{\prg_replicate:nn{2}{blah &}}
  \ExplSyntaxOff
  \begin{tabular}[t]{ccc}
  \sublabels blah \\
  \end{tabular}
\end{document}

You could of course take the code behind \prg_replicate:nn and implement it yourself without expl3 if you want (after all, it is ultimately just TeX programming). However, I'd go with the 'pre-packaged' version (not surprising, although I'd point out that \prg_replicate:nn pre-dates my involvement with LaTeX3 by a long way).