2

I am writing a thesis and have the first two pages of each chapter colored gray with white text, before starting with the sections on white pages. I now want a partial TOC in each chapter, on these gray pages, and thus in white text. Although the section titles are now colored white, the page numbers are still black. How do I get the page numbers to be white as well in the partial TOC (while remaining black in the main TOC)?
Edit: The main TOC contains some lines of text to divide the contents into parts (this is just in the contents for clarity, the parts are not coded or numbered in any way). This seems not compatible with the solution provided.

MWE:

\documentclass{book}
\usepackage{titletoc,xcolor}
\usepackage[pagecolor=none]{pagecolor}

\begin{document}
\tableofcontents
\addtocontents{toc}{\setcounter{tocdepth}{0}}

\color{white}
\chapter{This is Chapter 1}
\thispagestyle{empty}\newpagecolor{gray}

\newpage\thispagestyle{empty}
\startcontents[section]
\printcontents[section]{}{1}{\addtocontents{ptc}{\setcounter{tocdepth}{2}}}

\newpage\newpagecolor{white}
\color{black}
\section{bla}\section{bla}

\addtocontents{toc}{\smallskip \textit{Modeling} \medskip \par}

\chapter{This is Chapter 2}%would be made up like Chapter1; omitted here for clarity
\chapter{This is Chapter 3}

\addtocontents{toc}{\smallskip \textit{Experiments} \medskip \par}

\chapter{This is Chapter 4}
\chapter{This is Chapter 5}

\end{document}

1 Answer 1

2

The trick used here is that I define my page-numbers colour as a variable. Like this, I am able to switch it wherever I want. I define it as black by default. In front of the partial TOC, I do a \colorlet{MyColor}{white} which switches to your desired colour (here white) and after the partial TOC, I switch it back to black. The last switch is not needed in this example, but just to be sure...

If you like, you may colour each chapter differently like this.

% arara: pdflatex
% arara: pdflatex

\documentclass{book}
\usepackage{titletoc,xcolor}
\usepackage[pagecolor=none]{pagecolor}
\definecolor{MyColor}{named}{black}

\makeatletter
\let\oldl@chapter\l@chapter
\def\l@chapter#1#2{\oldl@chapter{#1}{\textcolor{MyColor}{#2}}}

\let\old@dottedcontentsline\@dottedtocline
\def\@dottedtocline#1#2#3#4#5{%
\old@dottedcontentsline{#1}{#2}{#3}{#4}{{\textcolor{MyColor}{#5}}}}
\makeatother

\begin{document}
\tableofcontents
\addtocontents{toc}{\setcounter{tocdepth}{0}}

\color{white}
\chapter{This is Chapter 1}
\thispagestyle{empty}\newpagecolor{gray}
\newpage\thispagestyle{empty}
\colorlet{MyColor}{white}
\startcontents[section]
\printcontents[section]{}{1}{\addtocontents{ptc}{\setcounter{tocdepth}{2}}}
\colorlet{MyColor}{black}

\newpage\newpagecolor{white}
\color{black}
\section{bla}
\section{bla}
\end{document}

enter image description here

2
  • Thanks! Incorporated this into my actual file and text color switches perfectly. However, I had some text lines between chapters in the main TOC like \addtocontents{toc}{\smallskip \textit{Modeling} \medskip \par} to divide it in parts, and this now fails. Any idea how I can fix this?
    – sue
    Jun 26, 2014 at 20:14
  • Could you please adapt your MWE in order to show what you mean? Or put it as "edit"-code in your OP. I will have a look on this later then.
    – LaRiFaRi
    Jun 26, 2014 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.