5

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

1
  • 1
    If you \smash{\Huge\thechapter} and use \renewcommand{\headrule}{\raisebox{0.25\headheight}{\makebox[\headwidth]{\dotfill}}}, then I don't get any warnings. You still have to set \headheight manually.
    – Werner
    Nov 30, 2013 at 14:22

1 Answer 1

4

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.

3
  • Thank you for your answer and your opinion. I'm trying to recreate the pagestyle of this thesis (70MB). Do you have any suggestions for a more pleasing header? How do you get the 24.4pt for \headheight? Nov 30, 2013 at 16:06
  • @HenriMenke Just don't set \headheight (or set it to zero) and look in the .log file for the warning fancyhdr emits.
    – egreg
    Nov 30, 2013 at 16:10
  • @HenriMenke The layout is not bad, but it has many defects. The chapter number in the header is too big; the dots have no place either. Usage of color is inconsistent: it doesn't make sense to use gray and colored backgrounds. The equations with a background have the equation number misplaced. Some notes are introduced by a blue title, others by a title with a dotted line. Skimming through it gives an overall impression of inconsistency.
    – egreg
    Nov 30, 2013 at 16:23

You must log in to answer this question.

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