4

The answer to the question fancyhdr - Change horizontal line color on top of footer is not completely satisfactory, because it changes the position of the header line, for example with this example.

\documentclass{article}

\usepackage{xcolor}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{red}\leaders\hrule height \headrulewidth\hfill}}
\fancyhead[LE,RO]{Hello}
\pagestyle{fancy}

\begin{document}
    hello
\end{document}

Is it possible to change only the color of the line, without changing its position?

  • 1
    A simple \renewcommand{\headrule}{{\color{red}\rule{\textwidth}{0.5pt}}} should do the trick. – Bernard Mar 14 at 18:33
  • Thanks, but it does not (at least not on my computer). – Rastapopoulos Mar 14 at 18:34
  • The position of the line isn't changed, neither with your code nor with mine. How is it modified, precisely? – Bernard Mar 14 at 19:15
3

Below I save the original \headrule in \oldheadrule, and then prepend \color{<colour>} to a newly-defined \headrule that just calls \oldheadrule. The approach is similar to etoolbox's \pretocmd.

enter image description here

\documentclass{article}

\usepackage{fancyhdr,xcolor}

\let\oldheadrule\headrule% Copy \headrule into \oldheadrule
\renewcommand{\headrule}{\color{red}\oldheadrule}% Add colour to \headrule
\renewcommand{\headrulewidth}{0.5pt}
\fancyhead{Hello}
\pagestyle{fancy}

\begin{document}

Lorem ipsum\ldots

\clearpage

Lorem ipsum\ldots

\end{document}
  • Thank you very much! I had tried exactly this, but with \newcommand{\oldheadrule}{\headrule}, which resulted in an infinite loop. – Rastapopoulos Mar 14 at 19:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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