[Tex/LaTex] Hyperlink only Section Number in Table of Contents

table of contents

I'm trying to get my table of contents to hyperlink only the section numbering and the page number, like this:

Table of contents example

But I've only managed to get the page number to link with

 linktocpage = true

can anyone help with getting the section numbering (but not the titles) hyperlinked as well? Thanks.

Best Answer

I couldn't figure out how to treat sections different than chapters, even though argument #1 contains either "chapter" or "section" (or "part" or ...). String compares just weren't working. Let me know if you want the page numbers to be linked as well.

\documentclass{book}

\let\stdnumberline\numberline
\let\stdcontentsline\contentsline
\usepackage{hyperref}

\begin{document}
\renewcommand{\contentsline}[4]%
{\bgroup
  \edef\anchorname{#4}
  \def\numberline#1{\stdnumberline{\hyperlink{\anchorname}{#1}}}
  \stdcontentsline{#1}{#2}{#3}
\egroup}

\tableofcontents

\chapter{One}

\section{First section}

\section{Second section}

\end{document}
Related Question