19

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}
yo'
  • 51,322
Ivan Bychkov
  • 731
  • 1
  • 6
  • 13
  • 8
    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 03:05

1 Answers1

33

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}
Gonzalo Medina
  • 505,128
  • This solves "how to fix". For an answer to the initial question ("why") see the comment by Mico in the op. – Arnaud Dec 07 '19 at 20:42