[Tex/LaTex] hyperref is pointing always to the first page of the document

back-referencingchaptershyperref

I am typing my thesis in LaTeX. I used hyperref. Two papers are in the bibliography. Paper no 1 is cited in the first page and paper no 2 is cited in the 2nd page.

When I am clicking the red coloured number below the paper number 1 in the bibliography it is pointing wrongly to the title page of the document. But there is NO PROBLEM with paper no 2. Please help me. I want the paper number 1 to point the correct page.

Also in my document, I don't want "Chapter 0, Synopsis". I want only "Synopsis" i.e. no mention of chapter 0 mark. This I want only for this chapter not in the later chapters. I also want synopsis to occur in the content of the document. Please help me to figure out this.

I am attaching my .tex code below.

\documentclass[12pt, reqno, a4paper]{book}

\usepackage{fancyhdr}%, setspace}

\usepackage[pagebackref]{hyperref}
\hypersetup{
     colorlinks = true, linkcolor = red,
     citecolor   = blue,
     urlcolor    = blue,
}


\begin{document}

\begin{titlepage}
\begin{center}
{\bf Title page}
\end{center} 
\end{titlepage}




\pagestyle{plain}
\pagenumbering{roman}
\addtocounter{page}{1} 

%\thispagestyle{empty}

$\textnormal{ }$
\vspace{9cm} 
\begin{center}
{\bf \Large{To my Parents}}
\end{center}


{\Large
\tableofcontents}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername\ \thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\headrulewidth}{0.1pt}
\renewcommand{\footrulewidth}{0.1pt}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark} 
\fancyhead[RE]{\bfseries\leftmark}
\lfoot{Anjan Gupta, TIFR, Mumbai.}




\pagenumbering{arabic}
\setcounter{chapter}{-1}  % to start Chapter numbers from 0

\chapter{synopsis}
\setcounter{page}{1}
In \cite[Theorem 4.1]{cr} it is shown that this is indeed true in the absolute case.

\newpage

we shall now use the result \cite{sfmaa}

\begin{thebibliography}{VAST}

\bibitem{cr} Paper 1.

\bibitem{sfmaa} Paper 2.

\end{thebibliography}

\end{document}

Best Answer

The chapter issue has been solved in comments, so I tried to address the duplicate page anchors.

There's another solution which should be the last resort, proposed by Werner, but he deleted his answer, so not everybody is able to see that post, so I am repeating this just for convenience and in short:

hypertexnames=false

Now, hypertexnames is often criticized, so I digged into the awesome codes of backref and hyperref.

The cause for the complaints about name(page.1) etc. is that the \pagenumbering macro resets the page number each time it \pagenumbering is called, resulting in more than one pages with the number 1, which confuses the hyperlinks. Using \clearpage statementss, this could solve the issue. If this is not done, more tricky setups have to be used.

I solved this by patching the \Hy@EveryPageAnchor command, injecting a \pagenumberingchanges macro between page and .\thepage (effectively).

\pagenumberingchanges uses \thepagenumberingcalls, which is the counter formater for pagenumberingcalls, which is stepped each time \pagenumbering is used.

In addition, this \pagenumberingchanges macro has to be written to the .brf file generated by backref each time a \cite command is applied.

This removes the multiple occurences of the same page.XYZ anchors, however, it is not failsafe, if somebody manipulates the page counter directly or with \setcounter{page}{}.

The links are alright now and point to the correct position.

Update with a version that checks whether the pagenumbering value has changed

\documentclass[12pt, a4paper]{book}

\usepackage{fancyhdr}%, setspace}

\usepackage[headheight=16pt]{geometry}% Fix the complaint of fancyhdr

\usepackage{xpatch}

\usepackage[pagebackref]{hyperref}
\hypersetup{
  colorlinks = true, linkcolor = red,
  citecolor   = blue,
  urlcolor    = blue,
}

\newcounter{pagenumberingcalls}
\newcommand{\pagenumberingchanges}{\thepagenumberingcalls}


\makeatletter

\AtBeginDocument{%

  % Each time the \pagenumbering command is called, increase a counter
  \xpretocmd{\pagenumbering}{\edef\@previouspagenumberingcalls{\thepagenumberingcalls}\stepcounter{pagenumberingcalls}}{}{}
  % Each time \cite is used, write the state of the counter to the .brf file
  \xpretocmd{\cite}{\write@to@brffile}{}{}

%Write the current pagenumbering change state to the .brf file
  % Idea from backref.sty (by David Carlisle and Sebastian Rahtz)
  \newcommand{\write@to@brffile}{%
    \edef\temp@@a{\@previouspagenumberingcalls}%
    \edef\temp@@b{\pagenumberingchanges}%
    \ifx\temp@@a\temp@@b
    \else
    \edef\@previouspagenumberingcalls{\thepagenumberingcalls}%
    \protected@write\@auxout{}{%
      \string\@writefile{brf}{%
        \string\renewcommand{\protect\pagenumberingchanges}{\thepagenumberingcalls}%
      }%
    }%  
    \fi
  }

\@ifpackageloaded{hyperref}{%
  \long\def\hyper@page@backref#1#2#3{\hyperlink{page.\pagenumberingchanges.#1}{#1}}
  \let\backrefxxx\hyper@page@backref

  \xpatchcmd{\Hy@EveryPageAnchor}{%
    \hyper@@anchor{page.\@the@H@page}%
  }{%
    \hyper@@anchor{page.\pagenumberingchanges.\@the@H@page}%
  }{\typeout{Patch 1 succeeded}}{\typeout{Patch failure for EveryPageAnchor}}
  \xpatchcmd{\Hy@EveryPageAnchor}{%
    \hyper@@anchor{page.\the\c@page}%
  }{%
    \hyper@@anchor{page.\pagenumberingchanges.\the\c@page}%
  }{\typeout{Patch 2 succeeded}}{}
  \xpatchcmd{\Hy@EveryPageAnchor}{%
    \hyper@@anchor{page.\the\Hy@pagecounter}%
  }{%
    \hyper@@anchor{page.\pagenumberingchanges.\the\Hy@pagecounter}%
  }{\typeout{Patch 3 succeeded}}{}
  }{%Nope, no hyperref loaded --> just be silent%
  }
}
\makeatother


\begin{document}
\begin{titlepage}
  \begin{center}
    \bfseries Title page
  \end{center} 
\end{titlepage}


\pagestyle{plain}
\pagenumbering{roman}
\addtocounter{page}{1}%????

{\Large
\tableofcontents}


\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername\ \thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\headrulewidth}{0.1pt}
\renewcommand{\footrulewidth}{0.1pt}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark} 
\fancyhead[RE]{\bfseries\leftmark}
\lfoot{Anjan Gupta, TIFR, Mumbai.}



\pagenumbering{arabic}
\setcounter{chapter}{-1}  % to start Chapter numbers from 0

\chapter{synopsis}
In \cite[Theorem 4.1]{cr} it is shown that this is indeed true in the absolute case.

\clearpage

we shall now use the result \cite{sfmaa}

\begin{thebibliography}{VAST}
\bibitem{cr} Paper 1.

\bibitem{sfmaa} Paper 2.

\end{thebibliography}
                                %
\end{document}

Related, but different problem can be found here: https://tex.stackexchange.com/a/373678/31729

Related Question