4

I want to provide author-usable macros in my package to store data that will later appear in a table, but it would be nice to allow authors to define the data in that table at any time, even after the table is created and rendered.

Can I store data at any point in the document, and still retrieve all of it at any location? Another way to ask this is: Is there some form of deferred processing such that I can do this (somewhat pseudo-code):

\adddata{some data}
\adddata{more data}

\maketable

\adddata{even more data}
\adddata{additional data}

such that the table, with all data (not just those stored via calls to \adddata prior to calling \maketable), will correctly contain all of those data, regardless of where in the document \maketable was called?

I know this is partially possible with clever use of command renewal and such, but once TeX finds and renders the table, isn't it too late to change its contents? I should clarify again that the table must appear in the document where \maketable is called.

Note: Assume that \adddata and \maketable are defined in a class or package that handles all of the internals of storing the data and formatting them in the table.

3
  • 4
    You have to use the .aux file. But some more information about \adddata and \maketable is needed.
    – egreg
    Oct 22, 2015 at 22:17
  • 1
    See the answers to this question here which is related to the ToC, but can be used for other data as well: tex.stackexchange.com/questions/273098/…
    – user31729
    Oct 22, 2015 at 22:23
  • 1
    Note that if you write to an external file, it will take at least a second run for the data to be updated in the table. That is, changes won't affect the rendering of the table on this run, but they will affect it on the next run.
    – cfr
    Oct 22, 2015 at 22:23

1 Answer 1

3

It's not possible to change something that has already been typeset.

However, you can use a two pass approach: during a run of LaTeX, \adddata writes on the aux file and \maketable uses the data gathered in the previous run.

Here's a proof of concept. When an \adddata is inserted anew or data is modified, a warning like

LaTeX Warning: Rerun for \maketable on input line 51.

appears on the console.

\documentclass{article}
\usepackage{atveryend}

\makeatletter
\newcommand{\maketable}{%
  \begin{center}
  \begin{tabular}{cc}
  \maketable@body
  \end{tabular}
  \end{center}
}
\newcommand{\adddata}[1]{%
  \@bsphack
  \protected@write\@auxout{}{\string\maketable@data{#1}}%
  \@esphack
}
\newcommand{\maketable@data}[1]{%
  \g@addto@macro\maketable@body{#1\\}%
}
\def\maketable@body{}% initialize
\AtBeginDocument{\global\let\maketable@body@startup\maketable@body}
\AtEndDocument{%
  \global\let\maketable@body@end\@empty
  \def\maketable@data#1{\g@addto@macro\maketable@body@end{#1\\}}%
}
\AtVeryEndDocument{%
  \ifx\maketable@body@startup\maketable@body@end\else
    \@latex@warning{Rerun for \string\maketable}%
  \fi
}
\makeatother

\begin{document}

Start accumulating data

\adddata{a & b}
\adddata{c & d}

Here we print the table

\maketable

And we have new data later

\adddata{1 & 2}
\adddata{3 & 4}
\adddata{5 & 6}
\adddata{7 & 8}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .