# What is the maximum integer that can be saved in a LaTeX counter?

I am using counters to save positive integers, something like that:

\setcounter{mycoutervalue}{59}


Is there any limit in the value I can save in a counter, in this case mycountervalue? And what is the exact limit?

Thanks! I do not want to get an unexpected overflow.

• I would say 2^31-1 with e-TeX extensions
– user31729
Oct 1, 2015 at 13:04
• Thaks for your quick answer! Well, I am not using e-TeX extensions... Oct 1, 2015 at 13:08
• @loved.by.Jesus are you sure? That would be quite strange nowadays. A modern LaTeX kernel assumes etex available, many packages do as well. Oct 1, 2015 at 13:22
• @loved.by.Jesus: I confused the e-TeX extensions with the register range. The register range hasn't change, but the numbers of registers have increased.
– user31729
Oct 1, 2015 at 13:28
• @clemens: I had no idea what e-TeX was :-\, thanks for your explanation. Oct 1, 2015 at 13:33

The maximum value is the 'usual' 2^31-1 long integer value, as well as the negative range from -2^31, so the full range is 2^32 integers possible.

2^31-1 is 2147483647, which is the largest possible integer usable for counters or \ifnum and \numexpr codes.

In the code below I stored this number to the counter \mycounter and print it several times after using \stepcounter. After the first \stepcounter the register overflows and the number is set to -2147483648, being the 'largest' negative number possible. A subsequent \stepcounter works normally then.

The e-TeX standard extended the limit of 256 registers (count, skip etc.) to 32568 possible registers (for each type)

\documentclass{article}

\newcounter{mycounter}

\begin{document}

\setcounter{mycounter}{2147483647}

\themycounter  % prints 2147483647

\stepcounter{mycounter}  % Now the overflow will occur

\themycounter % prints -2147483648

\stepcounter{mycounter}

\themycounter % -2147483647
\end{document}


• Hey that is a nice coded answer! You got it. Oct 1, 2015 at 13:13
• @loved.by.Jesus: You're welcome
– user31729
Oct 1, 2015 at 13:19

According to The TEXbook:

TEX has 256 registers called \count0 to \count255, each capable of containing integers between -2147483647 and +2147483647, inclusive; i.e., the magnitudes should be less than 231.

• A very minor detail: The minimal possible value of a counter is -2147483648=-2^{31}, not -2147483647. (There are 2^{32} integers between -2147483648 and +2147483647...)
– Mico
Oct 10, 2015 at 14:52
• @Mico does that mean The TeXBook has a typo? Oct 22, 2021 at 2:27
• @Symbol1 - Oh no, did I commit an act of heresy against the Book of K?!
– Mico
Oct 22, 2021 at 3:39
• I mean the other answer clearly shows that -21...48 is possible. So either TeXbook is wrong or the new compiler changed something. Oct 22, 2021 at 3:41
• @Symbol1 - Actually, there's no outright heresy. I wrote "The minimal possible value of a counter is -2147483648=-2^{31}, not -2147483647." However, while the minimal possible counter value, -2147483648, cannot be set directly, it can be reached by causing an overflow, as is demonstrated in the other answer. For sure, the Book of K has it right when it is said that the counter's "magnitude" (a synonym for "absolute value"?) should be less than 2^{31}. :-) Try running \setcounter{mycounter}{-2147483648} to verify that the Book of K is right.
– Mico
Oct 22, 2021 at 3:48