# Change color of a type of text like header/footer

Can we change the color of a logical type of text, say every header or every footer, by a single command? Header and footer is using fancyhdr.

I believe it should be a \renewcommand but won't it affect dependent packages like fancyhdr? I could define newer macros from the one which fancyhdr uses like \rfoot or \cfoot, to say \rfootcolored or \cfootcolored. But then I have to redesign the entire header/footer from scratch...which I don't want to do..

A MWE..

\documentclass{book}

\usepackage{lipsum}
\usepackage{fancyhdr}

\begin{document}
\pagestyle{fancy}

\chapter{Lipsum}
\lipsum[1-6]

\section{lipsumsection}
\lipsum[7-10]

\chapter{Lipsum2}
\lipsum[2-7]

\end{document}

• Short answer: Yes. Long answer: But how to do it will depend on which part you want to modify. Jun 25, 2012 at 10:56
• @VineetMenon: You should also include information on how you generate your headers and footers, by posting a minimal example. Otherwise someone might try to answer explaining how to do it with fancyhdr, and then you might answer "Actually, I'm using memoir, and this approach isn't working". You should always try to make questions as concrete and detailed as possible.
– Jake
Jun 25, 2012 at 12:40
• Probably an ifelse statement would be the simplest way here. Jun 25, 2012 at 14:30
• i have put fancyhdr as the tag..I thought it was sufficient... Jun 26, 2012 at 4:56
• @VineetMenon: I think it would really help if you composed a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages. This will also serve as a test case and ensure that the solution actually works for you. Jun 26, 2012 at 5:13

## 1 Answer

You may use the etoolbox package to patch the internal fancyhdr macros \f@nch@head and \f@nch@foot (and in case you want colored rules, also \headrule and \footrule):

\documentclass{book}

\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{xcolor}

\usepackage{etoolbox}

\makeatletter
\patchcmd{\f@nch@head}{\rlap}{\color{red}\rlap}{}{}
\patchcmd{\headrule}{\hrule}{\color{red}\hrule}{}{}
\patchcmd{\f@nch@foot}{\rlap}{\color{green}\rlap}{}{}
\patchcmd{\footrule}{\hrule}{\color{green}\hrule}{}{}
\makeatother

\begin{document}
\pagestyle{fancy}

\chapter{Lipsum}
\lipsum[1-6]

\section{lipsumsection}
\lipsum[7-10]

\chapter{Lipsum2}
\lipsum[2-7]

\end{document}


• The relevant internal macros in the fancyhdr package have since changed from \@fancyhead and \@fancyfoot to \f@nch@head and \f@nch@foot, respectively. Jul 2, 2017 at 20:03