Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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
share|improve this answer
    
Ah, yes, smacking my head! +1! I corrected the question. But also with that the colour change for the header is not regarded? –  Stephen Jun 20 '12 at 8:52
    
move the \newpage up a line so the previous page is output within the scope not just after it. You can get away with this as you have forced page breaks, in general what you should do is define a new page style which is like the old one but with coloured header then use \thispagestyle{colouredhead} and latex will take care if using the right header on the right page. –  David Carlisle Jun 20 '12 at 9:31
    
OK, moving the \newpage into the group makes sense, but when I tested this, it had no effect (header still black). To make things easier I limited the text white just to the text, but that does not work either. As you can see from the second (working) example, I defined a new pagestyle, and that indeed works. For just 1 single page with header, \chapter* and 2 lines of text this is probably overkill, but I'm using that. I'm just curious why the other approach did not work. When I make a mistake here, I'll probably also make it somewhere else. (And I'd like to understand how things work.) –  Stephen Jun 20 '12 at 10:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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