[texhax] Reference counter turns out to be too big by 1.

Susan Dittmar Susan.Dittmar at gmx.de
Thu Aug 31 08:32:19 CEST 2017


Am 31.08.2017 um 07:10 schrieb Rolf Turner:
> I have defined a new numbered environment and counter by which to number
> instances of that environment.  When I label such instances and refer to
> the labels, I get values of the counter that are too big by 1.

That's no wonder. You first define the counter to be 1. Then, in the
Zorch environment, you use \refstepcounter, which first increases the
counter by one, then creates a reference point. So what you want is
first start the counter by 0, and then put the \refstepcounter macro
before any other usage of the counter in the environment Zorch:

That means:
\newcounter{Zorch}
\setcounter{Zorch}{0}
\newenvironment{Zorch}{
\refstepcounter{Zorch}% no white space in-between!
\section*{Zorch \theZorch}
}{}

Alternatively you can still create the reference point after the section
heading as you did by the following construction (my search fu failed
me, so I did not find a macro offering only the "set reference point"
part of \refstepcounter):

\newcounter{Zorch}
\setcounter{Zorch}{1}
\newenvironment{Zorch}{
\section*{Zorch \theZorch}
\addtocounter{Zorch}{-1}
\refstepcounter{Zorch}
\addtocounter{Zorch}{1}
}{}

What I don't like with the second version: Except for the very start of
the Zorch environment, the Zorch counter has a value one too high for
the current environment. So you can't use it again within the environment.

HTH,
Susan Dittmar



More information about the texhax mailing list