1

Consider the following MWE with cleveref:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
Page one
\label{one}
\newpage
Page two
\label{two}
\newpage
\cpagerefrange{one}{two}
\end{document}

The resulting document looks correct, but the hyperlink of the second part of the range ('2') is wrong, it points to the same page as the first.

Is this simply a bug, or am I missing something essential here?

  • 4
    Untested, but those labels have nothing to anchor to. Try, in this case, adding \phantomsection before each label. – daleif Dec 27 '18 at 9:13
  • 2
    Normally the anchors come from sections, equation or theorem counters, but in this mwe there are none. – daleif Dec 27 '18 at 9:14
  • 2
    Labels can be used whenever there is some \refstepcounter or another command that sets an anchor which could be labelled. So in your case they aren't page labels but rather "labels in the wild". – TeXnician Dec 27 '18 at 9:49
  • 1
    @TeXnician I am still not completely sure I understand the concept or rather how to smartly deal with this. So I have a book with parts, chapters, sections etc. and in this I regularly crossref to page and figure ranges. Do I always have to insert a \phantomsection to get correct hyperlinks? This does not look like an unusual setup to me. Isn't there some general solution to this or do I have to check every label individually whether it needs a \phantomsection command? – Simifilm Dec 27 '18 at 10:05
  • 2
    @TeXnician: lower level 'labeling' (rather hypertargeting) will give destination labels, but nothing that is usable by cref, in my point of view. But you and daleif are right: Without proper \phantomsection or \refstepcounter there is nothing to label or to refer to at all – user31729 Dec 27 '18 at 10:32
2

Links need an anchor. hyperref sets such an anchor when \refstepcounter is issued. So references and links to normal numbered objects normally work without problems. But if you set a label "in the wild" you need to tell hyperref which anchors it should use by issuing a \phantomsection.

You can link to pages as a whole with \hyperpage (this is e.g. used by the index), but then you need an (expandable) method to get the page number.

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
Page one
\phantomsection\label{one}
\newpage
Page two
\phantomsection\label{two}
\newpage
\cpageref{one}, \pageref{one}, \cpageref{two}, \pageref{two}
\cpagerefrange{one}{two}

\hyperpage{\getpagerefnumber{two}}
\end{document}
  • Thanks. Is there anything speaking against something along the lines of \pretocmd{\label}{\phantomsection}{}{}? – Simifilm Dec 27 '18 at 12:04
  • 1
    Well it will break all normal \label uses - the anchors will no longer be e.g. at the begin of a section but at the position where you issue the \label. – Ulrike Fischer Dec 27 '18 at 12:05

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.