[Tex/LaTex] Coloring toc page numbers

colorhyperreftable of contents

I'm using book class and I'm trying to achieve a toc which titles and page numbers are all clickable and hyperlinked to their corresponding sections, but with the characteristic that the page numbers have different color from their sections.

I think I need to set something inside \hypersetup, but I can't figure out how to do it.

I have tried using:

\hypersetup{linktocpage}

But it only makes clickable the page numbers; I'd like to make both page numbers and titles clickable. Here is my complete code and a picture of what I'm trying to get:

\documentclass[a4paper,11pt]{book}
\usepackage[latin1]{inputenc}
\usepackage[sc]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage{palatino}
\usepackage[english,spanish,activeacute]{babel}
\usepackage{enumerate}
\usepackage{minitoc}
\usepackage[dvipsnames,table]{xcolor}
\usepackage[pdftex, breaklinks=true, colorlinks=true,linkcolor={blue}, citecolor={black}]{hyperref}
\hypersetup{linktocpage}

\begin{document}
\dominitoc
\setcounter{secnumdepth}{6} \setcounter{tocdepth}{6}
\renewcommand{\contentsname}{Table of contents}
\tableofcontents
\pagenumbering{arabic}

\chapter{Chapter 1}
Text
\section{Section 1}
Text
\subsection{Subsection 1}
Text
\subsubsection{Subsubsection 1}
Text

\end{document}

enter image description here

Thank you so much!

Best Answer

The default setup with hyperref is to make only the sectional titles hyperlinked. However, you can use the linktoc key-value option to switch between

  • none: Neither title nor page number is hyperlinked;
  • section: Only section titles are hyperlinked;
  • page: Only page entries are hyperlinked; and
  • all: Both section titles and pages are hyperlinked.

The following minimal example changes the link colour of the page temporarily using the hooks provided by tocloft:

enter image description here

\documentclass{book}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{tocloft}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
\hypersetup{linktoc=all}

\makeatletter
\renewcommand{\cftchappagefont}{\bfseries\HyColor@HyperrefColor{red}\@linkcolor}
\renewcommand{\cftsecpagefont}{\HyColor@HyperrefColor{red}\@linkcolor}
\let\cftsubsecpagefont\cftsecpagefont % \renewcommand{\cftsubsecpagefont}{\HyColor@HyperrefColor{red}\@linkcolor}
\makeatother

\usepackage{blindtext}

\begin{document}

\tableofcontents

\blinddocument

\end{document}