TeX \count Register Arithmetic: Increment Safely Without Breaking Your Compile
TeX \count registers let you embed integer arithmetic directly in your typesetting, but the assignment syntax, 32-bit limits, and per-run scope require attention. A minimal example and diagnostic checks help you avoid silent overflow and unexpected resets.
16 Jun 2026, 14:20 UTC

The = Assignment Operator vs. Explicit Addition
In TeX, \count0=\count0+1 assigns the result of the addition on the right-hand side. The right-hand side is always evaluated before the value is stored, so if \count0 currently holds 5, after this line it becomes 6. There is no separate increment operator; you write the arithmetic explicitly. The = symbol is TeX's assignment token, and it triggers the assign command, which evaluates the expression and stores the result. This means \count0=1 is a valid simple assignment, while \count0=\count0+1 performs arithmetic and then assigns.
Scope and Persistence
\count registers reset to zero at the start of every TeX run. Within a single run, you can share a register across input files with \input or \include; the value persists until the run ends. If you need a counter that survives across runs, you'll need to write the value to an auxiliary file with \write or rely on an .aux mechanism; \count alone won't do it.
The 32-Bit Trap: Silent Overflow
Each \count register is a 32-bit signed integer, ranging from -2147483648 to 2147483647. Incrementing past 2147483647 wraps silently to -2147483648 without an error message. This can surprise users who expect an overflow trap. The safe way to inspect the current value is the primitive \showthe\count0; typesetting it with \the\count0 places the number in the output, while the terminal command shows the internal value.
A Minimal Working Example
\count0=1
Hello, count: \the\count0
\count0=\count0+1
After increment: \the\count0
Run this with pdflatex document.tex in your terminal. No special permissions are required beyond read access to the .tex file.
- After the first compile, the document should typeset Hello, count: 1 followed by After increment: 2.
- Start a second TeX run; \count0 resets to 0, so the output restarts at 1.
Add \showthe\count0 after any assignment and run the job. The terminal will print something like >1, confirming the register's current value.
Repeatedly incrementing \count0 beyond its maximum value causes a silent wrap-around. Always verify the register value with \showthe\count0 before relying on it in production pipelines.
If you need a counter that persists across runs, consider writing the value to an auxiliary file or using LaTeX's \newcounter which handles scope and persistence for you. The \count register is best suited for per-run, lightweight arithmetic where you control the lifecycle.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.