I found a conflict between the hyperref/subcaption/refcheck packages.

I was using figures with subfigures and subcaptions, enabling the hyperref package for cross referencing in the document without any problems so far.

When I included the refcheck package to check for unused bib items and cross references, it causes problems if I cross link a subfigure using the \subref{...} command to a figure label with underscores, eg. \label{fig:A_A_A}, in the caption of the figure environment (see MWE with \subref{fig:A_A_A})

It complaings about missing $ as if it would like to put the underscores in math mode.

Missing $ inserted [...]

Any ideas how to fix this conflict?

EDIT: If I load the refcheck package before the other two packages, the document compiles fine, but then actually the refcheck package functionality is lost, i.e. no warnings about unused references.



\usepackage{hyperref}          %Make hyperlinks available
\usepackage{subcaption}        %Make subfigures available

%commenting out this package restores the working version   
\usepackage{refcheck}          %Warn for unused bibitems

Figure \ref{fig:A_A} %no problem with the reference here

    \rule{3cm}{3cm} %dummy for a grafic
    \caption{A subcaption}
    \label{fig:A_A_A} %problematic label for subfigure with underscores
  \caption[A short caption]%
          %caption without the subref works without any problems
          %{A long caption without a subref}
          %caption with subref for the label with underscores causes problem
          {A long caption with a subref with underscores \subref{fig:A_A_A}}
  \label{fig:A_A} %no problem with label for figure with underscores


The problem is that refcheck isn't really compatible with hyperref, because refcheck doesn't respect hyperref's starred version of the \ref macro (this could be considered a bug with refcheck, and should probably be reported to the package maintainer).

When you do \subref{fig:A_A_A} it expands to something like this:


Note the use of \ref* here. It's because hyperref overrides the regular \ref to use \hyperref to produce a hyperlink. hyperref therefore also provides \ref* as an alternative that does the same as \ref, except that it doesn't generate a hyperlink (you can think of \ref{A} as the same as \hyperref[A]{\ref*{A}}). In the case above, the reference text and the link use different reference labels, hence the use of \ref*.

Because refcheck overrides \ref without regards to hyperref's \ref* macro, you basically lose the "capabilities" of the starred version. Hence \ref*{A} will now expand as if you'd written \ref{*}{A}.

In the case of \ref*{sub@fig:A_A_A}, you hence get \ref{*}{sub@fig:A_A_A}, which clearly won't work very well. You'll get a warning

LaTeX Warning: Reference `*' on page 1 undefined on input line XY

and since {sub@fig:A_A_A} is not an argument to the \ref macro, you'll get the normal "underscore error" that you saw.


As a quick-fix, you could use something like this in your preamble:




This code restores hyperref's definition of \ref, and patches the \T@ref (the non-starred version) and the \@refstar (the starred version) macros that hyperref uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.