Is there something like \renewcounter or \providecounter?
No, \renewcounter
and \providecounter
don't exist and they shouldn't.
By way of example, suppose you say
\providecounter{page}
\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
\repeat
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.
If you really need \renewcounter
then writing it is really simple:
\def\renewcounter#1{\expandafter\ifcsname c@#1\endcsname\else\newcounter{#1}\fi \setcounter{#1}{0}
. However, its use cases are indeed quite rare. The previous answer claims that it should “never” be used, which is, as quite often for such claims, an exaggeration; an example of a specific use case is an exam file, which includes some exercise files, each of which may define (through \renewcounter
) their own, independent question
counters.