0

TLDR; Is there a way I can use the final value of a variable at the beginning of a document?

I want to automatically generate a report by simply adding textblocks. Every textblock-occurence increases the according counter, which works pretty fine. For example, [cat, dog, bird] textblocks can be created. The variables cnt_dog, cnt_cat, cnt_bird are defined in advance and are increased by using \newcommand{\catbox}{\addtocounter{cnt_cat}{1}}.

Now the problem: When I access the variable (\arabic{cnt_cat}) at the end of the document (using TeX-Studio) everything works fine, but when I access the variable at the beginning of the document, the value is 0. Seems the parser does not update the previously used value in the end :/

Cheers, Andi

dzr
  • 1

1 Answers1

1

you can use, at the end

\immediate\write\@auxout{\string\setcounter{cnt_cat}{\arabic{cnt_cat}}}

(with @ a letter)

then the .aux file will have something like

\setcounter{cnt_cat}{5}

so the counter will be set to 5 at \begin{document} on the next run.

David Carlisle
  • 757,742