# Using counters or macros

Should I use counters or macros to store numerical data? Is there a limitation on a number of counters that can be used at the same time or other considerations?

I would also like to perform simple arithmetic on the data -- I know how to do that with counters.

## Example

Using a macro:

\def\minnumber{24}
...
\ifnum\currentnumber<\minnumber\xdef\minnumber{\currentnumber}\fi


Using a counter:

\newcounter{minnumber}
\setcounter{minnumber}{24}
...
\ifnum\currentnumber<\theminnumber\setcounter{minnumber}{\currentnumber}\fi

-

Using counters has some advantages in terms of logical meaning. It also shows improved robustness in some cases: there are places where the fact that counters are terminated properly is important. For example, try

\documentclass{article}
\begin{document}
\newcounter{demo}
\setcounter{demo}{10}
\newcounter{minnumber}
\setcounter{minnumber}{1}
\ifnum\value{demo}>\value{minnumber}11 correct\else oops\fi
\end{document}


versus

\documentclass{article}
\begin{document}
\newcommand*\demo{10}
\newcommand*\minnumber{1}
\ifnum\demo>\minnumber 11 correct\else oops\fi
\end{document}


This happens because a number stored in a macro can be 'partial', and so TeX will keep looking for the end of the number after the macro. This does not happen with counters, which TeX considers as 'complete'.

One thing to watch is that LaTeX counters are always set globally:

\documentclass{article}
\begin{document}
\newcounter{demo}
\begingroup
\setcounter{demo}{10}
\endgroup
\the\value{demo}
\end{document}


This effect also means that the performance (speed of execution) with counters may be better than with macros.

If you want a local value, either use a macro or use the plain TeX count register type:

\documentclass{article}
\begin{document}
\newcount\demo
\begingroup
\demo10\relax
\endgroup
\the\demo
\end{document}


(TeX count registers have a different syntax to LaTeX counters, as you can see.)

-
I agree with Joseph and when I say "if you can" it was to say that you need to look in which case you are. There is also perhaps a difference between a simple document and a package. There are some complex packages that use a lot of counters. –  Alain Matthes Jun 16 '11 at 15:12
But using \ifnum\demo>\minnumber\relax 11 correct\else oops\fi in the second example will make things right, right? Is that what you meant with "a number stored in a macro can be 'partial'"? –  Gonzalo Medina Jun 17 '11 at 2:36
@Gonzalo: Yes, you can insert a \relax here, although in some cases this may not be desirable (it will not 'disappear' in an \edef). When TeX inserts a macro, it simply replaces the macro name with the contents. So \demo 1 is turned into 101 by TeX: TeX does not stop looking for digits just because it's expanded \demo. On the other hand, TeX does not expand counters but uses them directly as values. –  Joseph Wright Jun 17 '11 at 6:12

Yes you have a limit of counters (256) but you can use \usepackage{etex} to add some counters (65536) I think but not sure). I think it's preferable to use macros if you can .

-
Don't forget \chardef and \mathchardef that are very useful: after \chardef\xyz=100 it's possible to use \xyz wherever TeX expects a number and, being non expandable, frees from termination problems. However \chardef stores numbers from 0 to 255, and \mathchardef up to 0x7FFF=32767. With LuaTeX and XeTeX, \chardef can be used for 21 bit numbers (that is up to 2097151). –  egreg Jun 16 '11 at 14:54
Interesting ! thanks, I never use this trick –  Alain Matthes Jun 16 '11 at 15:04
@egreg: Indeed they are - in expl3 constant positive integers are created using \chardef or \mathchardef 'behind the scenes. (See the implementation of \int_const:Nn.) –  Joseph Wright Jun 16 '11 at 15:21