13

I try to color the page numbers of my table of contents.

I'm using the package hyperref along with the options :

\hypersetup{
  colorlinks,
  linkcolor=wine-stain % custom color
}

Here's the output :

tableofcontents


The numbers remain black..

What should I do so the numbers become the same color as defined for the sections ?

I tried to \color{wine-stain}{\tableofcontents} but still that doesn't work since "Contents" is also colorized and the subsection numbers are remaining black.

1
  • use also linktocpage
    – user2478
    Feb 20, 2013 at 20:02

3 Answers 3

18

This happens because the links are only the titles of entries in the ToC. The page numbers themselves are not hyperlinked. Add the option

linktoc=all

to your set of hyperref options, which should link both the section names as well as the page number.

enter image description here

\documentclass{book}
\usepackage{hyperref,xcolor}% http://ctan.org/pkg/{hyperref,xcolor}
\definecolor{wine-stain}{rgb}{0.5,0,0}
\hypersetup{
  colorlinks,
  linkcolor=wine-stain,
  linktoc=all
}
\begin{document}
\tableofcontents

\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection{Second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}

\section{Second section}
\subsection*{An unnumbered first subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection*{An unnumbered second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}

\chapter{Second chapter}
\section{First section}
\subsection*{An unnumbered first subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection*{An unnumbered second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}

\section{Second section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection{Second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}

\end{document}

Adding linktocpage is not sufficient, since the sectional units are then not hyperlinked. Unless, of course, this is what you're after in the first place. That is, if used, it provides a mutually exclusive switch between hyperlinking in the ToC. In fact, options related to hyperlinking from the ToC include:

  • linktoc=none: No hyperlinking in the ToC;
  • linktoc=section: Only the section titles are hyperlinked;
  • linktoc=page: Only the page numbers are hyperlinked;
  • linktoc=all: Both section titles and page numbers are hyperlinked;
  • linktocpage: Similar to linktoc=page

The linktoc key-value is undocumented, but stem from the following code in hyperref.dtx:

\chardef\Hy@linktoc@none=0 %
\chardef\Hy@linktoc@section=1 %
\chardef\Hy@linktoc@page=2 %
\chardef\Hy@linktoc@all=3 %
\ifHy@linktocpage
  \let\Hy@linktoc\Hy@linktoc@page
\else
  \let\Hy@linktoc\Hy@linktoc@section
\fi
\define@key{Hyp}{linktoc}{%
  \@ifundefined{Hy@linktoc@#1}{%
    \Hy@Warning{%
      Unexpected value `#1' of\MessageBreak
      option `linktoc' instead of `none',\MessageBreak
      `section', `page' or `all'%
    }%
  }{%
    \expandafter\let\expandafter\Hy@linktoc
    \csname Hy@linktoc@#1\endcsname
  }%
}
3
  • merci, can you eventually post a link of what the \l@chapter command is used for ? For I get a bit confused of those two lines of code you wrote though I understand the patchcmd command, thanks.
    – vdegenne
    Feb 20, 2013 at 21:01
  • @Oddant: I've updated my post since to use the (undocumented) linktoc=all key-value provided by hyperref; thereby eliminating the requirement/use of etoolbox's \patchcmd. In particular, Which solution are you interested in? The older solution is still available in the revision history.
    – Werner
    Feb 20, 2013 at 21:05
  • really good finding~ no linktoc in the doc as said, you sure are a code-digger..
    – vdegenne
    Feb 20, 2013 at 21:11
5

Here's another option, using the tocloft package and its \cftXpagefont family of commands:

\documentclass{book}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{hyperref}

\colorlet{wine-stain}{orange!80!black}% just for the example

\hypersetup{
  colorlinks,
  linkcolor=wine-stain % custom color
}

\renewcommand\cftchappagefont{\color{wine-stain}}
\renewcommand\cftsecpagefont{\color{wine-stain}}
\renewcommand\cftsubsecpagefont{\color{wine-stain}}

\begin{document}

\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

enter image description here

If the leaders (the filling dots) should alos receive color, you can also redefine the \cftXleader family of commands:

\documentclass{book}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{hyperref}

\colorlet{wine-stain}{red!80!black}

\hypersetup{
  colorlinks,
  linkcolor=wine-stain % custom color
}

\renewcommand\cftchappagefont{\color{wine-stain}}
\renewcommand\cftsecpagefont{\color{wine-stain}}
\renewcommand\cftsecleader{\color{wine-stain}\cftdotfill{\cftsecdotsep}}
\renewcommand\cftsubsecpagefont{\color{wine-stain}}
\renewcommand\cftsubsecleader{\color{wine-stain}\cftdotfill{\cftsubsecdotsep}}

\begin{document}

\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

enter image description here

Notice that both these approaches just add color.

0
\RedeclareSectionCommands[
        toclinefill=\color{teal}{\quad\dotfill},%Color of dots set here,\hypersetup{linktotoc=all} not effect to dotscolor in \usepackage{hyperref} about texlive2019
        tocraggedpagenumber=true,
        tocindent=0pt,
        tocentryformat=\color{olive}{\sffamily\fontsize{12pt}{18pt}\selectfont},%This will effect, need to set \hypersetup{colorlinks=false},otherwise, \hypersetup{linkcolor=teal} will effect to "entry of Contents", like section name, subsection name etc.
        tocpagenumberformat=\color{purple}{\fontsize{12pt}{18pt}\selectfont}%
]{chapter,section,subsection}

You can see KOMA-Script: Pagenumber immediately after heading in table of contents

You must log in to answer this question.

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