7

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?

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

1 Answer 1

7

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}
1
  • Thank you very much! I had tried exactly this, but with \newcommand{\oldheadrule}{\headrule}, which resulted in an infinite loop. Commented Mar 14, 2019 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .