# Dos and Don'ts of \countdef\mycount=<number> and \newcount vs \newcounter{} in LaTeX

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.

• You should use \newcount\mycount. Or even better \newcounter{mycount} which creates a counter with name \c@mycount. – yo' Nov 7 '13 at 0:09
• Doing \countdef\mycount=81 is a really bad idea also in Plain TeX. – egreg Nov 7 '13 at 0:12
• @tohecz I have a little picky remark (hope that you don't get it wrong): the counter is still called 'mycount'. \c@mycount is the internal count register. – Ruben Nov 7 '13 at 0:14
• @tohecz I said in the OP that I discovered \newcounter{} already. My question is about illuminating the whys and wherefores. – Geoff Pointer Nov 7 '13 at 0:38
• @Ruben So, what are the dos and don'ts of the capital letter when talking about Plain TeX? Many people write Plain-TeX when referring to the layer that Knuth wrote over the top of the TeX primitives. That is what I'm referring to here. – Geoff Pointer Nov 7 '13 at 1:08

It's a bad idea to do

\countdef\mycount=81


in all flavors of TeX. No discussion allowed.

Plain TeX allocates 25 count registers; it can be checked from any tex.log (or plain.log) on your machine. So, if a macro package says

\newcount\foo


TeX will execute internally \countdef\foo=26, but the actual number should never be used. So, assume you load eplain 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 (if amsmath is the first package called) would be \classnum@, which is a very important internal register for amsmath. 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 counter foo that's added to the reset list of the counter baz; an example is

\newcounter{section}[chapter]


that'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

1. \c@foo via \newcount\c@foo
2. \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 use

\stepcounter{foo}
\setcounter{foo}{<number>}

\refstepcounter{foo}


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

• What you say is what I assumed. I'm making sure that I miss no other aspects of the issue. I originally got the \coundef\mycount=81 from The TeXBook. Coming from a history of writing C++ I was never comfortable with it. You distinguish between .tex and .sty usage of \newcount\mycount. Are you saying it is best to use that in a package or would you care to elaborate? This is what my question is aiming at. Best practice. Cheers – Geoff Pointer Nov 7 '13 at 0:54
• I've rewritten the question and your answer still fits it. What I would like if you wouldn't mind is for you to top up your answer with some expansion on your comment about usage of \newcount'. Thanks for your help so far, it has pushed me to read further into Knuth's book and I'm starting to enjoy it. Cheers – Geoff Pointer Nov 8 '13 at 1:11
• @GeoffPointer I added something about “TeX versus LaTeX counters”. – egreg Nov 8 '13 at 9:51
• So, you're saying a LaTeX counter is not appropriate if I want to do some local manipulations? There are times when you do want a global value updated from a local context. Should I not be doing that with a TeX counter? – Geoff Pointer Nov 12 '13 at 7:46
• @GeoffPointer For local manipulations you don't need \refstepcounter` (which is the main reason for all operations being global), so a TeX counter is probably preferable. – egreg Nov 12 '13 at 7:48