# etoc with hyperref and \addchap

I have a problem when using etoc, hyperref and the KOMA-script-command \addchap:

\documentclass{scrbook}

\usepackage{etoc}

\newcommand*\chaptertoc{
\etocsetnexttocdepth{2}
\etocsettocstyle{\section*{Local contents}}{}
\localtableofcontents
}

\usepackage{hyperref}

\begin{document}
\chaptertoc

\section{Alpha}
\section{Beta}

\chaptertoc

\section{Alpha}
\section{Beta}

\end{document}


The links from the second chapter-TOC lead to the sections of the first chapter. When I use \chapter, this isn΄t a problem; however, I would like to retain \addchap. Any way out?

• Do you really want a section with number 0.1 in both unnumbered chapter »ALPHA« and unnumbered chapter »BETA«? – esdd Oct 30 '18 at 11:30
• No, not really; at the time of writing the question I saw the issue but wanted to keep the working example minimal. I didn΄t see the problem wasn΄t related to etoc, either. In my big document I just redefined the section counter so that every section looks like »ARABIC SECTIONTITLE«. – Kubo Oct 31 '18 at 11:53

Set option hypertexnames=false for package hyperref:

\documentclass{scrbook}
\usepackage{etoc}

\newcommand*\chaptertoc{
\etocsetnexttocdepth{2}
\etocsettocstyle{\section*{Local contents}}{}
\localtableofcontents
}

\usepackage[hypertexnames=false]{hyperref}% <- changed

\begin{document}
\chaptertoc
\section{Alpha}
\section{Beta}

\chaptertoc
\section{Alpha}
\section{Beta}
\end{document}


As @jfbu mentioned, the issue is unrelated to package etoc. So I will use a MWE without etoc:

\documentclass{scrbook}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\section{Alpha}
\section{Beta}
\section{Alpha (2)}
\section{Beta (2)}
\end{document}


Result:

As you can see there are two sections with number 0.1 and two sections with number 0.2 because \addchap does not step the chapter counter but resets the section counter.

Package hyperref uses hypertexnames=true by default. So hyperref builds the link names from the link type and the corresponding counter (you can find these link names in the *.aux and *.toc):

chapter*.2
section.0.1
section.0.2
chapter*.3
section.0.1
section.0.2


As you can see there are two links with name section.0.1 and two links with name section.0.2. And you get two related warnings:

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

and

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

So all links to a section with number 0.1 or 0.2 goes to the first section with number 0.1 and 0.2 in the document, respectively.

To avoid this issue, you have to use unique link names. This can be done using option hypertexnames=false for package hyperref:

\usepackage
{hyperref}


Then only one counter is used for all link names

chapter*.2
section.3
section.4
chapter*.5
section.6
section.7


The resulting document looks same as above, but the link names are unique. So the TOC entry »0.2 Beta (2)« links to the second section in the second chapter.

But I think, it would be better if a section number does not occur more than once in a document.

You could combine \addchap and \addsec:

\documentclass{scrbook}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\end{document}


Result:

Or you use the section counter independently of the chapter counter:

\documentclass{scrbook}
\usepackage{hyperref}

\RedeclareSectionCommand[
counterwithout=chapter
]{section}

\begin{document}
\tableofcontents
\section{Alpha}
\section{Beta}

\section{Alpha (2)}
\section{Beta (2)}
\end{document}


Result:

• +1 for workaround. But I do think KOMA should provide a compatibility patch with hyperref (I don't want to dig into its code but it is clearly not enough hyperref aware). – user4686 Oct 26 '18 at 22:01
• There is no problem for a combination of \addchap and \addsec (see my updated answer). But unnumbered chapters containing numbered sections (resulting in section number 0.1 the first and the second chapter) are unusual. – esdd Oct 26 '18 at 22:47
• I agree that it is unusual. Perhaps the OP would be better off with \part, in a scrartcl class rather than such chapters. I still feel there is something "wrong" about \addchap in presence of hyperref. – user4686 Oct 27 '18 at 7:44
• @jfbu \addchap works fine. You would have the same result with standard classes and numbered sections in unnumbered chapters. – Johannes_B Oct 28 '18 at 13:20
• @Johannes_B I beg to differ. \addchap is a higher level user command and should care of all that is needed. – user4686 Oct 28 '18 at 21:02

(this is only a partial answer but too long for a comment)

The problem is unrelated to etoc as the following minimal example demonstrates, where all etoc related matters are commented out:

\documentclass{scrbook}

% \usepackage{etoc}

% \newcommand*\chaptertoc{
%     \etocsetnexttocdepth{2}
%     \etocsettocstyle{\section*{Local contents}}{}
%     \localtableofcontents
% }

\usepackage{hyperref}

\begin{document}

\tableofcontents

%\chaptertoc

\section{Alpha}
\section{Beta}

%\chaptertoc

\section{Alpha (2)}
\section{Beta (2)}

\end{document}


This produces a main TOC like this:

where the PDF was opened in Acrobat Reader which displays the red boxes of hyperref indicative of internal links (not all PDF viewers do, for example Skim on Mac OS doesn't). However the links for sections Alpha (2) and Beta (2) are wrong.

Thus there is incompatibility of hyperref with \addchap of KOMA-script, as used here. Perhaps documentation of KOMA-script has related information?

### updated with a proof of concept

\documentclass{scrbook}

\usepackage{hyperref}

\newcounter{unnumberedchapter}

\def\theHsection{\the\value{chapter}.\the\value{unnumberedchapter}.\the\value{section}}

\begin{document}

\tableofcontents

\section{Alpha}
\section{Beta}

\section{Alpha (2)}
\section{Beta (2)}

\end{document}


(using not the proof of concept above, but a common way to both numbered and un-numbered chapters for link target names; be it a common counter, or a way like done by other mark-up languages outputting to html to convert sectioning titles into ids, etc.... There is absolutely no reason whatsoever that I can see why internal links, which must be able both with numbered and un-numbered targets, should be tied forever to use the actual typographical numbering. If it were, then wouldn't be using \phantomsection a sin? it creates too "non-guessable" names, although it is hard to tell exactly what "non-guessable" means, because these names do obey a definitive logic, they are not random, so any human in possession of the document can "guess" the names. But what for?)
• in my "proof of concept" it would be better if \theHsection was defined using a counter related to chapters, numbered or unnumbered, and the section counter. But this means modifying \chapter too, so here I used the not so pleasing \the\value{chapter}.\the\value{unnumberedchapter}... approach. – user4686 Oct 29 '18 at 18:15
• Your suggestion results in non-guessable link names for all KOMA-Script documents even if there are no \addchap in the document. Additionally it would be difficult to restore the default settings of hyperref. And you have to do the same for table, figure etc. There is also \addsec ... If you really want non-guessable link names by default you can use \AfterPackage{hyperref}{\hypersetup{hypertexnames=false}} in a wrapper class. – esdd Oct 29 '18 at 22:29
• @esdd non-guessable by whom? anyway, as I said in my comment above, my real suggestion is to increment a chapter Hcounter common to both numbered an non-numbered chapters. If one wants more guessable one does not even have to use numbers, one could use html like ids, working on the sectioning names. One may have to do additional things, but that's the whole point: things should work. Currently they are broken for normal user. The class provides \addchap what is its purpose if it is not fully hyperref aware? – user4686 Oct 30 '18 at 6:58