Problem with dynamic table creation

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}%
\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

-

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
\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.

-
@Matthew: Nice! The third blah comes because \sublabels doesn't take an argument, so it's just printed out as well. –  Stefan Kottwitz Nov 20 '10 at 13:30
@stefan: of course! Should have seen that. –  Matthew Leingang Nov 20 '10 at 14:17
@ Matthew - thx for your help. Looks like just what I need.I'll be able to try it with the rest of the code later on today and I'll get back to you. BTW, what I 'really' want is to make one command which will label an array of figures. So, instead of 'blah' in my example, I have \parbox{\fa@labelwidth}{\hfill (\alph{fa@labelnum}\addtocounter{fa@labelnum}{1})\hfill}. The real \sublabels also handles the last cell entry with the \\. So if the user want to have the images n his array labeled (a), (b)....(N), he just issued one command on the next row. –  bev Nov 20 '10 at 23:47
This is part of a collection of 'beginner' packages that I'm writing for total newbies. Doing some things, like making arrays of figures is hard for some, even with (especially with) the available packages. My package, while not being very versatile, is dead easy and does enough (like scaling the entire array, making different numbers of columns in each row, labeling, and some other stuff) so that newbies can use it with no problems. BTW, if you ever examine this package, your code will be changed to latex from tex. I made this decision 2 years ago for heuristic purposes in my lab. –  bev Nov 20 '10 at 23:51
For those who may want to use Matthew's code there's an annoying bug that you wouldn't notice unless you put it in a table like I did. There is an extra space before the first item in the repeated string and only the first item. So it isn't properly centered in a center justified cell. The fix is to put a comment char '%' after {\the\ans#1} on line 5 of '\repeatntimes' –  bev Nov 24 '10 at 6:53

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).

-
I'd add that if anyone wants a full explanation of \prg_replicate:nn then I think it would be a separate question :-) –  Joseph Wright Nov 20 '10 at 14:38
@Joseph - Well, now you've done it. When I got to this site I was dismayed at learning that some of the LaTeX3 folk were here. I've spend a decade avoiding LaTeX3 like the plague for the same reason I've avoided learning to play the piano. I desperately wanted to pursue it, but decided that I couldn't afford the time investment. A hard but necessary decision. Now I feel impelled to investigate your solution to my question, and a decade of disciple is out the window. Blerk. –  bev Nov 21 '10 at 0:22
Oh, I have to say that, at this point I'll be using Matthews solution for my package, for the simple reason that I won't use code I don't understand. But I will be looking at expl3 and future versions might include LaTeX3 code (hurt to say that :-) ) –  bev Nov 21 '10 at 0:27
@bev: Here, all that I'm using expl3 for is a pre-built version of a known approach to replicating items: the same idea has been in TUGBoat, for example. So there is nothing 'new' in this, except the category codes used (no @ as a letter) and the fact that I'm not coding this function myself. –  Joseph Wright Nov 21 '10 at 8:09
@Joseph - OK, I hear you. I spend a bit of time yesterday looking at expl3. I'm still a little bit confused as to why your solution is working. You changed some catcodes, but not catcode 4 (=> &) which I would have expected. So, I'm still working my way through it. –  bev Nov 22 '10 at 18:36