2

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?

  • 2
    You surely find, in the log file, a warning issued by fancyhdr about \headheight. – egreg Oct 31 '13 at 11:48
  • And the spaces after the opening braces and some of the closing braces matter Use {% to hide them. – Ulrike Fischer Oct 31 '13 at 11:50
  • @egreg please, could you write the answer? – doed Nov 3 '13 at 17:49
  • The example code does not give the result of the images, because \ does not result in a line break but only a inter word space. – Schweinebacke May 16 '17 at 7:56
3

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}
  • Instead of changing \headheight I would recommend to use KOMA-Script option headheight or headlines so that typearea can take the large head into account while calculation of page areas. Also option headinclude could be useful with such a large head. – Schweinebacke May 16 '17 at 7:53
5

Usage of fancyhdr with a KOMA-Script class is not recommended. Nevertheless, if you switch to scrlayer-scrpage you will get a similar warning message, because the default height of the head is only one line. But KOMA-Script provides options headheight and headlines to setup the page with a larger head. In the example you are using three lines, so headlines=3 should be a good choice:

\documentclass[headlines=3]{scrartcl}
\usepackage{ifthen}
\usepackage{scrlayer-scrpage}
\manualmark
\setkomafont{pagehead}{\bfseries}
\chead{\ifnum\value{page}=1\else foo\\bar\\baz\fi}

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

enter image description here

And if you don't want the head elements being centered but a left aligned block that is centered, you may use, e.g., a tabular inside the head definition. And to remove the slanted font, set also the font element pageheadfoot:

\documentclass[headlines=3]{scrartcl}
\usepackage{ifthen}
\usepackage{scrlayer-scrpage}
\manualmark
\setkomafont{pageheadfoot}{}
\setkomafont{pagehead}{\bfseries}
\chead{\ifnum\value{page}=1\else \begin{tabular}{@{}lll@{}}foo\\bar\\bazooka\end{tabular}\fi}

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

I would also suggest not to refer to the page number in the head but to use two different page styles:

\documentclass[headlines=3]{scrartcl}
\usepackage{ifthen}
\usepackage{scrlayer-scrpage}
\manualmark
\setkomafont{pageheadfoot}{}
\setkomafont{pagehead}{\bfseries}
\chead{\begin{tabular}{@{}lll@{}}foo\\bar\\bazooka\end{tabular}}

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.