# How to erase/delete a counter

I was wondering how does TeX manage counters. In particular, assuming I define a new counter in TeX by \newcount\mycounter and at some point I don't need it anymore, can I delete it and free the associated registry?

This may be useful when one needs to use a sequence of counters \c1, \c2, ..., \c<n> where n may become large, and at some point there is no need for some of the counters in the sequence (say, up to some index).

-
You can free it without worrying about the name, but the problem is that the standard allocation system (from plain TeX and inherited by LaTeX2e and ConTeXt) is not really designed for this. You could look at the e-TeX mechanism, but that is still more about freeing when TeX groups end. One could write a system to allow freeing, but with e-TeX providing 32k registers, I'm not sure of the need! –  Joseph Wright May 17 '12 at 13:42

If the usage of the n counters is specific for some task you can do the computations in a group and assign them whatever register you want:

\newcount\mycounter
\begingroup
\countdef\mycounta=11
\countdef\mycountb=12
...
<the computations where the total is stored in \mycounta>
\global\mycounter=\mycounta
\endgroup


The local equivalence of \mycounta to \count11 will be forgotten at the end of the group as will be their values. As long as you don't use any "external" counter in your computation, you'll be sure that no conflict will arise.

Freeing counters would require a rather complex rewriting of the allocation macros; this is not very important with e-TeX that provides 32768 registers of each type.

-