2

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} \addtolength\cftchapnumwidth{\mylength}

Output (Toc) - only 'Chap' is in blue

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}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
  colorlinks=true,
  citecolor=PineGreen,
  linkcolor=red,
  urlcolor=blue}

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

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

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document}

Output: ToC chap color

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}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
  colorlinks=true,
  citecolor=PineGreen,
  linkcolor=red,
  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}

\addbibresource{biblatex-examples.bib}

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

% Toc for chapter \newlength\mylength \renewcommand\cftchappresnum{Chapter~} \renewcommand\cftchapfont{\hypersetup{linkcolor=blue}\bfseries} \settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum} \addtolength\cftchapnumwidth{\mylength} \renewcommand\cftchapaftersnum{:}

\begin{document}

\section*{Abstract} \addcontentsline{toc}{chapter}{Abstract}

\section*{Acknowledgements} \addcontentsline{toc}{chapter}{Acknowledgements}

\tableofcontents \thispagestyle{empty}

\chapter{Intro} Lorem \citet{sigfridsson}

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

\chapter{Test}

\addcontentsline{toc}{chapter}{Bibliography} \printbibliography

\appendix

\chapter{Conclusions}

\end{document}

Output (ToC): 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. – David Purton Dec 27 '22 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. – Shahmeer Dec 28 '22 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? – David Purton Dec 28 '22 at 23:28
  • Only chap lines to be in blue, rest including sections and subsections to be in red – Shahmeer Dec 29 '22 at 00:40

3 Answers3

2

Update to answer your revised question

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}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
  colorlinks=true,
  citecolor=PineGreen,
  linkcolor=red,
  urlcolor=blue}

\apptocmd{\appendix} {\addtocontents{toc}{% \protect\addtolength\protect\cftchapnumwidth{-\mylength}% \protect\renewcommand{\protect\cftchappresnum}{Appendix~}% \protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}% \protect\addtolength\protect\cftchapnumwidth{\mylength}}% }{}{}

\newlength\mylength \renewcommand\cftchappresnum{Chapter~} \settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum} \addtolength\cftchapnumwidth{\mylength} \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} \addcontentsline{toc}{chapter}{Abstract}

\section*{Acknowledgements} \addcontentsline{toc}{chapter}{Acknowledgements}

\tableofcontents

\chapter{Intro}

\section{A section}

\section{A subsection}

\chapter{Test}

\appendix

\chapter{Conclusions}

\section{A section}

\end{document}

output

David Purton
  • 25,884
  • 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! – Shahmeer Dec 29 '22 at 06: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. – Shahmeer Dec 29 '22 at 06:51
  • 2
    @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… – David Purton Dec 29 '22 at 06:59
  • I just added a second update my question (Update 2). Please see my new MWE. – Shahmeer Dec 29 '22 at 07:28
  • @Shahmeer, see new answer. – David Purton Dec 29 '22 at 08:22
  • This works! @David – Shahmeer Dec 30 '22 at 21:24
  • @Shahmeer, you can click the tick mark next to the answer to show that the question is solved. – David Purton Dec 30 '22 at 22:08
0

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

Try this code.

b

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

\usepackage{xcolor}% added <<<<<<<<<< \apptocmd{\appendix} {\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}% \protect\renewcommand{\protect\cftchappresnum}{\color{red}Appendix~}% changed <<<< \protect\renewcommand{\protect\cftchapfont}{\color{red}}%% added <<<<<<<<<< \protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}% \protect\addtolength\protect\cftchapnumwidth{\mylength}}% }{}{}

\newlength\mylength

\renewcommand\cftchappresnum{\color{blue}Chapter~} \settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum} \addtolength\cftchapnumwidth{\mylength} \renewcommand{\cftchapfont}{\color{blue}} % added <<<<<<<<<<

\begin{document}

\tableofcontents

\chapter{Intro}

\chapter{Test}

\appendix

\chapter{Conclusions}

\end{document}

Simon Dispa
  • 39,141
  • It does not work when adding: \usepackage[hyperfootnotes=false,english,colorlinks]{hyperref} \hypersetup{ colorlinks=true, citecolor=PineGreen, linkcolor=red, urlcolor=blue} – Shahmeer Dec 28 '22 at 22:09
0

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.

enter image description here

\documentclass{report}

\usepackage{tocloft} \usepackage{etoolbox} \usepackage[titletoc]{appendix} \usepackage[usenames,dvipsnames]{xcolor} \usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}

\hypersetup{ colorlinks=true, citecolor=PineGreen, urlcolor=blue}

% Change ToC settings for Appendix \apptocmd{\appendix} {\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}% \protect\renewcommand{\protect\cftchappresnum}{Appendix~}% \protect\renewcommand{\protect\cftchapfont}{\bfseries}% \protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}% \protect\addtolength\protect\cftchapnumwidth{\mylength}% \protect\hypersetup{linkcolor=red}}% Force red link colour }{}{}

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

\begin{document}

\hypersetup{linkcolor=blue}% All hyperlinks will be blue (this will be for chapters) \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.

Werner
  • 603,163