[Tex/LaTex] Automatically adjusting textheight dependant on header / footer size

fancyhdrheader-footer

I want to have a variety of page styles used throughout my document, with different header and footer formats / content.

I'm having trouble with getting the footer to not flow off the page.

The work-around I have used is to set the positioning absolutely, as per example below:

\documentclass[12pt, a4paper]{article}

\usepackage{blindtext}
\usepackage{fancyhdr}
\usepackage[margin=2cm, includefoot]{geometry}

\usepackage[utf8]{inputenc}
\usepackage{lastpage}
\usepackage{mdwlist}
\usepackage{titlesec}

\fancypagestyle{firstpagestyle}
{
  \fancyhf{} % clear all header and footer fields
  \chead{
      \LARGE \textbf{Common Header} \\
      \small diff header line 1 \\ 
      \small diff header line 2 \\
      }
  \cfoot{\thepage \ of \pageref{LastPage}}
  \renewcommand{\headrulewidth}{2pt}

  \setlength\headheight{60pt}
  \setlength{\footskip}{15pt}

}

\fancypagestyle{otherpagesstyle} {
  \fancyhf{} % clear all header and footer fields
  \chead{
      \LARGE \textbf{Common Header} \\ \small
      }
  \cfoot{\thepage \ of \pageref{LastPage}}
  \renewcommand{\headrulewidth}{2pt}

  \setlength{\headheight}{33pt}
  \setlength{\footskip}{42pt}
}



\pagestyle{otherpagesstyle}

%==============================================
\begin{document}

  \thispagestyle{firstpagestyle}
%==============================================

\subsection*{Section0}
\vspace{-0.4em}
  \blindtext

%==============================================

\subsection*{Section1}
\vspace{-0.4em}
\hrule
\vspace{1em}

  \blinditemize
  \blindtext
  \blinditemize
  \blindtext
%==============================================
\subsection*{Section2}
\vspace{-0.4em}
\hrule
\vspace{1em}

  \blindtext
  \blinditemize
  \blindtext
  \blinditemize
  \blindtext
  \blinditemize
  \blindtext
\end{document}

However I'm not satisfied with this solution as it doesn't seem like it will scale well for more complicated documents, not to mention coupling between the header / footer / text area, so that a change to one will possibly require a (manually calculated) change to all.

I have tried to alter the textheight attribute per pagestyle but 'firstpagestyle' doesn't seem to overload the default settings (eg \pagestyle{otherpagesstyle})….

Is there a neater way to do this?

Best Answer

Since your problem is only in the first page, just use the same head height throughout and do as if the first header block fits in it.

\documentclass[12pt, a4paper]{article}

\usepackage{blindtext}
\usepackage[margin=2cm,headheight=25pt,includefoot]{geometry}
% headheight=25pt has been suggested by fancyhdr
\usepackage{fancyhdr}

\usepackage{lastpage}

\fancypagestyle{firstpagestyle}{
  \fancyhf{} % clear all header and footer fields
  \fancyhead[C]{\LARGE
    \parbox[t][\ht\strutbox]{\textwidth}{
      \centering
      \LARGE \textbf{Common Header} \\
      \small diff header line 1 \\ 
      \small diff header line 2\endgraf % not \par because \fancypagestyle is not \long
      \kern4pt
      \hrule height2pt
    }%
  }
  \fancyfoot[C]{\thepage \ of \pageref{LastPage}}
  \renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{otherpagesstyle}{
  \fancyhf{} % clear all header and footer fields
  \fancyhead[C]{\LARGE \textbf{Common Header}}
  \fancyfoot[C]{\thepage \ of \pageref{LastPage}}
  \renewcommand{\headrulewidth}{2pt}
}

\pagestyle{otherpagesstyle}

%==============================================
\begin{document}
\thispagestyle{firstpagestyle}
\vspace*{1sp} % adjust here in case you need more room
%==============================================

\section{Section0}
\blindtext

%==============================================

\section{Section1}

\blinditemize
\blindtext
\blinditemize
\blindtext

%==============================================
\section{Section2}

\blindtext
\blinditemize
\blindtext
\blinditemize
\blindtext
\blinditemize
\blindtext

\end{document}