[Tex/LaTex] Fancyhdr adding multiline header causes footer margin to be inconsistent

fancyhdrheader-footermargins

I have been searching to fix this problem for a while now. When I use the fancyhdr package to create headers and footers, and I run my code, as long as the header does not take up multiple lines, the footer spacing from the bottom of the page is consistent on every page. However, when I add a multiline header, the footer on the first page is always higher than the footer on the subsequent pages. I have not been able to find a solution to this in my online searches.

Example of code WITHOUT a multiline header and consistent footer spacing:

    \documentclass[10pt]{article}

    \usepackage{amsmath, amssymb, graphicx}
    \usepackage[height=9in,width=7in]{geometry}

    \usepackage{fancyhdr}
    \pagestyle{fancy}

    \lhead{ Left Header}  \rhead{Right Header}
    \rfoot{ \textit{Left footer}}
    \renewcommand{\headrulewidth}{0pt} 


    \begin{document}
    Testing
    \newpage
    Hi

    \end{document}

Example of code WITH a multiline header that is more than 2 lines and a footer higher on first page than on subsequent pages:

    \documentclass[10pt]{article}

    \usepackage{amsmath, amssymb, graphicx}
    \usepackage[height=9in,width=7in]{geometry}

    \usepackage{fancyhdr}
    \pagestyle{fancy}

    \lhead{ Left Header \\ 2nd left header \\ 3rd left header \\ 4th left header}  \rhead{Right Header \\ 2nd right header \\ \text{  } \\ Skipped 3rd}
    \rfoot{ \textit{Left footer}}
    \renewcommand{\headrulewidth}{0pt} 


    \begin{document}
    \text{  } \\ \\Testing
    \newpage
    \text{  } \\ \\ Hi

    \end{document}

Best Answer

With the second example, I get the warning

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

Solution:

\usepackage[height=9in,width=7in,headheight=48pt,heightrounded]{geometry}

(or 46.6pt, I prefer a multiple of \baselineskip. The addition of heightrounded is not related to the problem, but it's better to always use it.


When the document class is article (one sided), geometry applies some heuristics for setting the unspecified lengths. Since you just specify the text height to 9in, it will divide the space at the top and bottom according to a 2:3 ratio; thus the space above is 0.8in.

The default value of \headsep is 25pt; it's the distance from the baseline of the header to the top of the text block. With a \headheight of 48pt, it makes 73pt, which is slightly more than 1 inch, so part of the header will disappear above the top of the paper.

Since you need 48pt for the header, you have to adjust also other parameters. Let's say you want 18pt (1/4 in) above the header and also a reduced \headsep, say 12pt. Then adding

top=78in,

will do, because 18+48+12=78. Of course, the text block will be shifted down.

Complete example:

\documentclass{article}
\usepackage[
  height=9in,      % height of the text block
  width=7in,       % width of the text block
  top=78pt,        % distance of the text block from the top of the page
  headheight=48pt, % height for the header block
  headsep=12pt,    % distance from the header block to the text block
  heightrounded,   % ensure an integer number of lines
  showframe,       % show the main blocks
  verbose,         % show the values of the parameters in the log file
]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all fields

\fancyhead[L]{Left Header \\ 2nd left header \\ 3rd left header \\ 4th left header}
\fancyhead[R]{Right Header \\ 2nd right header \\ \  \\ Skipped 3rd}
\fancyfoot[C]{\thepage}
\fancyfoot[R]{\textit{Left footer}}
\renewcommand{\headrulewidth}{0pt}

\usepackage{lipsum}

\begin{document}
\lipsum[1-20]
\end{document}

With showframe some hairlines will outline the main blocks, with verbose the computations will be shown in the log file.

enter image description here

The documentation of the geometry package is very extensive. Here is a link, but you can surely find it on your system (texdoc geometry from a command line).