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 use both amsmath and pdfcomment in one document. This leads to the references produced by (\ref{}) to appear like (zref@1) instead of numbers like (1). Without amsmath or pdfcomment this problem does not show up.

A minimal (not) working example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref} 
\usepackage{pdfcomment}

\begin{document}

\begin{equation}
\label{eq:1}
E = m \pdftooltip{c}{speed of light}^2
\end{equation}

See eq. (\ref{eq:1}). % <- this produces (zref@1) instead of (1)

\end{document}

Compile it with pdflatex.

Any suggestions are very welcome.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

The problem is not really to do with pdfcomment, except that one is more likely to use a \pdftooltip inside an equation than, say, a \section command. But if you were so foolish as to try:

\begin{equation}
\label{eq:1}
x^2 + y^2 = z^2
\section{Pythagoras}
\end{equation}

then (assuming it actually compiled, which I doubt) you would find exactly the same problem.

It is due to the fact that when amsmath processes an equation environment, including the equation one itself (since it redefines it), then it does some Complicated Stuff on the equation which involves processing it a few times over (this causes Issues with beamer and tikz as well, I believe). One consequence of this is that if you put a \label command in an equation environment then the label is not saved right away, rather the equation is processed at least once before the label is saved to the .aux file. This is presumably to deal with things like align where more than one piece of the whole equation contents can be labelled (I'm guessing here).

Now the \label command figures out what information to save by using the value of \@currentlabel. This is a neat piece of work, and it's what ensures things like \section{Introduction}\label{sec:intro} Just Works(TM). But this does mean that you need to ensure that between when the counter is incremented and the \label command is processed then the the \@currentlabel doesn't get redefined.

Here's where amsmaths wizardry works against it. If there is a command inside the equation which increments a counter in such a way that the \@currentlabel is redefined then it is that which gets saved rather than the equation label. My silly example with \section is one such command, but that's unlikely to appear in an equation environment. Another is \pdftooltip and that, as we see, is a little more likely.

To verify that what I'm saying is true, we can simulate the \section command:

\documentclass{article}
\usepackage{amsmath}

\renewcommand{\thesection}{\alph{section}}

\begin{document}

\section{Introduction}
\label{sec:intro}

\begin{equation}
\label{eq:1}
E = m c^2
\refstepcounter{section}
\end{equation}

See eq.\ \ref{eq:1} in Section~\ref{sec:intro}.

\end{document}

This produces:

See eq. b in Section a.

If we comment out the \refstepcounter line, we get:

See eq. 1 in Section a.

As another experiment, we can remove amsmath and get the problem with the following code:

\documentclass{article}
\usepackage{pdfcomment}

\renewcommand{\thesection}{\alph{section}}

\begin{document}

\section{Introduction}
\label{sec:intro}

\pdftooltip{hello}{world}

\label{sec:introb}

Sections~\ref{sec:intro} and~\ref{sec:introb} are the same.

\end{document}

In this case, the answer is simple: make sure you issue the \label before the \pdftooltip. But for the amsmath situation, things aren't so easy because everything is internal.

Looking in the source of pdfcomment I can see that \pdftooltip uses \refstepcounter which is the magic command which resets the \@currentlabel. My guess is that it only needs to use it internally, and afterwards it doesn't need to have \@currentlabel set to anything particular. So the best solution would be to patch the pdfcomment so that it saves the current value of \@currentlabel when invoked and restores it again afterwards.

However, I don't recommend patching packages yourself. So you should contact the maintainer of the package and ask him to do so. In the meantime, there is a way to get around the problem. This is simply to ensure that the correct \@currentlabel is in force at the end of the equation. So we need to call \refstepcounter but without actually incrementing the counter. Here's a simple macro that does that:

\makeatletter
\def\refsetcounter#1{%
    \protected@edef\@currentlabel
       {\csname p@#1\endcsname\csname the#1\endcsname}%
}
\makeatother

Call it as \refsetcounter{equation}. It has to be placed at the end of the equation environment (or at least, after the \pdftooltip).

Now, it would be nice to have this done automatically. This would involve patching the amsmath commands. Normally, I'd be wary about this as well, but I feel a little more on sure ground with the amsmath package than the pdfcomment one so I'm prepared to have a go!

My experiments show that it is actually only the equation command that has this problem. Somehow, things like align, gather, and multline work just fine. So we only need to ensure that our new command, \refsetcounter{equation} is run at the end of the equation environment. Here's the code for that:

\makeatletter
\def\refsetcounter#1{%
    \protected@edef\@currentlabel
       {\csname p@#1\endcsname\csname the#1\endcsname}%
}
\let\orig@endequation=\endequation
\def\endequation{%
  \refsetcounter{equation}%
  \orig@endequation}
\makeatother

With this in the preamble, your code produces:

See eq. (1).

As it should.

share|improve this answer
    
Thanks a lot. It has solved my problem. –  amorua Sep 13 '11 at 19:13
    
Andrew, this was a very interesting read, though I confess, that I'm not latex literate enough to understand your code. It seems that the problem here is almost identical in nature to a problem that I posted here to do with markup in figure captions: tex.stackexchange.com/q/42960/1269 . I tried your temporary solution using \refsetcounter{figure} in figure captions, and also at the end of the figure environment, but this didn't work. I'm guessing that different variables are involved. –  aghsmith Feb 8 '12 at 17:36
    
@aghsmith I've had a look at your question and it certainly looks similar enough. However, the figure environment doesn't work in the same way as the equation one so even if the problems are the same then the solutions may be different. –  Loop Space Feb 8 '12 at 19:18

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.