# “\pageref” produces wrong page number. It's time to argue!

I know this can be considered as duplicate question. I'm not here to find solution for it. I asked this question just to argue about reliability of pageref. I mean, why this malfunctioning happens so often? Are there any technical issues behind it?

I faced it while typesetting the 'latex-second-d.tex' file found in 'first-latex-doc.zip' came from an article "first-latex-doc – A document for absolute LATEX beginners" on https://ctan.org/pkg/first-latex-doc.

Instead of 2, it produced 3 while still 'Goto page 2' (as expected) when we clicked on it, Why?
Here is the code: (I'm using TeXworks ver 0.6.2)

\documentclass{article}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{lipsum}

\title{Test document}
\date{2009-Oct-12}
\begin{document}
\maketitle
\tableofcontents
\newpage

This is some preamble text that you enter
yourself.\footnote{First footnote.}\footnote{Second footnote.}

\section{Text for the first section}
\lipsum[1]

\subsection{Text for a subsection of the first section}
\lipsum[2-3]
\label{labelone}

\subsection{Another subsection of the first section}
\lipsum[4-5]
\label{labeltwo}

\section{The second section}
\lipsum[6]

Refer again to \ref{labelone}.\cite{ConcreteMath}
Note also the discussion on page \pageref{labeltwo}

\subsection{Title of the first subsection of the second section}
\lipsum[7]

\begin{thebibliography}{9}
\bibitem{ConcreteMath}
Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
\textit{Concrete mathematics},
\end{thebibliography}
\end{document}


A snap of it's output:

• You need to place label immediately after \section. Or use \phantomsection if want link to page 3 – daniel Sep 25 '18 at 20:01

The command \pageref behaves as designed: It produces the page number of the place where the corresponding \label appeared, and not the place where the previous \refstepcounter (triggered by \subsection in the example) appeared. If you want it to produce the latter page number, place it right after the \section command.
The hyperref package muddies the water, however. It uses different mechanisms to achieve its goals, and so it links to the subsection instead of the place where the \label occurred.
If you want the latter, you need to make your own hypertarget: \hypertarget{somename}{some contents}\label{labeltwo}.
Then at the place of reference, use \hyperlink{somename}{\pageref*{labeltwo}}.