1

Introduction

I would like to create a table of contents with links in all text and page number but with different colours for the text and page numbers and it should compile with pdflatex.

Work done

I have the (minimal) working code below.

\documentclass[11pt, letterpaper, openany, oneside]{book}

\usepackage{sectsty}
\usepackage{titlesec}
\usepackage{color}

\usepackage[bookmarks,hypertexnames=false,debug,linktocpage=true,hidelinks]{hyperref}

\hypersetup{
    colorlinks,
    linktoc=all,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}



\begin{document}

\titleformat{\chapter}{\color{red}\normalfont\Huge\bfseries}{\thechapter}{1em}{}[]
\sectionfont{\color{red}}
\subsectionfont{\color{red}}

\tableofcontents

\chapter{Chapter One}
\section{A Section}
Text goes here.
\section{Another Section}
Text goes here.
\subsection{A Sub Section}
Text goes here.
\section{One more section Section}
Text goes here.

\chapter{Chapter One}
Text goes here.
\section{A Section}
Text goes here.
\section{Another Section}
Text goes here.

\end{document}

This gives the following output for the table of contents:

enter image description here

It also gives the following output for a chapter

enter image description here

Desired outcome

I would like to have a table of contents where all text (chapter headings, section headings, subsection headings, page number etc.) are hyperlinks to the relevant section but have customized colours for the section headings, subsection headings and page numbers in the table of contents.

It should also compile with pdflatex (not xelatex, lualatex etc.).

Here is a summary of the desired output:

enter image description here

1 Answer 1

2

Here I patch the \contentsline command to locally change the link color used in title part of toc entry (compared to the page number part), per toc type.

In the following example,

  • the title part of chapter toc entry is changed to red by \def\toccolor@chapter{red}, and
  • the title part of section and subsection toc entries are changed to black by \def\toccolor@section{black} and \def\toccolor@subsection{black}, respectively.

To apply this for other types of toc entries,

  1. find the corresponding auxiliary file. For example, \listoftables uses aux file .lot.
  2. find the type of toc entry this list uses. For example, in .lot file you can see lines like \contentsline {table}{\numberline {1}{\ignorespaces Title}}{1}%, then table (the first argument of \contentsline) is the type name.
  3. provide corresponding \toccolor@<type> command. In the previous example, with \def\toccolor@table{<color>}, the color of title part of toc entries table will typeset in <color>.
\documentclass[11pt, letterpaper, openany, oneside]{book}

\usepackage{sectsty}
\usepackage{titlesec}
\usepackage{color}
\usepackage{xpatch}

\usepackage[bookmarks,hypertexnames=false,debug,linktocpage=true,hidelinks]{hyperref}

\hypersetup{
    colorlinks,
    linktoc=all,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}


\titleformat{\chapter}
  {\color{red}\normalfont\Huge\bfseries}
  {\thechapter}{1em}
  {}[]
\sectionfont{\color{red}}
\subsectionfont{\color{red}}


\makeatletter
% redefine \contentsline, locally change linkcolor
\IfPackageAtLeastTF{hyperref}{2022-11-13}{\@tempswatrue}{\@tempswafalse}
\if@tempswa
  % see https://github.com/latex3/hyperref/commit/7bf9ab4c99c434c30d0d00771326ae3d487fb092
  \xpatchcmd\contentsline
    {%
      \csname l@#1\endcsname{%
        \Hy@toclinkstart{#2}\Hy@toclinkend
      }{%
        \Hy@toclinkstart{#3}\Hy@toclinkend
      }%
    }
    {%
      \csname l@#1\endcsname{%
        \begingroup
          \def\@linkcolor{black}%
          \Hy@toclinkstart{#2}\Hy@toclinkend
        \endgroup
      }{%
        \Hy@toclinkstart{#3}\Hy@toclinkend
      }%
    }
    {}{\PatchFailedII}
\else
  \xpatchcmd\contentsline
    {%
      \csname l@#1\endcsname{%
        \hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
      }{%
        \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
      }%
    }
    {%
      \csname l@#1\endcsname{%
        \begingroup
          \Hy@toccolor{#1}%
          \hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
        \endgroup
      }{%
        \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
      }%
    }
    {}{\PatchFailedI}
\fi

% choose toc linkcolor, #1 is first arg of \contetnsline, 
% eg "chapter", "section"
\def\Hy@toccolor#1{%
  \ifcsname toccolor@#1\endcsname
    \edef\@linkcolor{\csname toccolor@#1\endcsname}%
  \fi
}

\def\toccolor@chapter{red}
\def\toccolor@section{black}
\def\toccolor@subsection{black}
\makeatother

\begin{document}
\tableofcontents

\chapter{Chapter One}
\section{A Section}
Text goes here.
\section{Another Section}
Text goes here.
\subsection{A Sub Section}
Text goes here.
\section{One more section Section}
Text goes here.

\chapter{Chapter One}
Text goes here.
\section{A Section}
Text goes here.
\section{Another Section}
Text goes here.

\end{document}

enter image description here

8
  • Thank you! Is is possible to generalize this so that the text appears black for \listoftables, \listoffigures etc.?
    – NM_
    Jul 30, 2020 at 18:36
  • 1
    @NM_ You can add \def\toccolor@table{black} and \def\toccolor@figure{black}. Jul 30, 2020 at 18:45
  • thank you. This works for figures and tables, however subfigures in the list of figures still appear in the link colour. I also have some algorithms created using algorithm2e and the text for the algorithms appears in the link colour. Is it possible to fix this too? Thank you.
    – NM_
    Jul 30, 2020 at 18:54
  • @NM_ I've added a general guide. See my updated answer. Jul 30, 2020 at 19:29
  • great thank you! I will try to use it to figure out the remaining parts. thank you so much!
    – NM_
    Jul 30, 2020 at 19:40

You must log in to answer this question.

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