1

My actual situation

I set (with the following commands) that in the table of contents each chapter element is represented with the color steelblue, while sections and subsections are represented in black.

\RequirePackage{titletoc}
\setcounter{tocdepth}{2}

\titlecontents{chapter}[0cm]
{\addvspace{14pt}\large\sffamily\bfseries}
{\color{steelblue}\contentslabel[\Large\thecontentslabel]{1.25cm}}{}
{\color{steelblue}\normalsize\sffamily\bfseries
 \;\titlerule*[.5pc]{.}\;\thecontentspage}

\titlecontents{section}[0cm]
{\addvspace{6pt}\sffamily\bfseries}
{\contentslabel[\thecontentslabel]{1.25cm}}{}
{\sffamily\hfill\color{black}\thecontentspage}[]

enter image description here

The problem is that my toc does not create any links to the referenced pages.
Therefore, in order to generate a toc with the links, I thought to add the hyperref package

The issue

After adding the package named above, I cannot determine how to color the chapters of the toc (steelblue) differently from all other components.
I have found a workaround, which I mark in the following code, but I am not satisfied with it as the clickable reference is only placed on the page number (and not on the chapter/section/... "tag")

\hypersetup{
    bookmarksnumbered=true,     
    bookmarksopen=true,         
    bookmarksopenlevel=3,   
    colorlinks, 
    breaklinks,
    linkcolor=blue,
    citecolor=blue,
    filecolor=black,
    linktocpage=true, 
}

enter image description here

My goal

I am trying to figure out how to change just the color of the chapter links in the toc of my project.
Unfortunately, just putting colorlinks=true in the hypersetup everything becomes monochrome (which is red by default, but even changing it with linkcolor the issue is still the same).
enter image description here

MWE

main.tex

\documentclass{mybook}

\renewcommand{\familydefault}{\sfdefault}
\setlength{\parindent}{0pt}

\begin{document}
\tableofcontents

%---------------------------------------------------------------------------
% Chapters
%---------------------------------------------------------------------------
\chapter{Chapter 1}
\section{Section 1.1}
\section{Section 1.2}

\chapter{Chapter 2}

\end{document}

mybook.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mybook}
\LoadClass[11pt]{book}

% Colors
%--------------------------------------------------------------------------
\RequirePackage{xcolor}
\definecolor{steelblue}{RGB}{70,130,180}

% Table of contents styling
%--------------------------------------------------------------------------
\RequirePackage{titletoc}
\setcounter{tocdepth}{2}

\titlecontents{chapter}[0cm]
{\addvspace{14pt}\large\sffamily\bfseries}
{\color{steelblue}\contentslabel[\Large\thecontentslabel]{1.25cm}}{}
{\color{steelblue}\normalsize\sffamily\bfseries
 \;\titlerule*[.5pc]{.}\;\thecontentspage}

\titlecontents{section}[0cm]
{\addvspace{6pt}\sffamily\bfseries}
{\contentslabel[\thecontentslabel]{1.25cm}}{}
{\sffamily\hfill\color{black}\thecontentspage}[]
1

1 Answer 1

1

You can use \hypersetup{<options>} as in How to use different colors for different \href commands?

\titlecontents{chapter}[0cm]
{\hypersetup{linkcolor=black}\color{green}\addvspace{14pt}\large\sffamily\bfseries} %<!---- NEW BIT
{\hypersetup{linkcolor=steelblue}\contentslabel[\Large\thecontentslabel]{1.25cm}}   %<!---- NEW BIT
{}
{\normalsize\sffamily\;\color{red}\titlerule*[.5pc]{.}\;\thecontentspage}

The code below gives the following:

enter image description here

Here's a complete edited version of mybook.cls; I've marked the new bits.

mybook.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mybook}
\LoadClass[11pt]{book}

% Colors
%--------------------------------------------------------------------------
\RequirePackage{xcolor}
\definecolor{steelblue}{RGB}{70,130,180}

% Table of contents styling
%--------------------------------------------------------------------------
\RequirePackage{titletoc}
\RequirePackage{hyperref}   %<!-------- NEW BIT
\setcounter{tocdepth}{2}

%
% default hyperref settings
%
\hypersetup{
    bookmarksnumbered=true,
    bookmarksopen=true,
    bookmarksopenlevel=3,
    colorlinks,
    breaklinks,
    linkcolor=blue,
    citecolor=blue,
    filecolor=black,
    linktoc=all,  %<!--------- NEW BIT
}

%
% chapter
%
\titlecontents{chapter}[0cm]
{\hypersetup{linkcolor=black}\color{green}\addvspace{14pt}\large\sffamily\bfseries} %<!---- NEW BIT
{\hypersetup{linkcolor=steelblue}\contentslabel[\Large\thecontentslabel]{1.25cm}}   %<!---- NEW BIT
{}
{\normalsize\sffamily\;\color{red}\titlerule*[.5pc]{.}\;\thecontentspage}

% 
% section
%
\titlecontents{section}[0cm]
{\hypersetup{linkcolor=yellow}\addvspace{6pt}\sffamily\bfseries}        %<!---- NEW BIT
{\hypersetup{linkcolor=orange}\contentslabel[\thecontentslabel]{1.25cm}}%<!---- NEW BIT
{}
{\sffamily\hfill\color{black}\thecontentspage}[]
2
  • That's almost perfect! Just a question: what if I want to keep the number before "Chapter X" the same color? I've tried many options so far, but think I'm still missing something. Thanks in advance! Oct 16, 2022 at 9:18
  • @LukeTheWolf see update :)
    – cmhughes
    Oct 16, 2022 at 20:56

You must log in to answer this question.

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