# contentsline in hyperref: setting the target link

I am using the fourth-argument version of \contentsline defined in hyperref. Is there any way to compute the right hyperlink for the last argument of \contentsline in the hyperref package automatically, based on its label?

Right now I am using something like this:

\addtocontents{myList}{\string\contentsline{subsection}{Title of the entry}{\pageref*{labelForTheEntry}}{othertocsubsection.4.0.14}}


Where "othertocsubsection.4.0.14" was set manually by me, which is kind of brittle.

I tried with things like:

\addtocontents{myList}{\string\contentsline{subsection}{Title of the entry}{\pageref*{labelForTheEntry}}{\ref{labelForTheEntry}}}


but it does not work.

I am trying to reference here a future page, that will be included later. The first option above works, but the problem is that the target link must be set manually.

I include below a minimal example:

\documentclass[a4paper,twoside]{book}

\usepackage[final]{pdfpages}
\usepackage[bookmarks=true, bookmarksnumbered=true, bookmarksopen=true]{hyperref}
\usepackage{tocloft}

\begin{document}

\pagenumbering{Roman}

\newcommand{\indexNumberOne}{}
\newlistof{myIndexOne}{myIndOne}{\indexNumberOne}
\setcounter{myIndOnedepth}{4}
\newlistentry{myIndexOnechapter}{myIndOne}{0}
\newlistentry[myIndexOnechapter]{myIndexOnesection}{myIndOne}{1}
\newlistentry[myIndexOnesection]{myIndexOnesubsection}{myIndOne}{2}
\newlistentry[myIndexOnesubsection]{myIndexOnesubsubsection}{myIndOne}{3}

\pagenumbering{arabic}
\setcounter{page}{1}

\pdfbookmark[1]{Main TOC (documents appear in sequential order)}{MainTOC}
\tableofcontents
\cleardoublepage

\pdfbookmark[1]{Additional index (documents DO NOT appear in sequential order)}{AdditionalIndex}
\listofmyIndexOne
\cleardoublepage

\addtocontents{myIndOne}{\centerline{\underline{Additional Index}}}
\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 2}{\pageref*{docTwo}}{section*.2}}
\cleardoublepage

\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 1}{\pageref*{docOne}}{section*.1}}
\cleardoublepage

\phantomsection
\label{docOne}
\addcontentsline{toc}{section}{Document 1}
\includepdf[pages=-,fitpaper=true]{doc1.pdf}
\cleardoublepage

\phantomsection
\label{docTwo}
\addcontentsline{toc}{section}{Document 2}
\includepdf[pages=-,fitpaper=true]{doc2.pdf}
\cleardoublepage

\end{document}


What I would like is a general solution to replace things like "section*.2" and "section*.1" in such a way that this is computed automatically from the labels (the current links will end up pointing to the wrong place if the order of documents is moved). Any ideas?

• why aren't your section headings setting this automatically, which is the intended behaviour. It is impossible really to suggest how you should change your code if you show no code. – David Carlisle Oct 23 '18 at 18:48
• Try with \csname r@labelForTheEntry\endcsname. Without more detail, it's difficult to answer your question. Can you provide a complete, minimal example that uses your setup? It should start with \documentclass and end with \end{document} and allow the community to copy-and-paste-and-compile and see exactly what the issue is. – Werner Oct 23 '18 at 19:29
• Thank you for the suggestions. I have added a minimal example in the question. – user173026 Oct 24 '18 at 15:38
• \pagenumbering{arabic} and \setcounter{page}{1} is doing the same thing twice, basically. – user31729 Dec 30 '18 at 21:00

## 1 Answer

The hyperanchor of a label can be extracted with \getrefbykeydefault{labelname}{anchor}{some default value}, where anchor is the key and the label name is docOne etc. like in this document.

I have provided a small wrapper macro named \adddoctotoc which writes to myIndOne, requires a entry title and the labelname, then extracts the anchor automatically.

Please note that \includepdf has a toc feature as well!

The mentioned dummydocX.pdf files are created with LaTeX using this dummy code:

\documentclass{article}

\usepackage{blindtext}

\pagestyle{empty}
\begin{document}
\section{Beginning \jobname}
\blindtext[50]
\end{document}


Here is the working code, it might require three (!!!) runs of compilations in order to get the correct references and anchors!

\documentclass[a4paper,twoside]{book}

\usepackage[final]{pdfpages}
\usepackage[bookmarks=true, bookmarksnumbered=true, bookmarksopen=true]{hyperref}
\usepackage{tocloft}

\begin{document}

\pagenumbering{Roman}

\newcommand{\indexNumberOne}{}
\newlistof{myIndexOne}{myIndOne}{\indexNumberOne}
\setcounter{myIndOnedepth}{4}
\newlistentry{myIndexOnechapter}{myIndOne}{0}
\newlistentry[myIndexOnechapter]{myIndexOnesection}{myIndOne}{1}
\newlistentry[myIndexOnesection]{myIndexOnesubsection}{myIndOne}{2}
\newlistentry[myIndexOnesubsection]{myIndexOnesubsubsection}{myIndOne}{3}

\newcommand{\adddoctotoc}[3][myIndOne]{%
\addtocontents{#1}{\string\contentsline{subsection}{#2}{\getpagerefnumber{#3}}{\getrefbykeydefault{#3}{anchor}{page.1}}}%
}

\pagenumbering{arabic}

\pdfbookmark[1]{Main TOC (documents appear in sequential order)}{MainTOC}
\tableofcontents
\cleardoublepage

\pdfbookmark[1]{Additional index (documents DO NOT appear in sequential order)}{AdditionalIndex}
\listofmyIndexOne
\cleardoublepage

\addtocontents{myIndOne}{\centerline{\underline{Additional Index}}}

\adddoctotoc{Document 2}{docTwo}
\adddoctotoc{Document 1}{docOne}
%%\addtocontents{myIndOne}{\string\contentsline{subsection}{Document 1}{\pageref*{docOne}}{section*.1}}
\cleardoublepage

\phantomsection
\label{docOne}
\addcontentsline{toc}{section}{Document 1}
\includepdf[pages=-,fitpaper=true]{dummydoc1.pdf}
\cleardoublepage

\phantomsection
\label{docTwo}
\addcontentsline{toc}{section}{Document 2}
\includepdf[pages=-,fitpaper=true]{dummydoc2.pdf}
\cleardoublepage

\end{document}