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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command that creates a counter or, if it already exists, redefines it? Thing is that I'm working with nested loops. The second loop is however in a command. This means if I want to use a second counter for the second loop, I need to define it before the first loop and give it as an argument in the command which seems a bit cumbersome.

As example:

\newcommand\recursiveloop[2]{% do loop with counter #2, #1 times
    % do something

\newcounter{loop}     % counter for first loop
\newcounter{recloop}  % counter for recursive loop

\loop\unless\ifnum\theloop=4  % start the first loop
  \stepcounter{loop}  % counter loop +1
  % do something
  \recursiveloop{3}{recloop}  % call command to run second loop

In this example it's of course not a big deal. But my command has already 3 arguments and more will be added.. Isn't there a way of (1) check if the counter exists to run the appropriate command: \setcounter or \newcounter or (2) a command that can do it by himself.

share|improve this question
What if the counter already exists and its value is important in other places? You'd change it and the old value wouldn't be available any more. – egreg Jul 20 '12 at 8:25
you could do \ifx\c@recloop\@undefined \newcounter{recloop} \fi but it's generally inadvisable to do that. The assignment to the csname c@recloop would be local but the allocation of the register would be global. It is better to do as latex lists do and just allocate a fixed number of counters (your maximum depth) in the preamble. allocating mid-document is always a suspect practice. – David Carlisle Jul 20 '12 at 8:28
@egreg: If I had a good way of defining names it wouldn't be any problem, but I see what you mean. Still it is cumbersome in my opinion... – Didii Jul 20 '12 at 8:33
@DavidCarlisle: I'll try that, but what are the consequences of the allocation? Could it be possible to overwrite the value without LaTeX knowing about it you mean? – Didii Jul 20 '12 at 8:35
you may overwrite something or you may run out of registers if you keep allocating new register for the same name. (classic tex only has 255 of them and around half are already used typically) – David Carlisle Jul 20 '12 at 8:43
up vote 7 down vote accepted

No, \renewcounter and \providecounter don't exist and they shouldn't.

By way of example, suppose you say


\loop\unless\ifnum\value{loop}=4  % start the first loop
  \stepcounter{loop}  % counter loop +1
  % do something
  \recursiveloop{3}{page}  % call command to run second loop

and \recursiveloop does \stepcounter{page}: chaos will ensue even if the loop is in a group, because \stepcounter, \addtocounter and \setcounter always act globally.

Of course you'd not use page, but what would happen if you choose a name already referring to some counter that's defined in a package you load and stores some important value?

Always allocate your own counters in the preamble or use the scratch counters provided by LaTeX (\@tempcnta and \@tempcntb).

By the way, don't use \theloop for numeric tests, but \value{loop}, which stores the value in a form which is independent from the value's representation.

share|improve this answer

Your Answer


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.