3

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.

1 Answer 1

3

Based on Ulrikes answer you could use

\documentclass{scrbook}
\usepackage[linktoc=page,colorlinks]{hyperref}

\makeatletter
\newcommand*\linkedentrynumber[1]{%
  \ifx\Hy@tocdestname\ltx@empty
    #1
  \else
   \hyper@linkstart{link}{\Hy@tocdestname}#1\hyper@linkend
  \fi
}
\makeatother

\DeclareTOCStyleEntries[
  entrynumberformat=\linkedentrynumber
]{tocline}{part,chapter,section,subsection,subsubsection,paragraph,subparagraph,figure,table}

\begin{document}
\tableofcontents
\part{Foo}
\chapter{Bar}
\section{Baz}
\end{document}

enter image description here

Or you could use the undocumented (ie. they could change in the future) hooks \numberline@numberprefix and \numberline@numberpostfix provided by tocbasic:

\documentclass{scrbook}
\usepackage[linktoc=page,colorlinks]{hyperref}

\makeatletter
  \g@addto@macro{\numberline@numberprefix}{%
    \ifx\Hy@tocdestname\ltx@empty\else\hyper@linkstart{link}{\Hy@tocdestname}\fi
  }
  \g@addto@macro{\numberline@numberpostfix}{%
    \ifx\Hy@tocdestname\ltx@empty\else\hyper@linkend\fi
  }
\makeatother

\begin{document}
\tableofcontents
\part{Foo}
\chapter{Bar}
\section{Baz}
\end{document}
1
  • Thanks, works great. I should mention that solution 2 (the undocumented tocbasic hooks) works out of the box and for all TOCStyleEntrys. While solution 1 uses the option entrynumberformat, so it will only work for an entry style that can take this option, such as tocline (as in your example). In my real-life setup I used dottedtocline for some sectioning levels, so solution 1 wouldn't compile. But I can switch to the highly flexible tocline and mimick the behavior of dottedtocline. (Sorry for not mentioning this in the MWE, I will edit my question accordingly.)
    – marquinho
    Mar 25, 2021 at 22:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .