Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Not sure how I should post my question but here it is: I've seen that in plain TeX, \newcount is illegal inside a macro \definition. Is there a way to allocate a register inside a macro? Also, if I know that I will use a register only once or twice, can I free it after and leave the space for a future \newcount?

share|improve this question
1  
It is not illegal, it works: it is just usually a bad idea, you should allocate the counter once and then use it each time in the macro. But if you want do it it works in particular if you are defining a variant declaration form (eg declaring a theorem needs to declare a counter. –  David Carlisle Jan 16 at 15:24
    
@egreg yeah true! I didn't know that it works fine in LaTeX. (question edited) –  lvaneesbeeck Jan 16 at 15:48
    
@egreg well seen! –  lvaneesbeeck Jan 16 at 15:55
add comment

2 Answers 2

up vote 9 down vote accepted

In Plain TeX, \newcount is defined as \outer so it can't be in the replacement text for a macro. The idea is that a macro should never define a new counter, although this isn't true in every occasion, but I'll do an example later.

The main reason for this is that you can't "free" the register after having allocated it. I could be possible, but what about other macros that might have defined to access it? The best strategy is to use symbolic names (what \newcount does) and never care about the actual register number.

If you need a counter for temporary storage, just use \count255 (which is aliased to \count@) or, in a group, any other count register. In more complex cases you can allocate a temporary counter yourself and use it as many times as you wish.


Here's an example of a “legitimate” call of \newcount in a definition. Suppose you want to define distinct counters for different logical structures (like LaTeX does for \newtheorem). So you want to define a command

\newtheorem{<name>}{<label>}

so that \<name> is defined and a counter is allocated.

\def\newtheorem#1#2{%
  \outer\expandafter\def\csname #1\endcsname{%
    \par\medskip\noindent
    \global\advance\csname thmcnt#1\endcsname by 1
    {\bf\number\csname thmcnt#1\endcsname\ #2}%
    \enspace\begingroup\it \ignorespaces}
  \outer\expandafter\def\csname end#1\endcsname{\endgroup\par\medskip}
  \csname newcount\expandafter\endcsname\csname thmcnt#1\endcsname
}

\newtheorem{thm}{Theorem}
\newtheorem{lem}{Lemma}

\lem
Something preliminary.
\endlem

\thm
The big result.
\endthm

\thm
Another big result.
\endthm

\bye

enter image description here

This is just an example, of course; numbering of theorems should be independent of the label. If you look closely, you see how to fool TeX so that an \outer macro is used in the replacement text.

share|improve this answer
    
Thanks! I'll review the expansion rules more closely. Btw it could be possible to define \freecount, \freeskip etc for TeX if \newcount were redefined to allocate a number in function of the previously free'd registers, am I right? –  lvaneesbeeck Jan 16 at 15:54
    
@lvaneesbeeck Not really. One would have to maintain a catalog of the allocation numbers and of the associated names. But you can always say \newcount\foo and then \let\baz\foo and this would give headaches, at the least. –  egreg Jan 16 at 15:57
add comment

It is not illegal, this works without error:

\newcommand\foo{\newcounter{zz}}

\foo

\setcounter{zz}{2}



\stop

However it is almost always better not to do this and to allocate the counter once, amd just use it in each macro.

I assume that you mean LaTeX (as you used \newcounter) If on the other hand you mean the plain TeX \newcount that is different as it is an \outer macro to stop you using it. It is possible to work around that, However the same applies that it is almost always better not to do so.

the standard allocation system used in plain and latex does not allow for freeing unused allocations. It is a simple increment. In classic TeX there are only 256 count registers so you can easily run out, etex allows several thousand so it is less of a problem there.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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