# Building labels out of counters

There is a simple version to my question, and a complex version. First, the simple version: I want to generate label names by combining a constant string with a counter's value, something like what is done here:

http://tug.org/pipermail/texhax/2006-July/006599.html

\newcommand{\addnextlabel}{\stepcounter{foo}\label{bar:\arabic{foo}}}


But when I use this, I get a bunch of warnings about multiply-defined labels. It appears they are all getting named the same thing, using the highest value of counter foo. I guess this has to do with the order TeX evaluates things, as suggested by the answer in the link above. Can anyone do a better job explaining this than the response there?

Now the more complex problem, and what I'm really trying to solve: I'm using the lineno package, which has a \linelabel command that works much like \label. You can get the label's line number using \ref{labelname}. It's these labels I really want to number with a counter. I tried applying the linked solution to \linelabel, but I couldn't get it to work. Any ideas how I might do this?

-
Hang on a second. After thinking about this again, if you need to type \ref{bar:7} to get the counter number 7, why do you need the label in the first place? –  Will Robertson Dec 2 '10 at 17:12

1. You need to use \refstepcounter instead of \stepcounter. This must be badly documented somewhere — lots of people make this mistake.

2. You might need to save the linelabel counter to a counter of your own, decrement it, and then \refstepcounter it again. (Hope this explanation's not too brief; I'm in a hurry.)

-

your \addnextlabel works fine in standard latex (for me) -- you're presumably using one of the myriad packages that fiddles with the reference mechanism.

i would expect

\newcommand{\addnextlabel}{\stepcounter{foo}%
\edef\baz{\noexpand\label{bar:arabic{foo}}}%
\baz
}


to do the job for you, if my hypothesis is correct.

-
The original \addnextlabel doesn't work to refer back to the labels that it's trying to set. –  Will Robertson Dec 2 '10 at 17:14