I need to store and use a large amount of integers (a little bit of "programming with TeX"), but I know that TeX has a limited (up to 256) number of counters that can be used simultaneously. How to work with these numbers using macros? I need basic numerical operations and comparison.

  • 2
    Related (duplicate?) question: Using counters or macros.
    – Alan Munn
    May 15, 2012 at 17:33
  • 1
    I agree with @AlanMunn that this seems to be a duplicate. Note that you can (re-)define "constants" using \chardef and \mathchardef which act basically like counters without needing a register. You can't add to them like with counter, however. May 15, 2012 at 17:41
  • 1
    @AlanMunn: I read the topic you mentioned, but I still did not get an answer to my question, that is, how to use integers stored in macros as if they were numbers stored in counters. May 15, 2012 at 17:42
  • 1
    Could you explain in more detail what exactly you need? You can define integer "constants" with \mathchardef\myconstant=<integer>. With e-TeX you can use e.g. \mathchardef\myotherconstant=\numexpr \myconstant + 1 \relax to add one to this "constant", etc., but \myconstant=1 doesn't work. A benefit is that it is a terminated number and TeX will not expand further tokens to look for more potential digits like it does with macros. May 15, 2012 at 17:49
  • @MartinScharrer: Ok, I will try to use \mathchardef. Maybe I was not carefull enough when reading the ralated post, this may be consider as a duplicate. May 15, 2012 at 17:54

1 Answer 1


There is no real limitation nowadays if you use pdftex (or pdflatex), because the engine has 32768 count registers. The only problem is to persuade the allocation macros to use the whole set. In LaTeX this is obtained by loading (as early as possible, that is, immediately after \documentclass) the etex package.

If you use constants less than "8000 = 32768, they can be conveniently stored with \mathchardef:


allows you to use \myconst in every place where TeX is expecting a <number> (as part of its syntax rules).

Note. A trick such as

\mathchardef\myconstant=\numexpr \myconstant + 1 \relax

doesn't work. This is because of a security precaution taken by TeX during assignments. When one says


the token \myconstant is temporarily set to \relax, so it can't be used in the following \numexpr. In that case one has to use a temporary storage bin:


However, such computations are rather inefficient.

  • 1
    +1 Could you also add an calculation example like the one in my comment above? May 15, 2012 at 17:54
  • @MartinScharrer Did you try it? You can't use it in that way.
    – egreg
    May 15, 2012 at 18:04
  • No, I didn't actually tried it. It's funny. Apparently the macro definition is already changed (\relax'ed?) before the right-hand side is evaluated, which is very unusual. Anyway, \mathchardef\temp=\numexpr \myconstant + 1 \relax \mathchardef\myconstant=\temp works fine. May 15, 2012 at 18:12
  • :-) I should reload the post before adding comments ... May 15, 2012 at 18:14

You must log in to answer this question.

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