# After adding 'Chap' word in toc in report, change color of the entire chap lin in toc?

Referring to good working solution with MWE included in How can I add the word "Chap" before the chapters numbers in the Toc in class report? for adding word 'Chap' before chapter numbers in toc in report class, if I want to color in blue the entire the 'Chap..' line in toc, what must I do? Only the word 'Chap' is in blue and not its title. I would the entire 'Chap' toc line to be in blue.

Modifying the lines for enabling 'Chapter..' word in toc below with \color{blue}

\newlength\mylength

\renewcommand\cftchappresnum{\color{blue}{Chapter~}}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}


Update 1: (12/28/2022) It does not work when using hyperref

See my MWE

\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}
\hypersetup{
citecolor=PineGreen,
urlcolor=blue}

\apptocmd{\appendix}
\protect\renewcommand{\protect\cftchappresnum}{\bf\color{red}Appendix~}% changed <<<<
\protect\renewcommand{\protect\cftchapfont}{\bf\color{red}}
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
}{}{}

\newlength\mylength
\renewcommand\cftchappresnum{\bf\color{blue}Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\renewcommand{\cftchapfont}{\bf\color{blue}}
\renewcommand\cftchapaftersnum{:}

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document}


Output:

Update 2 (12/29/2022)

Okay I have been able to enable the entire Chap line in ToC using response from @David Purton to be in blue but now other entries such as Acknowledgements, Abstracts, Bibliography that are not meant to be chapters but needed to be added to Toc in the same format (think indent and spacing in toc) using \addcontentsline{toc}{chapter}{} I would want those entries to be in red (appendix toc line in red is ok).

See my new MWE

\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[usenames,dvipsnames]{xcolor}
\hypersetup{
citecolor=PineGreen,
urlcolor=blue}
\usepackage[
style=authoryear,
natbib=true,
backend=biber,
dashed=false,
isbn = false,
doi = false,
url = false,
language=auto,
sorting=nyt,
maxbibnames=99,
maxcitenames=2,
uniquelist=false,
uniquename=false]{biblatex}

% Change ToC settings for Appendix
\apptocmd{\appendix}
\protect\renewcommand\protect\cftchapfont{\protect\bfseries}
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
}{}{}

% Toc for chapter
\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\renewcommand\cftchapaftersnum{:}

\begin{document}

\section*{Abstract}

\section*{Acknowledgements}

\tableofcontents
\thispagestyle{empty}

\chapter{Intro}
Lorem \citet{sigfridsson}

\section{A section}
\subsection{A subsection}

\chapter{Test}

\printbibliography

\appendix

\chapter{Conclusions}

\end{document}


Output (ToC):

• I Guess since you don't show code which produces your output. But are you using the hyperref package with the colorlinks option? If so, maybe it would suit you to just set the link colour using linkcolor=blue as an option to hyperref. Dec 27, 2022 at 10:24
• Yes! I was using hyperref. I just edited to show my MWE. I want only chap line in blue and the rest red so I have to keep linkcolor=red. What should I do so that the entire chap line in Toc is in blue. Dec 28, 2022 at 22:17
• What about TOC lines for sections and subsection? What colour should they be? Maybe you could set the linkcolor to blue at the start of the TOC and change it to red at the start of the appendices? Dec 28, 2022 at 23:28
• Only chap lines to be in blue, rest including sections and subsections to be in red Dec 29, 2022 at 0:40

This no longer adjusts \hypersetup. Instead I redefine \numberline to change the colour of numbered entries in the table of contents when the following conditions are true:

• \cftchappresnum is equal to ‘Chapter~’ (this excludes appendices)
• The width of the number is > \mylength which ensures we are at a chapter level.

Only numbered chapters are blue. Everything else is red.

\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}
\hypersetup{
citecolor=PineGreen,
urlcolor=blue}

\apptocmd{\appendix}
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
}{}{}

\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\renewcommand\cftchapaftersnum{:}

\makeatletter
\newcommand{\testpresnum}{Chapter~}
\renewcommand{\numberline}[1]{%
\ifx\cftchappresnum\testpresnum
\ifdim\@tempdima>\mylength\color{blue}\fi
\fi
\hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
\makeatother

\begin{document}

\section*{Abstract}

\section*{Acknowledgements}

\tableofcontents

\chapter{Intro}

\section{A section}

\section{A subsection}

\chapter{Test}

\appendix

\chapter{Conclusions}

\section{A section}

\end{document}


• This works! I think I would love to see if I can also get the Appendix line (the entire) in toc in blue since they are technically chapter too. This would be neater for my ToC I think! Dec 29, 2022 at 6:40
• Besides, your solution also caused other entries in toc that are not meant be chapters such as abstract, acknowledgements, toc itself, lof, lot to be in blue too because I used \addcontentsline{toc}{chapter}{}. I would want those to be in red. Dec 29, 2022 at 6:51
• @Shahmeer Your question is difficult to answer because you don't give all the information needed. Unless you tell us, it's hard to predict that chapters added using \chapter should be blue but chapters added manually should not be… Dec 29, 2022 at 6:59
• I just added a second update my question (Update 2). Please see my new MWE. Dec 29, 2022 at 7:28
• @Shahmeer, see new answer. Dec 29, 2022 at 8:22

Use \renewcommand{\cftchapfont}{\color{blue}}

Try this code.

\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}

\apptocmd{\appendix}
\protect\renewcommand{\protect\cftchappresnum}{\color{red}Appendix~}% changed <<<<
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
}{}{}

\newlength\mylength

\renewcommand\cftchappresnum{\color{blue}Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document}

• It does not work when adding: \usepackage[hyperfootnotes=false,english,colorlinks]{hyperref} \hypersetup{ colorlinks=true, citecolor=PineGreen, linkcolor=red, urlcolor=blue} Dec 28, 2022 at 22:09

hyperref overrides any colour specifications you insert. So, in the below example, I've used the hyperref precedence to set the colour through specifically-placed \hypersetup{linkcolor=<color>} commands.

\documentclass{report}

\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}

\hypersetup{
citecolor=PineGreen,
urlcolor=blue}

% Change ToC settings for Appendix
\apptocmd{\appendix}
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\renewcommand{\protect\cftchapfont}{\bfseries}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
}{}{}

% Default ToC settings
\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\renewcommand{\cftchapfont}{\bfseries}
\renewcommand\cftchapaftersnum{:}

\begin{document}

\tableofcontents
\hypersetup{linkcolor=red}% If you don't have anything after the appendices you want linked in blue, this is not necessary

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document}


Since the colour changes are sent to the .toc, you'll need to compile at least twice with any change in the .toc related to that placement.