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.

I'm not quite sure what the problem is, so I'll just post a MWE:

\documentclass{article}

\newcommand\twolabels[2]
{
    \label{my:#1}
    \label{my:#2}
}

\begin{document}
\twolabels{4}{a}
I have two labels: \ref{my:4} and \ref{my:a}.
\end{document}

Instead of referring me to 1 and 2, it gives me two ??'s. Where am I going wrong?

share|improve this question
    
The MWE works for me. Are you sure you compiled enough times? It takes at least two times for the changes in the labels to come trough. Also your MWE won't do anything good, since there is nothing to reference to (see @lockstep's answer). –  romeovs Feb 5 '12 at 19:55
add comment

2 Answers

up vote 4 down vote accepted

In its minimal or default form, \labels only store the counter and the page that it's on, the former retrievable via \ref and the latter retrievable via \pageref. TeX \label-\ref systems is used to mark the last referencable counter and store it in the \label. If there is no counter that was last referenced (as in your case), there will be no appropriate \ref, hence your ?? as output.

As a brief introduction to how one can use counter referencing, here is a way to achieve your result. First you define a counter using

\newcounter{mycount}

Then, whenever you want to store the counter for referencing, you perform

\refstepcounter{mycount}

This does two things: increases the counter by 1 (or "step" it), and adds a "referencing" capability or mark. Now, if you use \label it will store the value of mycount as well as the page that mycount was last \refstep-ped. Here's a minimal example of how that could work in your instance:

enter image description here

\documentclass{article}
\newcounter{mycount}
\newcommand\twolabels[2]
{
  \refstepcounter{mycount}\label{my:#1}%
  \refstepcounter{mycount}\label{my:#2}%
}

\begin{document}

\section{foo}
\twolabels{4}{a}
I have two labels: \ref{my:4} and \ref{my:a}.
\end{document}

Now, this doesn't mean that you always have to create your own counter if you want to refernece something via \label-\ref. Instead, there are other counters that is \refstep-ped like section whenever you perform \section. Using the example above, if you had just placed \label{test} after the \section and subsequently used \ref{test}, you retrieve the section number 1 in the text.

Finally, you can also modify the way the counter is displayed in the text. For example, if you're interested in (lower case) Roman numerals for your counter, you could add

\renewcommand{\themycount}{\roman{mycount}}

after defining the counter mycount.

You need to compile twice for the references to be accurate, since information is first stored in an .aux file and retrieved upon the second compile.

share|improve this answer
    
Thanks, it helped me a lot! –  Jerry Feb 5 '12 at 21:24
    
FWIW: I had a similar problem and this answer is a bit over the top for me. Basically, I wanted to count citations used in a table, so I did basically: \refstepcounter{usedcitations}\label{usedcitationslabel} on every table entry and then anywhere in the document: \getrefnumber{usedcitationslabel}. Hope this helps someone. –  Frederick Nord Feb 10 '13 at 21:21
add comment

The \label/\ref mechanism needs a counter that can be referred to. If you add, e.g., a \section command immediately after \begin{document}, your \twolabels macro will create references to section 1.

\documentclass{article}

\newcommand\twolabels[2]
{
    \label{my:#1}
    \label{my:#2}
}

\begin{document}

\section{foo}
\twolabels{4}{a}
I have two labels: \ref{my:4} and \ref{my:a}.
\end{document}
share|improve this answer
    
Thanks, I didn't know that. Good to know! :-) –  Jerry Feb 5 '12 at 21:24
add comment

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.