[Tex/LaTex] Using counters or macros

countersmacros

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

Best Answer

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

Related Question