I have been developing my own custom dark theme for TeXworks that I am currently using but just recently noticed a somewhat bug with the TOC. The TOC entries for the chapter are correctly being printed in white text but the sections/subsections/etc are not. This is how the MWE is currently outputting the TOC:
I have tried adding hyperref with the options suggested by Werner (namely: \usepackage[linktoc=all]{hyperref}) but this did not change the colour of the page numbers. The answer by Mico to change the color of the TOC using \begingroup, \endgroup also did not work - the page numbers for sections/subsections/etc stayed black.
What am I missing that is causing this? I am not sure why the chapter numbers are displaying correctly yet the TOC entries beneath chapter are not changing to be printed in white when I have set the \color to white as global \AtBeginDocument.
MWE:
\documentclass[oneside]{book}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{etoolbox}
\AtBeginDocument{\pagecolor{black}\color{white}}
\begin{document}
\tableofcontents
\chapter{First chapter}
\lipsum[1-2]
\section{Foo section}
\lipsum[1]
\subsection{Bar subsection}
\lipsum[1]
\chapter{Second chapter}
\lipsum[1-2]
\section{Foo section}
\lipsum[1]
\subsection{Bar subsection}
\lipsum[1]
\end{document}


\AtBeginDocumentchanged it strictly only for those page numbers or is it just one of the TeX quirks? Thanks for the quick response, worked in my main doc and I will update my answer in the first link. – JamesT Oct 01 '22 at 23:20\begin{document}but\AtBeginDocumentis too late so you set white but the document default was black. I didn't check why only some levels are enforcing the default color – David Carlisle Oct 01 '22 at 23:25\AtBeginDocumentand look there for issues, thanks for the advice and explanation. I have updated two previous answers that contained that error so hopefully they are useful to others in the future. – JamesT Oct 01 '22 at 23:27