23

It seems that there are different ways of accessing counters. I have been using \theMyCount as in this post about Using counters or macros, but noticed that the arrayjobx package uses \the\value{MyCount}.

This post about LaTeX programming comparison operators uses just \value{MyCount} but that does not seem to compile for me.

Are there any differences between these different methods of accessing counters?

\documentclass{article}

\newcounter{MyCount}

\begin{document}

MyCount = \theMyCount

MyCount = \the\value{MyCount}

%MyCount = \value{MyCount}% Does not compile for me.
\end{document}
19

\the\value{MyCounter} is just a slightly faster way to say \arabic{MyCounter}, as the former expands successively to

\the\csname c@MyCounter\endcsname
\the\c@MyCounter

and the latter to

\expandafter\@arabic\csname c@MyCounter\endcsname
\@arabic\c@MyCounter
\number\c@MyCounter

TeX rules say that \the is essentially equivalent to \number. I don't think that the difference in execution time can be significant in a normal document. It's usually better to stick to "official" macros, when they exist, in order to be sure that the result will be stable over time.

Usage of \theMyCount may be improper when we are not sure what kind of representation is associated to the counter MyCount.

On the other hand, \theMyCount should not be used in contexts when we are interested in the "abstract" value stored in the counter, so putting in a macro

\ifnum\value{MyCounter}>0

is correct, while

\ifnum\theMyCounter>0

is dangerous, because the user might have changed the representation of MyCounter when the macro is used.

18

You should use \theMyCounter if you want to output the value in Arabic numerals, Roman numerals, symbols or other format. You can restyle \theMyCounter by

\renewcommand\theMyCounter{\roman{MyCounter}} % lowercase Roman numerals

You should use \value{MyCounter} if it is used in an expression. For example

\ifnum\value{MyCounter}>5 ... \else ...\fi
\ifthenelse{\value{MyCounter}<4}{...}{...} % ifthen package
\setcounter{AnotherCounter}{\value{MyCounter}}

You can use \the\value{MyCounter} the same as \arabic{MyCounter}. But it is a mixed syntax. You'd better always use \arabic{MyCounter} in LaTeX; and if you want to pry, it is the same as \the\c@MyCounter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.