12

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

1
  • 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. Apr 12, 2011 at 7:10

2 Answers 2

9

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}
4
  • 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. Apr 12, 2011 at 3:56
  • @drfrogsplat: All modern LaTeX versions like pdflatex are based on eTeX. So you don't have to change anything. Apr 12, 2011 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!) Apr 13, 2011 at 2:21
  • Is it possible to do this with newlines too? Feb 10, 2019 at 21:52
2

It's not necessary to construct the \tablecontent first and then print it. Instead, you can use \docvslist to add lines directly inside the tabular environment:

\documentclass{article}

\usepackage{array}
\usepackage{etoolbox}
\usepackage{booktabs}
\newcommand\AuthorList[1]{%
  \renewcommand*\do[1]{Approved by &&##1\\}
  \begin{tabular}{|b{4cm}|b{6cm}|b{4cm}|}
      \toprule
      \multicolumn{3}{l}{\begin{Large}\textbf{Signatures}\end{Large}} \\
      \midrule
      \docsvlist{#1}
      \bottomrule
  \end{tabular}
}

\begin{document}

    \AuthorList{Bob Jones,Sally Smith,Humpty Dumpty}

\end{document}

If you want to use a predefined list \reportAuthors as in the question then you'll need

\expandafter\AuthorList\expandafter{\reportAuthors}

or use \expandafter\docsvlist\expandafter{#1} in the definition of \AuthorList.

1
  • Is there a "\docsvlist" version for "\newline"? Feb 10, 2019 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .