5

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.

  • I'm using texlive 2012 (via official ppa backport), scrbase 2012/05/15 v3.11 KOMA-Script package and titletoc 1.6. Your code is working like a charm without any problem or wrong linking. – Pouya Jun 9 '14 at 12:31
  • @Pouya: Really? My output does not provide the desired linking. I wonder, if titletoc really sets a \hypertarget command at all in the corresponding sections, subsections etc. – user31729 Jun 9 '14 at 13:30
  • @ChristianHupfer, I think so... Maybe I haven't understand the question perfectly, so here is my pdf. Tell me if I got it wrong. – Pouya Jun 9 '14 at 13:39
  • @Pouya: I believe you ;-) I think, the problem is due to some clash between titletoc and the KOMA classes – user31729 Jun 9 '14 at 13:42
  • @ChristianHupfer, I just checked, the problem remains when using the standard article document class. The result just doesn't look so nice.... – CFL Jun 9 '14 at 13:49
2

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}
  • thanks, that was quick. This works, but it produces some warnings: Package hyperref Warning: Token not allowed in a PDF string I guess i can live with that as I prefer to keep the current version of hyperref. – CFL Jun 9 '14 at 14:51
  • While the MWE is fixed with your code, it does not fix the document i am working on. There must be an additional problem elsewhere. However, that will take a while to look into, so i guess it is down to v6.82q for now... – CFL Jun 9 '14 at 15:26
  • @CFL: Could you perhaps post your more elaborate document or at least the relevant portions of it, so that I can look for some solution. I suspect, that linking to the toc requires some redefinition of the other sectioning commands as well -- I already feared that this could be an issue. You could also contact Heiko Oberdiek in TeX.SX chat. – user31729 Jun 9 '14 at 17:08
  • that is a very generous offer, thank you. For now I have not encountered any new problems by using the older version of hyperref. As that is fixing the problem, I want to spare you looking through my messy code. Anyway, a real fix could only be worked out when looking into the internals of titletoc and hyperref, I suppose. – CFL Jun 10 '14 at 9:22
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.