# Problem when using pdfcomment in figure captions and then referencing

This is possibly a bug in the pdfcomment package, maybe not. I use the \pdfmarkupcomment[]{}{} macro to insert highlighting into the caption of a figure. Then follows a lable. I reference the figure using \autoref or \cleverref. In both cases the referencing fails and I get a warning:

LaTeX Warning: cref reference format for label type zref@unique' undefined on input line 14.

Here is code for this example:

\documentclass{article}
\usepackage{pdfcomment}
\usepackage{cleveref}

\begin{document}
\begin{figure}
\caption{\pdfmarkupcomment{Hello}{}}
\label{fig:myLabel}
\end{figure}

\cref{fig:myLabel}
\end{document}


Now I know something about this. The highlighting works by inserting hyperlinks/labels into the text and it looks like there is a conflict between the ones inserted into the caption and the label that I create after the caption.

Is there a fix for this?

EDIT: --- (note I changed the quote, and the load order of the two packages as well)---

I've put a bit more effort into this and improved my understanding of the problem, though not to the point of finding a solution

The "undefined" warning that I had previously is to do with pdfcomment inserting tags of type zref@unique which are analogous to tag types of figure or table.

I used the following modified code:

\documentclass{article}
\usepackage{pdfcomment}
\usepackage{cleveref}

\makeatletter
\crefname{zref@unique}{CLEVERzref}{CLEVERzrefs} % defining name for cleverref
\newcommand{\zref@uniqueautorefname}{AUTOzref}  % defining name for autoref
\makeatother

\begin{document}
\begin{figure}
\caption{\pdfmarkupcomment{Hello}{}}
\label{fig:myLabel}
\end{figure}

cleverref: \cref{fig:myLabel}

autoref: \autoref{fig:myLabel}
\end{document}


to get the following output with no warning messages:

So the current unknown is when referencing, why both cleveref and hyperref locate the pdfcomment tag within the comment block instead of the label they are given.

-

This is the same underlying problem as with the question Conflict between amsmath and pdfcomment, namely that pdfcomment uses the label system internally and doesn't clean up after itself. The reason that the solution from that question doesn't work is that you are using hyperref and cleveref which both add stuff to the label-reference system and that has to be taken into account: it's not enough to just reset @currentlabel anymore, you have to reset @currentHref and \cref@currentlabel as well. hyperref defines an internal command that sets its stuff correctly, so we just have to call that, cleveref doesn't (as far as I can tell) so we have to copy the new definition of \refstepcounter to a command \refsetcounter which does the same except that it doesn't increment the counter.

Here's a copy of the code that does that, so far as I can tell!

\documentclass{article}
%\url{http://tex.stackexchange.com/questions/28313}
%\url{http://tex.stackexchange.com/q/42960/86}
\usepackage{hyperref}
\usepackage{pdfcomment}
\usepackage{cleveref}

\makeatletter
\def\refsetcounter{%
\@ifnextchar[{\refsetcounter@optarg}{\refsetcounter@noarg}%]
}
\def\refsetcounter@noarg#1{%
\cref@constructprefix{#1}{\cref@result}%
\@ifundefined{cref@#1@alias}%
{\def\@tempa{#1}}%
{\def\@tempa{\csname cref@#1@alias\endcsname}}%
\protected@xdef\cref@currentlabel{%
[\@tempa][\arabic{#1}][\cref@result]%
\csname p@#1\endcsname\csname the#1\endcsname}%
\hyper@makecurrent{#1}%
}
\def\refsetcounter@optarg[#1]#2{%
\cref@constructprefix{#2}{\cref@result}%
\protected@xdef\cref@currentlabel{%
[#1][\arabic{#2}][\cref@result]%
\csname p@#2\endcsname\csname the#2\endcsname}%
\hyper@makecurrent{#2}%
}

\crefname{zref@unique}{CLEVERzref}{CLEVERzrefs}
\newcommand{\zref@uniqueautorefname}{AUTOzref}
\let\orig@caption=\caption
\def\caption#1{%
\orig@caption{#1}\refsetcounter{figure}%
}
\makeatother

\begin{document}

\begin{figure}
\caption{%
\pdfmarkupcomment{Hello}{}%
}
\label{fig:myLabel}
\end{figure}

cleveref: \cref{fig:myLabel}

autoref: \autoref{fig:myLabel}
\end{document}


At the very least, it produces what I would expect to see with your test document.

-
Thanks very much for this solution Andrew. It does work in the example I gave. I have found some problems though when implementing it in my thesis. The first is that there seems to be a kind of conflict with the caption package (there are warnings). Though the actual consequence of this is not clear. The other is that the \caption[LOF comment]{text comment} macro will no longer support its optional argument. Using one, breaks the referencing again and also messes up the formatting of the caption. –  aghsmith Feb 9 '12 at 19:03
I've tried to contact the pdfcomment package author about this problem. From the package developer point of view, is it as simple as telling it to save and reset the \@currentlabel as you suggested in your original solution: Conflict between amsmath and pdfcomment? –  aghsmith Feb 9 '12 at 19:08
The caption package is going to add yet another layer of things that need to be sorted out! At this point, I would seriously consider hacking the pdfcomment package myself. I forgot (or didn't know) that \caption can take an optional argument so didn't build that in to my solution. It's possible to fix this. Possibly the most elegant is to use the patchcmd` stuff from the etoolbox package, but I've never used that myself. –  Loop Space Feb 9 '12 at 19:33