1

How can I make page numbers red in table of contents? I have

  • colorlinks=true, linkcolor=blue, so standard internal links are blue;
  • linctoc=all, so both table of contents entry names and page numbers are links (the dots themselves are not);
  • \hypersetup{linkcolor=black} in the group generating the table of contents, so the links in the table of contents are black instead of blue everywhere else.

Finally, I hoped to change the color of the page numbers (to red, for example) in table of contents independently from the entry name color (black in this instace). As a start, I added \renewcommand{\cftsecpagefont}{\color{red}} to change the page numbers of sections, but it is ignored.

(It is also ignored when placed inside the group generating the table of contents. Furthermore, it is ignored even if \hypersetup{linkcolor=black} is removed from the group. Conversely, \renewcommand{\cftsecpagefont}{\bfseries} has the intended effect...)

\documentclass{book}
\usepackage{tocloft}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false, linktoc=all]{hyperref} % blue internal links for document; in TOC, entry name and page nr are links

\renewcommand{\cftsecpagefont}{\color{red}} % does not work

\begin{document}
    {\hypersetup{linkcolor=black} % black links for table of contents (but you may comment this out, red color still not added)
    \tableofcontents} 
    
    \chapter{Example chapter}
    
    \section{Example section}
    
    \subsection{Example subsection}
    
This is some text.
\end{document}

The question Change the page numbers in the Table of Contents to red by A H is not a duplicate: there linktoc=all is replaced with linktocpage. This removes links from entry names which is not desired.

1 Answer 1

2

You could hide the link colour in the toc:

\documentclass{book}
\usepackage{tocloft}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false, linktoc=all]{hyperref} % blue internal links for document; in TOC, entry name and page nr are links

\renewcommand{\cftsecpagefont}{\color{red}} % does not work

\begin{document}
    {\hypersetup{hidelinks}
    \tableofcontents} 
    
    \chapter{Example chapter}
    
    \section{Example section}
    
    \subsection{Example subsection}
    
This is some text.
\end{document}

enter image description here

2
  • This works, thank you. However, I am having trouble extending it. Namely, the idea is to have it show red on screen, but always be black when printed (some type of ocgx2 solution). Probably, I should ask a new question as it is, strictly speaking, beyond the scope here. Aug 21 at 17:44
  • Follow-up question tex.stackexchange.com/q/694045/116907 is up. Aug 21 at 18:19

You must log in to answer this question.

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