read and write commands to auxfile

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.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{graphicx}
\usepackage{environ}
\usepackage{etoolbox}

\begin{document}

%Don't overwrite \counti
\ifdefined\counti
TRUE
\else
FALSE
\newcount\counti
\counti=0
\fi

%make counti raise by 1
\the\counti

%write to aux-file
\makeatletter
\write\@auxout{\noexpand\newcount\counti}
\write\@auxout{\counti= \the\counti}
\makeatother

\end{document}


The output is

TRUE
0


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 '16 at 20:50
• @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. – MaestroGlanz Apr 17 '16 at 20:55

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. – MaestroGlanz Apr 17 '16 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:

where the value of the counter increments each time.

\documentclass{article}
\usepackage{refcount}\setrefcountdefault{0}
\usepackage{etoolbox}

\newcounter{TestCounter}

\AtEndDocument{% save counter value to aux file at end of document
\refstepcounter{TestCounter}\label{Ref:TestCounter}%
}
\AfterEndPreamble{% set counter using value saved in aux file
\setcounterref{TestCounter}{Ref:TestCounter}%
}

\begin{document}

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.)