Let's clarify that countdef\mycount=<number>
is a bad idea in LaTeX, then clarify appropriate usage of \newcount\mycount
vs \newcounter{mycount}
in LaTeX.
Background: I only started with TeX last year and before long I got tired of doing everything from scratch. Everyone surely knows what it's like reading The TeXBook. At any one time I had the book at my fingertips with around 10 or so cardboard bookmarks interspersed, jumping from bookmark to bookmark, referring to the index, adding more bookmarks, driving myself towards some solution or other.
I discovered countdef\mycount=<reg number>
on page 119, where Knuth just tells you how to use them without any warning. So, as a naive rookie I used them, my solution worked and I had no reason to look further. Sometime later, working in LaTeX, I dredged this out of my memory and got in to trouble using it. I discovered \newcounter{mycount}
and didn't look back.
Now that I'm looking into matters more deeply I need to raise the issue again. Back during my initial struggle to understand Plain-TeX, if I'd continued to read on to page 121, where Knuth explains the importance of \newcount
, I wouldn't have been asking the first part of my question now. The TeXBook is a vast and complicated book and I think raising this question is a useful awareness raiser of one of the "traps for young players" in delving into Plain-TeX.
Hopefully this explains how I originally fell foul of such a fundamental error and I'm pointing out that others could do the same. Also, as there are many times when using Plain-TeX in creating a LaTeX solution is useful, I'd really like to know the dos and don'ts of \newcount
vs \newcounter{}
in LaTeX.
Best Answer
It's a bad idea to do
in all flavors of TeX. No discussion allowed.
Plain TeX allocates 25 count registers; it can be checked from any
tex.log
(orplain.log
) on your machine. So, if a macro package saysTeX will execute internally
\countdef\foo=26
, but the actual number should never be used. So, assume you loadeplain
and some other macro package; they'll allocate new counters and, maybe, they'll use 56 of them. When you do\countdef\mycount=81
, any subsequent assignment to\mycount
will clobber the value the macro package is assuming the counter holds. Or a macro call will clobber the value you are counting on.Do
\newcount\mycount
(which works also in LaTeX, although it should be used carefully and only in package code) and no such problem will arise.For your information, the last counter allocated by LaTeX is number 78. Other eleven are allocated by
amsmath
. Do you see the problem? Counter 81 (ifamsmath
is the first package called) would be\classnum@
, which is a very important internal register foramsmath
. Chaos ensues.Let's tackle the ‘
\newcount
versus\newcounter
’ question. The former is a Plain TeX construct also imported by LaTeX, while the latter is LaTeX specific. With\newcounter{foo}[baz]
one creates a LaTeX counterfoo
that's added to the reset list of the counterbaz
; an example isthat's done by the
book
class. Adding the ‘parent’ counter is optional. After a\newcounter{foo}
declaration (with or without an optional argument), LaTeX defines\c@foo
via\newcount\c@foo
\thefoo
via\newcommand\thefoo{\arabic{foo}}
and also
\p@foo
(but describing this would be too technical).We can use the commands
\arabic{foo}
,\alph{foo}
,\Alph{foo}
,\roman{foo}
,\Roman{foo}
and\fnsymbol{foo}
for getting a representation of the counter's current value. Most importantly, we can useto change the current value. I typed the last command in evidence because it not only steps the counter, but also (locally) defines
\current@label
so that the next\label
command will reference this value (more precisely, the current representation of the value).In order to behave well with the
\label
-\ref
mechanism, all the above operations on the counter (using the allocated register named\c@foo
) are global.In the majority of documents, there's no need to do arithmetic with registers involving multiplications and divisions, so no
\multiplycounter
and\dividecounter
macros are defined at the user's level.In case a package needs to do arithmetic, TeX counters directly defined with
\newcount
are available for doing local or global computations with the primitives\advance
,\multiply
and\divide
. Several packages use TeX counters (I use this name as opposed to ‘LaTeX counter’ for those described above).Because of the inherent global nature of LaTeX counters, it's bad practice operating on them with
\multiply
or\divide
: operations on a register should be always local or always global. So\global\multiply\c@foo by 2
should be used in case one really needs to, which is quite rare, in my experience.