Dynamic table column generation

1,261

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
\newcommand{\columnheader}[1]{%
 \gdef\thisheader##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{\protected@xdef\thisheader{\thisheader&\el}%
}}



\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
\noalign{\columnheader{#1}}\thisheader \\ \midrule
    \endhead

    \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}
Share:
1,261

Related videos on Youtube

hetsch
Author by

hetsch

Updated on August 01, 2022

Comments

  • hetsch
    hetsch over 1 year

    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}
    
  • hetsch
    hetsch over 9 years
    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
    hetsch over 9 years
    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!
  • David Carlisle
    David Carlisle over 9 years
    @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
  • hetsch
    hetsch over 9 years
    All right - I think I got it. Once again thank's for your help and the wonderful packages!