1

I'm using fancyhdr. I want to make the header and footer rules red. When I do, the lines contract slightly, no longer the width of the paper.

First, the normal result when I use the following code:

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

The result looks normal, like the following showing the top left corner of the page:

enter image description here

Research showed the following code changes the color, and it does:

\renewcommand{\headrulewidth}{1pt}
\renewcommand{\headrule}{\hbox to\headwidth{
 \color{red}\leaders\hrule height \headrulewidth\hfill}}

But now the length of the rule is slightly less than the paper, about half a character, which of course does not look good:

enter image description here

It is also lower than desired, like there is a blank line after the header text, not sure why. The red footer, using similar code, is also too short, but does not have the same "extra space" problem.

Advice greatly appreciated.

1
  • Thanks, it corrects the length. Two problems remain. (1) There is the odd extra line below the header text, which makes the paper look quite off. (2) And for the footer, I tried just changing "head" to "foot" with "\renewcommand{\footrule}{\hbox to\footwidth{\color{red}\leaders\hrule height\footrulewidth\hfill}}" but it generated errors. To provide an example, it uses a Journal Article class, so is there a way to upload a zip file with all the files, or would a Dropbox link work? Dec 7, 2023 at 15:35

1 Answer 1

3

\headrule is defined as part of \f@nch@initialise by

\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
      \hrule\@height\headrulewidth\@width\headwidth
      \vskip-\headrulewidth}}%

and your attempt fails in several ways. For instance the line is shorter because of the unprotected endline; but you also get two unwanted pieces of vertical space, one above and one below the line. This is because \hrule doesn't add interline space fore and aft, but \hbox does.

Leaving out the fancyplain part might be OK, but I see no point in omitting it.

\documentclass{article}
\usepackage[a6paper]{geometry}% just for a smaller picture
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage{lipsum}

\makeatletter
\renewcommand{\headrule}{{% <--- !!!
  \if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
  \color{red}% <--- !!!
  \hrule\@height\headrulewidth\@width\headwidth
  \vskip-\headrulewidth
}}
\renewcommand{\footrule}{{% <--- !!!
  \if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
  \color{red}% <--- !!!
  \hrule\@width\headwidth\@height\footrulewidth
}}
\makeatother

\pagestyle{fancy}
\fancyhead{}\fancyhead[R]{\leftmark}
\renewcommand{\footrulewidth}{0.4pt}

\begin{document}

\section{Test}

\lipsum

\end{document}

enter image description here

5
  • Thanks, that works nicely - full length, no extra space. What should I change for the footer? I tried changing "head" to "foot" throughout, but got an error that there is no such thing as "footrule". Dec 7, 2023 at 16:38
  • @LivingInternet \footrule is defined in fancyhdr.sty just below \headrule. The definition is essentially the same.
    – egreg
    Dec 7, 2023 at 17:09
  • Sorry, maybe it is not footrule that is the problem. I also changed the command to "footrulewidth", "plainfootrulewidth", and "footwidth". One of those must not be right? The error I got was "! Undefined control sequence. \footrule ...ht \footrulewidth \@width \footwidth \vskip -\footrulewidth }" Dec 7, 2023 at 17:12
  • @LivingInternet Look at the modified answer.
    – egreg
    Dec 7, 2023 at 17:36
  • Awesome, does everything I need. Thanks so much, really appreciate it. Dec 7, 2023 at 17:53

You must log in to answer this question.

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