14

I have a problem using \intertext together with \vref when hyperref is also used. Consider the following minimal example:

\documentclass{article}
  \usepackage{amsmath, varioref}
  \usepackage{hyperref}
\begin{document}
  \begin{equation}
    \label{kinetic-energy}
    E = mc^2
  \end{equation}
  \begin{gather}
      A \\
  \intertext{ and therefore due to \vref{kinetic-energy}: }
      B
  \end{gather}
\end{document}

which yields an error message of the pattern:

! Package amsmath Error: Multiple \label's: label '1@xvr' will be lost.

at the end of the gather environment.

If I either comment out hyperref, OR use \ref instead of \vref the example works fine. I am using TeXLive 2011.

Have I encountered a package bug/conflict, or is there some remedial I have missed?


UPDATE: I tried the solution of Mico, and it works except for a slight incompatibility between cleveref and refstyle (sic); it misses a space between the equation number and the page reference. Adding this to the preamble after including cleveref seems to fix the problem:

\makeatletter
\let\withoutspacecref@@vpageref\cref@@vpageref
\renewcommand\cref@@vpageref{\space\withoutspacecref@@vpageref}
\makeatother

Update 2023: The problem shown above would no longer appear to be relevant as of 2023. Presumably, this is because the varioref and hyperref packages have both been modified in the intervening eleven years. For instance, \vref no longer outputs the string "on the same page" if the instruction occurs on the same page as the object being cross-referenced.

However, it's still the case that hyperref can cause problems for \vref. E.g., assume the commands

\usepackage[nospace]{varioref} 
    % since 2019, it's recommended to load varioref with the 'nospace' option
\usepackage[colorlinks]{hyperref}

are executed in the preamble. Then, insert a \clearpage instruction after equation and before align. The output of the OP's MWE then is:

enter image description here

I'm not sure whether this is an outright bug, but the fact that "on the preceding page" is now part of the hyperlink is probably somewhat unexpected, to say the least.

In contrast, if cleveref is loaded, one gets this output:

enter image description here

I daresay that this is far closer to what most users might expect to get.

Hence, the recommendation stands: to avoid some unexpected interactions between varioref and hyperref, it helps to load cleveref as well -- after both varioref and hyperref, for sure.

\documentclass{article}
  \usepackage{amsmath}
  \usepackage[nospace]{varioref} % 'nospace' option is recommended since 2019
  \usepackage[colorlinks]{hyperref}
  \usepackage{cleveref} % <-- new
  
\begin{document}
  \begin{equation}
    \label{kinetic-energy}
    E = mc^2
  \end{equation}
\clearpage
\begin{gather}
      A \\
\intertext{and therefore, due to \vref{kinetic-energy}:}
      B
\end{gather}
\end{document}
2
  • varioref and hyperref are not fully compatible, so some bad interaction is to be expected.
    – egreg
    Apr 12, 2012 at 9:17
  • @RolKau: Please see my edit based on your suggested edit. Apr 30, 2012 at 9:39

1 Answer 1

15

You have indeed discovered a (mild) incompatibility between the varioref and hyperref packages. A solution to this problem is to load yet one more cross-referencing package, viz., the cleveref package. This approach would appear to work because cleveref modifies some of varioref's internals in order to make varioref and cleveref cooperate better; in the process, varioref and hyperref also become mutually (more) compatible. Note that in the MWE below, the cross-referencing command that's executed is still \vref (and not, say, \cref).

Incidentally, the cleveref package should be loaded later than all other packages that perform cross-referencing tasks. In fact, it's highly advisable to load cleveref last, i.e., after all other packages have been loaded, simply because one may not know for sure whether or not the other packages might perform certain tasks -- however trivial they may seem at first -- that are related to LaTeX's cross-referencing mechanisms.

\documentclass{article}
  \usepackage{amsmath, varioref}
  \usepackage[colorlinks]{hyperref} % added 'colorlinks' option to make output 
                                    % show up in attached image file
  \usepackage{cleveref}  % new instruction
\begin{document}
  \begin{equation}
    \label{kinetic-energy}
    E = mc^2
  \end{equation}
  \begin{gather}
      A \\
  \intertext{and therefore due to \vref{kinetic-energy}: }
      B
  \end{gather}
\end{document}

enter image description here

1
  • 1
    You Sir, are a gentleman and a scholar! ;-)
    – RolKau
    Apr 13, 2012 at 7:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .