I have a problem with the aux-file. According to some posts here on Tex-Exchange, the aux-file is read at the beginning of the document (is this at \begin{document} or \documentclass? I wanted to built a test document. For every run it should increase the number that it is printed on the paper.



%Don't overwrite \counti

%make counti raise by 1
\advance\counti by1

%write to aux-file
\write\@auxout{\counti= \the\counti}


The output is


This proves, that it reads the newcount from the aux-file. But unfortunately, the value stays 0. Actually it should raise by one by every run.

The aux-file says

\newcount \counti 
\counti = 1

after every run.

  • It's read before \begin{document} -- is this TeX count register stuff really necessary. Have a look on xassoccnt package and it's numberofruns counter
    – user31729
    Apr 17, 2016 at 20:50
  • 1
    @ChristianHupfer I want to write and read two numbers. The number of pages and a custom number. I need them at the beginning of the document. This document has been for tests, to see how things work. If this example works, the actual problem would be solved also. I could use a custom aux-file, but this seems a bit too much for just two numbers. Beside of that I'd like to learn more about Latex. Apr 17, 2016 at 20:55

2 Answers 2


When LaTeX reads the .aux file, then it reads it inside a local group. \newcount assigns the count register globally, but the assignment \counti=... is local like LaTeX's \setlength but unlike LaTeX's \setcounter. Therefore the problem can be fixed by a global assignment:

\write\@auxout{\global\counti= \the\counti\relax}

(I have added \relax. Then TeX will not parse beyond to find digits.)

  • Thanks. The \relax makes sense, because of the \the, or doesn't it. Apr 17, 2016 at 21:12

Here is a more "latexy" way of doing this using Heiko's refcount package. The idea is to save the value of the counter to the auxfile, as a label, and then read it back in each time the file is compiled using \setcounterref.

The output of the MWE below takes the form:

enter image description here

where the value of the counter increments each time.



\AtEndDocument{% save counter value to aux file at end of document
\AfterEndPreamble{% set counter using value saved in aux file


Counter is \arabic{TestCounter}



I have automated this using \AtEndDocument and \AfterEndPreamble commands from the etoolbox package. (This is just for proof-of-concept. There are better ways of writing to the auxfile that avoid decrementing and then incrementing the counter.)

You must log in to answer this question.

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