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.

The \setcounter and \addtocounter commands have global effect, while the \setlength and \addtolength commands obey the normal scoping rules.

Why were they designed like this? Why are \setcounter and \addtocounter not defined to obey normal scoping rules?

share|improve this question

1 Answer 1

That is by design and coded accordantly in the LaTeX core source code. Counters in LaTeX usually count things which are independent from the local group. For example figure captions are enclosed in the group added by the figure environment and any change to the figure counter used by the caption would be lost afterwards. This would render the counter mostly useless.

If you want to set a counter locally use \c@<counter name>=value. To add something to it use \advance\c@<counter name> by value. To build the counter macro you can use \csname name\endcsname or (mis-)use \value{name} which also works on the left hand side of an assignment.

You can also define an own counter using the TeX macro \newcount\yourcounter and then use \yourcounter=<value> etc. However, you then need to define the \theyourcounter macro yourself if you need it and can't use the normal counter formating macros like \arabic{..} or \roman{..} with it.

share|improve this answer
    
I would say you can do \newcount\mycount, and only talk about using counters if they are defined with the appropriate LaTeX2e naming convention (\c@...). –  Joseph Wright Aug 11 '11 at 15:11
    
@Joseph: Yeah, the one is a LaTeX counter and the other one a TeX count(er). You have to keep them separated. –  Martin Scharrer Aug 11 '11 at 15:19
1  
With \newcount\xyz, \arabic{xyz} or \alph{xyz} don't work, as you say. But \@arabic\xyz or \@alph\xyz do. –  egreg Aug 13 '11 at 21:07

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.