1

I would like to add rows to a table with macros (as in the minimal example below). Therefore, I use the command \addtotable. The code works fine if I place the table after all the rows have already been assigned. However, I want to place the table containing all the assigned rows throughout the document at the beginning of the document, before any (or some) of the rows have been assigned (compare "Partial table" with "Full table" in the example below). How can this be done?

A minimal example is given below (see also Command to continually add to table with a command/macro)

    \documentclass{article}

\newcommand\foo{%
    \begin{table}[htp]
        \centering
        \begin{tabular}{ |c|c|c|c| }
            \hline
            Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
            \foorows
            \hline
        \end{tabular}
\end{table}}

\newcommand\foorows{}

\makeatletter
\newcommand\addtotable[1]{%
    \g@addto@macro\foorows{\@gobble}%
    \@for\tmp:=#1\do{%
        \expandafter\g@addto@macro\expandafter\foorows
        \expandafter{\expandafter&\tmp}%
    }%
    \g@addto@macro\foorows{\\}%
}
\makeatother

\begin{document}
    Partial table:
    \addtotable{A,A,A,A}
    \foo

    Full table:
    \addtotable{B,B,B,B}
    \foo 

\end{document}
Werner
  • 603,163

1 Answers1

2

enter image description here

this takes two runs of latex

    \documentclass{article}

\newcommand\foorows{}

\makeatletter
\newcommand\addtotable[1]{%
    \immediate\write\@auxout{\unexpanded{\g@addto@macro\foorows{#1\\}}}}
\makeatother

\begin{document}

full table

     \begin{tabular}{ |c|c|c|c| }
            \hline
            Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
            \foorows
            \hline
        \end{tabular}


add line one
    \addtotable{A&A&A&A}

add line two
    \addtotable{B&B&B&B}


\end{document}
David Carlisle
  • 757,742