# Generating tables with \@for command

I'm making a report template that requires the list of authors to be presented in several different ways, one of which is a table that lists each author and provides a space for them to sign the printed version of the document.

I have an author list defined as

\newcommand{\reportAuthors}{Bob Jones,Sally Smith,Humpty Dumpty}


and am attempting to generate a table as follows:

\begin{tabular}{|b{4cm}|b{6cm}|b{4cm}|}
\hline
\multicolumn{3}{l}{\begin{Large}\textbf{Signatures}\end{Large}} \\ \hline
Approved by: & & Bob Jones \\ \hline
Approved by: & & Sally Smith \\ \hline
Approved by: & & Humpty Dumpty \\ \hline
\end{tabular}


I've attempted this with the \@for command as follows:

\begin{tabular}{|b{4cm}|b{6cm}|b{4cm}|}
\hline
\multicolumn{3}{l}{\begin{Large}\textbf{Signatures}\end{Large}} \\
\hline
\makeatletter
\@for\authname:=\reportAuthors\do{%
Approved by: & & \authname \\
}
\makeatother
\end{tabular}


but this produces an error that \authname is an undefined control sequence. If I remove the & & so that there's no columns, the error goes away and it properly iterates and inserts each name... but of course that leaves me without the names in the right column as is required.

If I put the \authname in the first column (not that this is what the template should have, but just to test things), then instead of the undefined control sequence I get 'Incomplete \ifx'.

So firstly, what is causing \authname to be undefined if I insert the & characters? and secondly, how can I get around this?

Update: I've tried combining some info found elsewhere to try doing as Martin suggested in terms of trying to collect the rows outside the tabular environment:

\newcommand{\tablecontent}{}
\makeatletter
\@for\signame:=\reportAuthors\do{%
\g@addto@macro\tablecontent{Approved by: & & \signame \\ \hline }%
}
\makeatother

\begin{tabular}{|b{4cm}|b{6cm}|b{4cm}|}
\hline
\multicolumn{3}{l}{\begin{Large}\textbf{Signatures}\end{Large}} \\
\hline
\tablecontent
\end{tabular}


Which fixes the error, but results in empty cells in the third column (where \signame should be). I suspect this is due to my lack of understanding about expansion, and by the time it's getting expanded, the \signame definition is out of scope. Fiddling with \edef hasn't really helped so far, but perhaps someone can point out some flaws...?

-
The \signame is not expanded by the \g@addto@macro. You can use \xappto from the etoolbox package which expands its content before it is appended. Then you should protect \ and \hline with \noexpand. – Martin Scharrer Apr 12 '11 at 7:10

The error might be caused because of the special way & are treated inside a tabular (actually in the internal used \halign). The \@for loop might be executed in a different column, read group, than the \authname.

It would be saver if you collect the rows into a macro outside the tabular first. This is quite easy with the etoolbox package which is also able to loop over comma separated lists. You could also use its list-generating macro for \reportAuthors. See its manual for more details.

\documentclass{article}

\newcommand{\reportAuthors}{Bob Jones,Sally Smith,Humpty Dumpty}

\usepackage{array}
\usepackage{etoolbox}

\begin{document}

\begingroup
\newcommand\tablecontent{}
\def\do#1{\appto\tablecontent{Approved by: & & #1 \\}}%
\expandafter\docsvlist\expandafter{\reportAuthors}

\begin{tabular}{|b{4cm}|b{6cm}|b{4cm}|}
\hline
\multicolumn{3}{l}{\begin{Large}\textbf{Signatures}\end{Large}} \\
\hline
\tablecontent
\end{tabular}
\endgroup

\end{document}

-
I've not used e-TeX before and know almost nothing about it — is it just another \usepackage or do I need to change the way my documents are built? (i.e. replacing pdflatex). The documentation seems to imply the latter but I'm not sure I understand it fully. – drfrogsplat Apr 12 '11 at 3:56
@drfrogsplat: All modern LaTeX versions like pdflatex are based on eTeX. So you don't have to change anything. – Martin Scharrer Apr 12 '11 at 7:07
Thanks for this, both \xappto in your comment and this answer worked perfectly (and look much neater than the \@for and \g@addto@macro commands!) – drfrogsplat Apr 13 '11 at 2:21