1

I would like to generate a global table with entries that can be added with a command. I based myself on this post. The issue I have is that I can only display the table after I added all its entries. I wold like to be able to insert the completed table before calling all entries.

What I did so far:

% beginning of global table
\newcommand\startGlobalTable{
    \def\GlobalTable{%
    \begin{longtable}{ ll }
        \hline
        Name & Year \\
        \hline
}}

% appending element to global table
\newcommand\addGlobalTable[2]{
    \edef\GlobalTable{\expandafter\unexpanded\expandafter{%
        \GlobalTable%
            {#1} & {#2} \\
        \hline
    }}
}

% display the global table
\newcommand\createGlobalTable{\begin{center}\GlobalTable\end{table}\end{center}}

Whit this setup, the global table is displayed at the end of my document:

\startGlobalTable
Some text some text
\addGlobalTable{Hello}{World}
Some different text
\addGlobalTable{Hello}{Moon}
Some final text
\createGlobalTable

However when writing something like this the table at the beginning is (obviously) empty:

\startGlobalTable
\createGlobalTable
Some text some text
\addGlobalTable{Hello}{World}
Some different text
\addGlobalTable{Hello}{Moon}
Some final text

How can I display the table in the beginning of the document containing all entries that will be added later?

Many thanks in advance for your answers!

6
  • 2
    well do it like the \tableofcontents or \listoffigures: write to some external file and then read that in at the next compilation. And be careful with \edef. That shouldn't be use for arbitrary text. Commented Feb 5, 2023 at 11:01
  • What determines the pair of arguments of each \addGlobalTable directive? Are the arguments static or dynamic? Put differently, is it crucial where in the document the \addGlobalTable{...}{...} directives occur?
    – Mico
    Commented Feb 5, 2023 at 11:12
  • Yes it is, the location where \addGlobalTable is called is crucial.
    – Pa Dalton
    Commented Feb 5, 2023 at 15:11
  • To Ulrike, I need more than \tableofcontents or \listoffigures, my actual \addGlobalTable is more complex than in the question
    – Pa Dalton
    Commented Feb 5, 2023 at 15:14
  • Probably you can use the package datatool for maintaining two databases in TeX's memory: One which is created by reading a .csv-file and which contains all data created in the previous latex-run and which is used for obtaining data in the current run. Another one which is used for adding data and which is written to .csv-file at the end of the latex-run. This way you can have TeX/datatool-macros iterate on the database for reading for creating tables/longtables wherever you like. ... Commented Feb 9, 2023 at 22:54

0

You must log in to answer this question.

Browse other questions tagged .