# Should LaTeX counters always be modified globally?

In this answer, egreg states that "LaTeX counters should always be modified globally". However, the etoolbox package provides the \defcounter macro which will assign a value to a previously initialized counter locally (but may be prefixed with \global). I have been using this macro e.g. to change the value of the secnumdepth counter inside a group:

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\section{bla}

\begingroup
\defcounter{secnumdepth}{-2}

\section{blubb}
\endgroup

\section{foo}

\end{document}


So: Is egreg's statement about always globally modifying counters correct? If so, why? If not, is the usage of \defcounter in my MWE still bad practice?

-
It's discussed in the TeXbook (p. 301) or in TeX by Topic (p. 275). –  egreg Jul 17 '11 at 14:33

As always with (La)TeX: If you know what you are doing you are allowed to do it!

Some local counter changes make perfectly sense, like in the example in the question. However I wouldn't call secnumdepth a real counter. After all it is never incremented. Here simply a counter register was used to store an integer because there is no other suitable type. Real LaTeX counters for sectioning commands, lists or other stuff should normally be global, because they express a count which goes over the whole document and is not limited to local groups. If you are so far that you need a local counter for your code you normally already know about \newdimen and can define a TeX counter instead of a LaTeX one.

A good example IMHO of a local redefinition of an otherwise global counter is shown in my answer to Cross-referencing in multiple chapters. There I use {\value{chapter}=<value>\relax\thechapter} (which is kind of a dirty hack) so I can use the current \thechapter format but with a different chapter number as the current one.

PS: I usually code after the rule "Once global, always global!", except in special cases as explained above.

-

Thanks to egreg for pointing me to TeX by Topic. Here's the relevant quote (p. 275):

Quantities that are assigned to inside a group must be restored after the end of that group. The save stack is where the values to be restored are kept; the size of the save stack limits the number of values that can be restored.

Alternating global and local assignments to a value will lead to ‘save stack buildup’: for each local assignment following a global assignment the previous value of the variable is saved. Thus an alternation of such assignments will lead to an unnecessary proliferation of items on the save stack.

-
Yes, indeed you should change a counter in a global and local way inside the same group. But adjusted a few values locally isn't an issue at all. –  Martin Scharrer Jul 17 '11 at 14:59

Due to the nature of the TeX stack, you should use registers either locally or globally. Using primitives, that means something like

\newcount\mylocalcount
\newcount\myglobalcount
\begingroup
\mylocalcount 10 %
\global\myglobalcount 10 %
\endgroup


Thus if you want to use \defcounter for local assignments, you should not intermix it with \setcounter for the same counter.

In LaTeX3, we've formalised this by having \l_... and \g_... prefixes for variables.

-