# Problem when contents go over two pages

destination with the same identifier (name{page.i}) has been already used, duplicate ignored \relax l.36 ...on}{\numberline {5.6}test}{9}{section.5.6} [1])pdfTeX warning (ext4): d

If the document is not so long we don't get a warning, for example:

\documentclass[a4paper]{memoir}

\title{Test}
\usepackage{hyperref}

\begin{document}

\frontmatter
\begin{titlingpage}
\maketitle
\end{titlingpage}

\tableofcontents

\mainmatter
\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end{document}

However if we increase the document so that the contents go over several pages we get the warning, an example of a document giving the warning is:

\documentclass[a4paper]{memoir}

\title{Test}
\usepackage{hyperref}

\begin{document}

\frontmatter
\begin{titlingpage}
\maketitle
\end{titlingpage}

\tableofcontents

\mainmatter
\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end{document}

Is there a way to fix this?

• I bet this is not an error. I bet it is a warning. An error stops compilation. Does your code not compile? – cfr Jun 7 '17 at 2:36
• Ok, I changed the word to "warning", it compiles. – user119615 Jun 7 '17 at 2:40

The main culprit for the error is the titlingpage environment, which explicitly resets the page number with \setcounter{page}{\@ne} -- this confuses the hyperanchors set by hyperref.

In a similar answer I kept track of such page numbering changes (see https://tex.stackexchange.com/a/355883/31729), here I used the same method, adapted for the titlingpage environment, by patching \mem@titlingpage and its 'friend' \endmem@titlingpage.

In the very end, this removes the warnings and provides correct hyperanchors as long as no other explicit \setcounter{page} manipulations occur.

Another possible solution (untested, however): Use the hypertexnames=false option to hyperref or in \hypersetup.

\documentclass[a4paper]{memoir}

\usepackage{xpatch}

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

\makeatletter

\AtBeginDocument{%
\xpretocmd{\pagenumbering}{\edef\@previouspagenumberingcalls{\thepagenumberingcalls}\stepcounter{pagenumberingcalls}}{}{}

\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%
}

\xpatchcmd{\mem@titlingpage}{%
\if\mem@titlpg@reset\setcounter{page}{\@ne}\fi
}{%
\if\mem@titlpg@reset\stepcounter{pagenumberingcalls}\setcounter{page}{\@ne}\fi
}{\typeout{Successfully patched \string\mem@titlingpage}}{}

\xpatchcmd{\endmem@titlingpage}{%
\if\mem@titlpg@reset\setcounter{page}{\@ne}\fi
}{%
\if\mem@titlpg@reset\stepcounter{pagenumberingcalls}\setcounter{page}{\@ne}\fi
}{\typeout{Successfully patched \string\endmem@titlingpage}}{}
}

\makeatother

\title{Test}
\usepackage{hyperref}

\begin{document}

\frontmatter
\begin{titlingpage}
\maketitle
\end{titlingpage}

\tableofcontents

\mainmatter
\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end{document}

To get rid of the warning you only need to fool hyperref during the first two pages with a suitable \pagenumbering. Anything different than arabic and roman which are used afterwards.

\documentclass[a4paper]{memoir}

\title{Test}
\usepackage{hyperref}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\begin{titlingpage}
\maketitle
\end{titlingpage}

\pagenumbering{roman}
\tableofcontents

\mainmatter

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end{document}