14

In the following code, the command \label{my} appears on page 4, as well \pageref{my} says "4", but the hyperlink points to the page one. Why is that?

\documentclass{book}
\usepackage{hyperref}
\usepackage{lipsum}

\begin{document}

  % Four pages of text.
  \lipsum[1-20] 

  % The label is on fourth page.
  \label{my}    

  % Buggy pageref. It produces the number 4, but the link leads to the first page.
  \pageref{my} 

\end{document}
  • 7
    Most definitely not a bug. The \label command attaches a marker to the nearest structure -- \section, \caption, etc -- that can receive an anchor. Your document has no anchorable structures at all; hence the default anchor, the start of the document, applies. Use the \pantomsection command to create additional anchors for \label commands. – Mico Mar 23 '12 at 3:05
27

It's not a bug; there's no anchor; \label by itself does not place an hyperlink-anchor, so obviously your hyperlink won't take you to the place where you placed \label; you can use \phantomsection to create an anchor at a given location:

\documentclass{book}
\usepackage{hyperref}
\usepackage{lipsum}

\begin{document}

\lipsum[1-20] 

\phantomsection\label{my}\newpage 

\pageref{my} 

\end{document}

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.