2

I'm trying to have a command to be executed at the begining of the document in order to append a value to a variable after it is being used. The idea is to combine it with Add Rows to Table With Macro in order to have the table row to be inserted after the table is used.

Since the \addtotable is called after the usage of \foorows the row is never appended to the table. Futhermore, the table should allow page breaking if not fitting in a page (I guess that using longtable will be enough).

I would like the following example to work:

\documentclass{article}

\newcommand\foorows{}

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

\begin{document}

\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}

\addtotable{R1C1,R1C2,R1C3,R1C4} \addtotable{R2C1,R2C2,R2C3,R2C4}

\end{document}

fr0zn
  • 51
  • This cannot work, because, as you say, \foorows is called at the time where the table is typeset, and at this time, it is empty. You only fill it later. Somehow, you need to tell LaTeX first to look for the \addtotable macros and only then start with the actual typesetting. But what exactly is the application here? Why do you want it to be this way? I cannot quite imagine why you would want to do something like this ... – Jasper Habicht Dec 11 '21 at 09:15
  • 1
    I'm using a predefined pandoc latex template that creates a table at the start of the document. The user using the template should be able to insert rows into that table – fr0zn Dec 11 '21 at 09:20
  • 1
    Would it be feasable that you place the table into another macro \printtable and the user executes this after having added all the rows? – Jasper Habicht Dec 11 '21 at 09:28
  • 1
    You would have to write something to a file so that the next compilation can read and use it. – Ulrike Fischer Dec 11 '21 at 09:51
  • Found an answer!

    https://tex.stackexchange.com/questions/420224/show-table-defined-by-a-macro-before-assigning-its-elements

    – fr0zn Dec 11 '21 at 11:21

0 Answers0