# Dynamic table column generation

I want to generate table columns dynamically but I have no clue how to tell LaTeX to accept or evaluate & as a column separator.

As you can see in the attached code, the problem is with \@for \el:=#1\do{\textbf{\el} &}, where & should separate the columns and the column titles. If I remove the & char, I get the whole string in the first column. That makes sense to me, because table cells are separated with &. How can I make it work with separate columns?

\documentclass{article}

\usepackage{longtable}
\usepackage{booktabs}
\usepackage{forloop}
\usepackage{xstring}

\makeatletter

% http://tex.stackexchange.com/a/107298
\newcommand*{\commalength}[1]{%
\StrCount{#1,}{,}%
}

% http://tex.stackexchange.com/a/87420
% if i remove the "&" it works as a whole string but not splitted in columns
% HERE IS THE BUG WITH THE "&"
\@for \el:=#1\do{\textbf{\el} &}%
}
\makeatother

\newcommand{\generatecolumns}[1]{

% getting the column count automatically would be really nice like \commalength{#1}
\multicolumn{5}{l}{\emph{Continuation of \tablename\ \thetable{}}} \\
\toprule
% the column titles should be generated here ...

\hline
\multicolumn{5}{l}{\emph{Continued on next page}} \\
\bottomrule
\endfoot
}

\begin{document}

\begin{longtable}{lllll}

% this are the column titles
\generatecolumns{Column1,Column2,Column3,Column4,Column5}

\newcounter{i}
\forloop{i}{1}{\value{i} < 100}{
\arabic{i} & a & b & c & d \tabularnewline
}

\end{longtable}

\end{document}


The timing in alignments can be tricky, the main problem here is that & isn't just an alignment mark it is a TeX group like {} so if you evaluate the loop within the table the group started in one iteration of the loop ends in the next, and most loop macros can't cope.

It is usually easier to construct the entire row in a token register or macro.

As you are using LT, it counts the columns already, so you don't need to.

\documentclass{article}

\usepackage{longtable}
\usepackage{booktabs}
\usepackage{forloop}
\usepackage{xstring}

\makeatletter

% http://tex.stackexchange.com/a/87420
% if i remove the "&" it works as a whole string but not splitted in columns
% HERE IS THE BUG WITH THE "&"
}}

\newcommand{\generatecolumns}[1]{

% getting the column count automatically would be really nice like \commalength{#1}
\multicolumn{\LT@cols}{l}{\emph{Continuation of \tablename\ \thetable{}}} \\
\toprule

\hline
\multicolumn{5}{l}{\emph{Continued on next page}} \\
\bottomrule
\endfoot
}

\makeatother
\begin{document}

\begin{longtable}{lllll}

% this are the column titles
\generatecolumns{Column1,Column2,Column3,Column4,Column5}

\newcounter{i}
\forloop{i}{1}{\value{i} < 100}{
\arabic{i} & a & b & c & d \tabularnewline
}

\end{longtable}

\end{document}

• Hi David, sorry for my late response. I have been out of home. This is totally awesome and works perfect!!! Thank you so much - also for the good explanation! – hetsch Feb 18 '14 at 16:13
• I have seen that you are also the creator of ltxtable. May I ask you for further help. If I'm using ltxtable with a custom environment and trying to set the linespacing with the setspace package it throws an error ! Missing \endgroup inserted. <inserted text> \endgroup. A code sample would be at link. If you are having time to quickly check that code, that would be a great help for me! – hetsch Feb 18 '14 at 18:47
• @hetsch ltx tries to grab the whole environment body so like tabularx, or ams alignments there are restrictions on using the environment inside the definition of another env. by redefining more internal code that could be worked round, but to be honest, I wouldn't, the thing is fragile enough already. Just use longtable explictly – David Carlisle Feb 18 '14 at 18:56
• All right - I think I got it. Once again thank's for your help and the wonderful packages! – hetsch Feb 18 '14 at 19:03