2

I'm a bit confused with the behaviour of fancyhdr.

  1. When I change the \headheight for a \fancypagestyle to include a picture (here a tikz placeholder), why is the footer as well as the bottom margin not respected, but also moved (see first page of output)? How can I fix that?
  2. Why does the \headheight of the first page with one \fancypagestyle seemingly influence the headheight of the second page with a different \fancypagestyle and different \headheight (see second page of output)?

A MWE and its output follows. Thanks in advance!

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{tikz}

%header first page \fancypagestyle{firststyle}{ \setlength{\headheight}{130pt} \fancyhead[L]{} \fancyhead[C]{\begin{tikzpicture} \draw[fill=red] (0,0) rectangle (\textwidth,5); \end{tikzpicture}} \fancyhead[R]{} \fancyfoot[C]{\thepage} \renewcommand{\headrulewidth}{0pt} }

%main header \fancypagestyle{main}{ \setlength{\headheight}{10pt} \fancyhead[L]{Test Left} \fancyhead[C]{} \fancyhead[R]{Test Right} \fancyfoot[C]{\thepage}}

\pagestyle{main}

% \begin{document} \thispagestyle{firststyle} \lipsum \end{document}

The output: Page one, footer not respected (see arrow).

Page two, seemingly same headheight, but different fancypagestyle (see arrow).

Manuel
  • 321

1 Answers1

2

If you keep the geometry set by the class, you must leave a vertical space on the first page, to make place for the expanded heading, and activate the \pagestyle{main} to start from the second page onwards.

out2

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{tikz}

\usepackage{showframe} % added

%header first page \fancypagestyle{firststyle}{% %\setlength{\headheight}{130pt} % not needed, will add unnecessary vspace \fancyhead[L]{} \fancyhead[C]{\begin{tikzpicture} \draw[fill=red] (0,0) rectangle (\textwidth,5); \end{tikzpicture}} \fancyhead[R]{} \fancyfoot[C]{\thepage} \renewcommand{\headrulewidth}{0pt} }

%main header \fancypagestyle{main}{% \setlength{\headheight}{20pt} %changed \fancyhead[L]{Test Left} \fancyhead[C]{} \fancyhead[R]{Test Right} \fancyfoot[C]{\thepage}}

%\pagestyle{main}

\begin{document}

\vspace*{120pt} \thispagestyle{firststyle}

1.8. \lipsum[1-8]

\pagestyle{main}

  1. \lipsum

\end{document}

As an alternative, if you want to control the geometry of all pages and only insert the figure in the first page:

alt

can be done using geometry

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{tikz}

\usepackage[top=30pt,bottom=30pt,left=80pt,right=80pt,includeheadfoot, headheight=2ex, headsep=20pt]{geometry} %added

\usepackage{showframe} %added \renewcommand*\ShowFrameColor{\color{blue}} %added

%main header

\fancypagestyle{main}{% \setlength{\headheight}{2ex} % changed \fancyhead[L]{Test Left} \fancyhead[C]{} \fancyhead[R]{Test Right} \fancyfoot[C]{\thepage}}

\begin{document} \vspace*{-45pt} \noindent\begin{tikzpicture} \draw[fill=red] (0,0) rectangle (\textwidth,5); \end{tikzpicture}

1.8. \lipsum[1-8]

\pagestyle{main}

  1. \lipsum

\end{document}

Simon Dispa
  • 39,141
  • Thanks, helps a lot! You're absolutely right: if only an image is inserted and if this happens only at the first page, there is no need for actually defining a fancypagestyle. – Manuel Jan 01 '21 at 09:45