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
\newcommand{\columnheader}[1]{%
% 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 ...
\columnheader{#1} \\ \midrule
\endhead
\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}
! 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