[Tex/LaTex] Hyperref links in TOC point to wrong location when using titletoc

hyperreftable of contentstitletoc

I have modified the formatting of subsections in the toc, so that they appear in one line instead of separate lines. For this i used the titletoc package. Here is a MWE:

\documentclass{scrbook}

\usepackage{titletoc}
\titlecontents*{subsection}%
    [3.8em]%
    {\small\itshape}%
    {\hyperlink{subsection.\thecontentslabel}{\thecontentslabel}\ }%
    {}%
    {\ \thecontentspage}%
    [,\ ]%
    [.]%

\usepackage[colorlinks=true,linktoc=all]{hyperref}

\begin{document}

\tableofcontents 

\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\subsection{Another Subsection}
\clearpage
\section{Another Section}
\end{document}

This was very easy, but there are two problems:

  1. The subsection label is not included in the link. This is already fixed in the MWE with the help of the answer to this question: Common style for global TOC, LOF, LOT and titletoc partial TOCs (with hyperref & caption)
  2. The link to the section following the modified subsections points to the wrong location. In the MWE the link of '1.2 Another Section' points to '1.1.1 Subsection'.

I have not found a solution to the second problem. I have experimented with \phantomsection without any success. I have read here that titletoc has some incompatibilities with hyperref and tried to implement the same behavior with the tocloft package. I managed to get something similar, but not quite right. The tocloft approach seems to be so much more convenient, if the links would work properly.

On a sidenote: The document i want to apply this to is rather large and is typeset with the classicthesis style.

I would appreciate any tips or solutions to this problem.

Best Answer

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}
Related Question