I am using KOMA-script with hyperref and have set up my table of contents with tocbasic
.
I would like to format my TOC in the way presented here, that is, to have the page number and the entry number made into hyperlinks, and nothing else.
This is the desired result ([]
denotes hyperlinks):
[I] Foo ......... [3]
[1] Bar ......... [5]
[1.1] Baz ....... [5]
However, Ulrike Fischer's answer there works perfectly for classes article
and book
, but it fails as soon as it's changed to scrartcl
or scrbook
. Possibly because in KOMA-script, a different command is in charge of the entry number than in the standard classes.
I have also tried to replicate the approach of these two
related answers –
i.e. patch \contentsline
using etoolbox
, adding \hyper@linkstart{link}{<some #>}
and \hyper@linkend
around the entry number.
But I'm hitting the same wall: those answers work for the standard classes but do not (seem to) translate to KOMA-Script/tocbasic.
Is anyone able to help?
Here's a MWE:
\documentclass{scrbook}
\usepackage[linktoc=page, % plus, if possible, the entry number
colorlinks]{hyperref}
\begin{document}
\tableofcontents
\part{Foo}
\chapter{Bar}
\section{Baz}
\end{document}
EDIT
@esdd has provided two great solutions.
It is worth pointing out a difference in their usage.
The second one uses hooks from tocbasic
that are, as yet, undocumented.
The solution works out of the box for whatever style of sectioning levels one chooses via \DeclareTOCStyleEntries[]{}
.
The first one creates a command that can be fed to the option entrynumberformat
.
However, some styles do not recognize this option, f.i. dottedtocline
. So
\DeclareTOCStyleEntries[indent=0.5em,
entrynumberformat=\linkedentrynumber]
{dottedtocline}{chapter}
will not compile.
In this case, it helps to switch to the highly flexible style tocline
, which can be easily customized so as to mimick the behavior of dottedtocline
:
\DeclareTOCStyleEntry[beforeskip=0pt,
indent=0.5em,
linefill=\TOCLineLeaderFill,
entrynumberformat=\linkedentrynumber]
{tocline}{chapter}
looks reasonably similar and works like a charm.