[Tex/LaTex] Reduce space between header and headrule

boxesfancyhdrheader-footerspacing

Problem

I'm trying to create fancy headers for my documents using fancyhdr. I want the headrule to be dotted, which can be achieved using

\renewcommand{\headrule}{\vbox to 0pt{\hbox to\headwidth{\dotfill}\vss}}

as stated in the fancyhdr-documentation on page 12. Now I also want to move the rule nearer to the content of the header. For the footer, there is \footruleskip
, but unfortunately no \footruleskip. If I just elevate the box of the headrule by using \raisebox I get

Package Fancyhdr Warning: \headheight is too small (38.32509pt): 
 Make it at least 39.58127pt.

How do I automatically adjust \headheight or how can I get rid of the overfull box produced by raising the headrule box.

Bonus: Currently I specify \setlength{\headheight}{3.5em} because of the \Huge\thechapter. How can I automatically set \headheight?

MWE

\documentclass{scrbook}
% Display box around header
\usepackage{eso-pic}
\AddToShipoutPictureFG{
    \AtTextUpperLeft{%
        \put(0,\LenToUnit{\headsep}){%
            \framebox(\LenToUnit{\textwidth},\LenToUnit{\headheight}){}%
        }%
    }%
}

\usepackage{fancyhdr}
\pagestyle{fancy}

\setlength{\headheight}{3.5em}

% From fancyhdr documentation
\fancyhf{}
\fancyhead[LE,RO]{\sffamily\chaptername\ \Huge\thechapter}
\fancyhead[RE,LO]{\sffamily\leftmark}
\begin{document}

\chapter{Test Chapter}
\eject

\renewcommand{\headrule}{\vbox to 0pt{\hbox to\headwidth{\dotfill}\vss}}
\hbox{}
\eject

\renewcommand{\headrule}{\raisebox{0.25\headheight}{\vbox to 0pt{\hbox to\headwidth{\dotfill}\vss}}}
\hbox{}

\end{document}

Output

\renewcommand{\headrule}{\vbox to 0pt{\hbox to\headwidth{\dotfill}\vss}}

enter image description here

\renewcommand{\headrule}{\raisebox{0.25\headheight}{\vbox to 0pt{\hbox to\headwidth{\dotfill}\vss}}}

enter image description here

Best Answer

Use the center field, without redefining \headrule:

\documentclass{scrbook}
\usepackage{fancyhdr}
\usepackage{picture} % directly specify lengths with their unit in picture commands
\usepackage{lipsum}
% Display box around header
\usepackage{eso-pic}
\AddToShipoutPictureFG{
    \AtTextUpperLeft{%
        \put(0,\headsep){%
            \framebox(\textwidth,\headheight){}%
        }%
    }%
}

\pagestyle{fancy}
\setlength{\headheight}{24.4pt}
\newcommand{\dotrule}{%
  \makebox[0pt][l]{%
    \hspace{-.5\textwidth}%
    \raisebox{-8pt}{%
      \makebox[\textwidth][l]{%
        \kern-.00625\textwidth
        \xleaders\hbox to 0.0125\textwidth{\hss.\hss}\hfill
        \kern-.00625\textwidth
      }%
    }%
  }%
}

% From fancyhdr documentation
\fancyhf{}
\fancyhead[LE,RO]{\sffamily\chaptername\ {\Huge\thechapter}}
\fancyhead[RE,LO]{\sffamily\leftmark}
\fancyhead[C]{\dotrule}
\renewcommand\headrulewidth{0pt}
\begin{document}

\chapter{Test Chapter}
\lipsum

\end{document}

I defined “by hand” the dot filling, because \xleaders should be used.

enter image description here

Personal comment: I rarely saw something this ugly.