# Change page, section, figure, etc identifier avoiding duplicates

Imagine that you want to build a report composed by several papers. Before each paper the titlepage should be displayed. Also, the page number, section number, figure number, etc shall always be reset before each paper starts.

With my solution and using pdftex driver to compile, I got several

pdfTeX warning (ext4): destination with the same identifier (name{page.1}) has been already used, duplicate ignored

or

pdfTeX warning (ext4): destination with the same identifier (name{section.1}) has been already used, duplicate ignored

I saw many examples here, e.g., pdfTeX warning (ext4): destination with the same identifier (nam e{page.1}) has been already used, duplicate ignored. But they do not fulfill all my requirements.

\documentclass{article}

\usepackage{hyperref}

\newcommand{\inserttitlepage}
{
\setcounter{table}{0} % reset counters
\setcounter{figure}{0}
\setcounter{section}{0}
\begin{titlepage}
bla
\end{titlepage}
}

\begin{document}

\inserttitlepage

% Paper 1
\section{Introduction}
\section{Conclusion}

\inserttitlepage

% Paper 2
\section{Introduction}
\section{Conclusion}

\end{document}


So I thought that... I am able to define some custom identifier, which can be random and defined before each titlepage that would influence each page, figure, section, etc, so that there would not exit a duplicate identifier.

To be precise... I am able to change, e.g., name{page.1} to name{pageaaa.1} or name{pagebbb.1} or anything else?

• Perhaps something like my (well ;-)) answer to this question here: tex.stackexchange.com/questions/245680/… – user31729 Mar 10 '16 at 18:24
• No, page.<n> is used for several purposes; you avoid the issue of duplicate page anchor by adding the pageanchor=false option. – egreg Mar 10 '16 at 23:09

The anchor name page.<n> is used for several purposes; since you have several “page 1”, the only way seems to add the option pageanchor=false.

For the other counters with possible repeating values, use the H trick and define a titlepage counter.

\documentclass{article}

\usepackage{chngcntr}
\usepackage[pageanchor=false]{hyperref}

\newcounter{titlepage}
\renewcommand{\theHtable}{\thetitlepage.\arabic{table}}
\renewcommand{\theHfigure}{\thetitlepage.\arabic{figure}}
\renewcommand{\theHsection}{\thetitlepage.\arabic{section}}
\counterwithin*{section}{titlepage}
\counterwithin*{table}{titlepage}
\counterwithin*{figure}{titlepage}

\newcommand{\inserttitlepage}{%
\begin{titlepage}
\stepcounter{titlepage}%
bla
\end{titlepage}
}

\begin{document}

\inserttitlepage

% Paper 1
\section{Introduction}
\section{Conclusion}

\begin{figure}[htp]
\caption{A}
\end{figure}
\begin{table}[htp]
\caption{B}
\end{table}

\inserttitlepage

% Paper 2
\section{Introduction}
\section{Conclusion}
\begin{figure}[htp]
\caption{A}
\end{figure}
\begin{table}[htp]
\caption{B}
\end{table}

\end{document}

• In fact I used the Christian's answer with pageanchor=false, as you stated. But, since your answer is cleaner, I will accept it. – cacamailg Mar 11 '16 at 20:00

This is only a limited solution, but it provides correct linking for sections. The warnings about the page.1 etc. are still there!

One solution is the redefine \theHsection command to provide an additional number (or name, say, the number of titlepages so far) to get an unique anchor.

\documentclass{article}

\usepackage{hyperref}

\newcounter{titlepagecntr}

\newcommand{\inserttitlepage}
{
\refstepcounter{titlepagecntr}
\setcounter{table}{0} % reset counters
\setcounter{figure}{0}
\setcounter{section}{0}
\renewcommand{\theHsection}{papersection.\thetitlepagecntr.\number\value{section}}
\begin{titlepage}
bla \thetitlepagecntr
\end{titlepage}
}

\begin{document}
\inserttitlepage

% Paper 1
\section{Introduction \thetitlepagecntr}
\section{Conclusion \thetitlepagecntr}

\inserttitlepage

% Paper 2
\section{Introduction \thetitlepagecntr}
\section{Conclusion \thetitlepagecntr}

\end{document}

• I tried this solution and the another with your package xassoccnt in both cases the warnings about the page.1 still arise. But I do not understand why. For example, in this solution that you provided titlepagecntr seem to become with the value two for both \inserttitlepage, which I do not understand. – cacamailg Mar 10 '16 at 19:34
• As I wrote: The page.1 warning is still there! I am trying to figure out why the wrong page anchor is set, I assume, this is a titlepage fault – user31729 Mar 10 '16 at 19:37

Each of the two answers that have already been given addresses a different aspect of the problem, but both fail to solve all issues.

The hyperref package can generate names for the \pdfdest nodes (“destination anchors”) in the document in several ways; the one it uses by default is to assign names that are built up from a name of a sectional unit, e.g., section, or of a document element, e.g., page, and the textual representation of an associated counter, e.g., \thesection or \thepage. You can see this if you trace down the contents of the boxes that pdfTeX builds: for instance, if you say

\tracingoutput = 1


in your document, you will find, in your transcript files, lines looking like this

....\pdfdest name{section.1} xyz


for anchors tied to a section heading, or like this

...\pdfdest name{page.1} xyz


for an anchor tied to a page (note that, in order to see anchors bound to a page, you need to trace down the “complete box[es] being shipped out”, that is, to use \tracingoutput, as we did above). To access the textual representation of a counter used for this purpose, however, the hyperref package does not use directly its associated macro of the form \the<countername>, e.g., \thesection, but a macro named after the pattern \theH<countername>, e.g., \theHsection; the default definition of a macro like \theHsection is set up by the package itself, and makes \theHsection simply an alias for \thesection, but you are free to change this default definition in order to change the representation of the counter which is used for building anchors, without affecting the representation that will be used for printed text. Christian’s solution is based on this approach. Let me review it and briefly discuss its pros and cons:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{hyperref}

\newcounter{papernum}
\renewcommand*{\theHsection}{\arabic{papernum}.\arabic{section}}
\renewcommand*{\theHfigure} {\arabic{papernum}.\arabic{figure}}
\renewcommand*{\theHtable}  {\arabic{papernum}.\arabic{table}}
% ... and so on for subsection, subsubsection, etc., if you use them
% E.g., for the subsection counter:
% \renewcommand*{\theHsubsection}{\arabic{papernum}.\arabic{subsection}}

\newcommand{\inserttitlepage}
{
\stepcounter{papernum}
\setcounter{table}{0} % reset counters
\setcounter{figure}{0}
\setcounter{section}{0}
\begin{titlepage}
bla
\end{titlepage}
}

% \showboxdepth = 10
% \tracingoutput = 1

\begin{document}

\inserttitlepage

% Paper 1
\section{Introduction}
\label{S:1-Intro}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\section{Conclusion}
\label{S:1-Concl}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\inserttitlepage

% Paper 2
\section{Introduction}
\label{S:2-Intro}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\section{Conclusion}
\label{S:2-Concl}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\bigskip

Go to page~\hyperpage{1}, \hyperpage{2}, \hyperpage{3},
or~\hyperpage{4} (the latter should be this same page).

\end{document}


Pros:

• it gets rid of the warnings about doubly-defined “section-type” anchors;

• it makes cross references to section titles work correctly even across different papers;

• the labels it writes into auxiliary files can readily be linked with the relevant sectional unit;

Cons:

• the warning about doubly-defined “page-type” anchors are still there;

• \hyperpage commands do not work correctly; in particular, it is not possible to have an Index.

Note that the \theH... trick cannot be applied to the page counter, because the hyperref package treats this special counter in a different way than the others (since it is actually TeX’s primitive register \count0).

Let us consider, next, @egreg’s answer, which simply suggests to turn off generation of “page-type” anchors altogether. Of course, this doesn’t address at all the problem with other types of anchor, and indeed, you still get several warnings if you compile the following (otherwise) MWE:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}

\usepackage[pageanchor=false]{hyperref}

\newcommand{\inserttitlepage}
{
\setcounter{table}{0} % reset counters
\setcounter{figure}{0}
\setcounter{section}{0}
\begin{titlepage}
bla
\end{titlepage}
}

% \showboxdepth = 10
% \tracingoutput = 1

\begin{document}

\inserttitlepage

% Paper 1
\section{Introduction}
\label{S:1-Intro}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\section{Conclusion}
\label{S:1-Concl}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\inserttitlepage

% Paper 2
\section{Introduction}
\label{S:2-Intro}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\section{Conclusion}
\label{S:2-Concl}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\bigskip

Go to page~\hyperpage{1}, \hyperpage{2}, \hyperpage{3},
or~\hyperpage{4} (the latter should be this same page).

\end{document}


Moreover, both cross-references among sections and \hyperpage commands remain broken in this case. Note, however, that @egreg is—of course! :-) —perfectly aware of this, and explicitly suggests to “use the H trick” for “the other counters”.

However, a third solution is possible: the hypertex package can be asked to disregard completely the value of, say, the section counter for generating “section-type” anchors, and use an internally generated label instead. This behavior does apply to “page-type” anchors as well; to request it, you specify hypertexnames=false either as an option or in the argument of \hypersetup:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}

% Another possible solution:
\usepackage[hypertexnames=false]{hyperref}

\newcommand{\inserttitlepage}
{
\setcounter{table}{0} % reset counters
\setcounter{figure}{0}
\setcounter{section}{0}
\begin{titlepage}
bla
\end{titlepage}
}

% \showboxdepth = 10
% \tracingoutput = 1

\begin{document}

\inserttitlepage

% Paper 1
\section{Introduction}
\label{S:1-Intro}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\section{Conclusion}
\label{S:1-Concl}
Sections \ref{S:2-Intro} and~\ref{S:2-Concl}.

\inserttitlepage

% Paper 2
\section{Introduction}
\label{S:2-Intro}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\section{Conclusion}
\label{S:2-Concl}
Sections \ref{S:1-Intro} and~\ref{S:1-Concl}.

\bigskip

Go to page~\hyperpage{1}, \hyperpage{2}, \hyperpage{3},
or~\hyperpage{4} (the latter should be this same page).

\end{document}


If you compile this example, you will notice that all warnings disappear, and that all cross-references, included those provided by \hyperpage, work correctly. Drawback: the names of the anchors which are written into auxiliary files is now unrelated to the numbering of the relevant sectional unit.

In all the examples posted above, you can uncomment the diagnostic commands

% \showboxbreadth = 1000
% \showboxdepth = 10
% \tracingoutput = 1


in order to check exactly what anchor names are generated in each case.

• Sorry, I realized that, in several \ref commands I had written the wrong label! I have just fixed this, now the examples should behave as claimed. My apologies. – GuM Mar 12 '16 at 13:38
• This was super useful for the informs3 documentclass, where I had the problem that appendix sections had a duplicate identifier. I did in the appendix: \renewcommand\theHsection{apx.\thesection}` to make appendix sections non-ambiguous. – srs Jul 21 '17 at 18:40