[Tex/LaTex] Manually customizing a header’s colour

colorheader-footer

I would like to change the background colour of a page and necessarily also the text colour. While that is no problem for the main text, the header (and footer) text colour needs to be changes separately. Consider this minimal-but not working-example:

\documentclass{report}
\usepackage{xcolor}
\begin{document}
A black and white page.
\newpage
\pagecolor{green}
%%%%%%%%
{\color{white}%
\chapter*{A white and green page}
}
%%%%%%%%
\thispagestyle{myheadings}
\makeatletter
\global\let\originaloddhead\@oddhead
\global\let\originalevenhead\@evenhead
\renewcommand{\@oddhead}{{\protect\color{white}\originaloddhead}}
\renewcommand{\@evenhead}{{\protect\color{white}\originalevenhead}}
\makeatother
\markboth{header text}{header text}
%%%%%%%%
{\color{white}%
Some text.
}
%%%%%%%%
\newpage
\nopagecolor
\makeatletter
\let\@oddhead\originaloddhead
\let\@evenhead\originalevenhead
\makeatother
Another black and white page.
\end{document}

does not work but gives a black header.

What does work is

\documentclass{report}
\usepackage{xcolor}
\makeatletter
\newcommand{\ps@inverted}{%
  \renewcommand{\@oddhead}{%
    {\protect\color{white}\small\upshape\hfil\rightmark\hskip\tw@ em\thepage}}%
  \renewcommand{\@evenhead}{%
    {\protect\color{white}\small\upshape\thepage\hskip\tw@ em\leftmark\hfill}}%
 }
\makeatother

\begin{document}
A black and white page.
\newpage
\pagecolor{black}%
{\color{white}%
\chapter*{A white and black page}
\thispagestyle{myheadings}
\markboth{header text}{header text}
\thispagestyle{inverted}%
Some text.
}
\newpage
\thispagestyle{myheadings}
\nopagecolor
Another black and white page.
\end{document}

but I would like to know why the first attempt does not work and possibly how to fix it. OK, fancyhdr, nccfancyhdr or similar packages would probably do the trick, but I don't just want a solution (I got one, see above), but to understand how things work.

Best Answer

You have a { group starting just before the \color{white} which finishes at the end of the page. It keeps the colour change local but also your definition of \originaloddhead is discarded at the end of the group. You would need

\global\let\originaloddhead\@oddhead
\global\let\originalevenhead\@evenhead
Related Question