[Tex/LaTex] scrartcl and fancyhdr: Strange spacing behaviour

fancyhdrheader-footerkoma-scriptspacing

Consider the following piece of code:

\documentclass{scrartcl}
\usepackage{ifthen}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{
    \ifthenelse{\equal{\thepage}{1}}{}{
                    \textbf{foo}\\
                    \textbf{bar}\\
                    \textbf{baz}
        }
}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\newpage
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\newpage
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{document}

This is how pages 2 and 3 of the example look like:

enter image description here

as you can see the rows of x are not at the same height. Additionall baz does not start where foo and bar do – it is a bit more at the left.

Do you have any idea what the problem might be?

Best Answer

This is a well known and documented behavior of fancyhdr. If at the first usage of the defined header the package finds that the header is vertically bigger than \headheight, it will issue a message and change \headheight for the subsequent pages.

You will find the following message in your .log file:

Package Fancyhdr Warning: \headheight is too small (17.0pt):
 Make it at least 38.8842pt.
 We now make it that large for the rest of the document.
 This may cause the page layout to be inconsistent, however.

Thus you know that you need to set:

\setlength{\headheight}{39pt}

in your preamble (rounding is better):

\documentclass{scrartcl}
\usepackage{ifthen}

\setlength{\headheight}{39pt}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{%
  \ifthenelse{\equal{\value{page}}{1}}%
    {}%
    {\bfseries
     \makebox[0pt][r]{\smash{\vrule height 1cm depth 2cm}}boo\\
     boo\\
     boo%
    }%
}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\newpage
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\newpage
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{document}