Hyperref links in TOC point to wrong location when using titletoc
This is a workaround, (or hack ;-)), it seems, that titletoc
screws up the hyperlinks of sections for some reason I have not figured out so far.
I redefined the \section
command to manually set the hyperlinks and a dummy hypertarget right after the section starts. It works for \section{}
and \section[]{}
commands, but not for \section*{}
, but this writes no entry to the TOC
, so this is something you can live with(???).
I also used \usepackage{classicthesis}
, which seems to come before titletoc
, otherwise the toc style is not used.
\documentclass{scrbook}
\usepackage[colorlinks=true,linktoc=all]{hyperref} % Normally last!!!
\usepackage{classicthesis}
\usepackage{titletoc}
\titlecontents*{subsection}%
[3.8em]%
{\small\itshape}%
{\phantomsection\hyperlink{subsection::\arabic{chapter}.\arabic{section}.\arabic{subsection}}{\thecontentslabel}\ }%
{}%
{\ \thecontentspage}%
[,\ ]%
[.]%
\let\LaTeXStandardSection\section%
\makeatletter
\newcommand{\section@noopt}[1]{%
\LaTeXStandardSection[\protect\hyperlink{section::\thesection}{#1}]{#1}%
\phantomsection%
\hypertarget{section::\thesection}{}% Empty target
}%
\newcommand{\section@opt}[2][]{%
\LaTeXStandardSection[\protect\hyperlink{section::\thesection}{#1}]{#2}%
\phantomsection%
\hypertarget{section::\thesection}{}% Empty target
}%
\renewcommand{\section}{%
\@ifnextchar[{\section@opt}{\section@noopt}%
}%
\makeatother
\begin{document}
\tableofcontents
\chapter{First}
\chapter{Chapter}
\section{Section}
\newpage
\subsection{Subsection}
\subsection{Another Subsection}
\clearpage
\section{Another Section}%
\clearpage
\section[Yet Another Section]{Yet another section with a different short title}
\subsection{Yet another subsection}
\subsection{Yet another another subsection}%
\chapter{Next chapter}
\section[Yet Another Section]{Yet another section with a different short title}
\subsection{Yet another subsection}
\subsection{Yet another another subsection}%
\end{document}
The problem seems to be a clash between titletoc v1.6 and the current version of hyperref v6.83m. Downgrading to hyperref v6.82q solves the problem. However, using the outdated version of hyperref might produce other complications.
This was fixed in version 2.11 of titletoc; see the changelog at https://github.com/jbezos/titlesec
When I switched to this version of titletoc, it solved this problem for me.