[Tex/LaTex] Problem when using pdfcomment in figure captions and then referencing

cleverefcross-referencinghyperrefpdfcomment

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:

enter image description here

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.

Best Answer

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{https://tex.stackexchange.com/questions/28313}
%\url{https://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.

Related Question