[Tex/LaTex] Reduce space between header and headrule in standard fancyhdr, no dotfill required

fancyhdrspacing

I want to reduce the space between the headrule and the header itself in the standard fancyhdr package. All topics with similar title did not gave the solution for me.
Please help me with this simple question.

Best Answer

You can use a redefinition of \headrule to add a convenient (negative) skip before the rule is typeset; the following example code shows the necessary redefinition using -2pt (change this value according to your needs in the line marked % change here)

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}

\makeatletter
\def\headrule{{
  \if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
  \vskip-2pt% change here
  \hrule\@height\headrulewidth\@width\headwidth   
  \vskip-\headrulewidth%
}}
\makeatother

\pagestyle{fancy}

\begin{document}
\chapter{A test chapter}
\section{A test section}
\lipsum[1-40]
\end{document}