2

I wish to add header/footer to a document in landscape format. The document is basically a long table with few notes. Consider the following example:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{multirow}
\usepackage{longtable}
\setlength{\textwidth}{16cm}
\setlength{\textheight}{23cm}
\setlength{\oddsidemargin}{0cm}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{My name}
\lhead{document title}
\cfoot{Page \thepage}

\usepackage{geometry}
\geometry{letterpaper, 
landscape,
top=2cm,
bottom=2cm,
left=3cm,
right=2cm,
includehead,includefoot,
heightrounded}

\setlength\parindent{0pt}

\begin{document}

\begin{center}
\begin{longtable}{p{2cm}p{4cm}p{1cm}p{2cm}p{4cm}}
\multicolumn{5}{c}{\Large\textbf{Name:} My Name} \\
College:& My college & & University: & My university
\end{longtable}
\vspace{3em}

\begin{large}
    Title of document
\end{large}
\end{center}

\begin{footnotesize}
some notes
\end{footnotesize} 
\end{document}

The output is all messed up: mess

However, if I just remove the fancyhdr part from the code, there is no problem in the alignment of footer:

what we want

How can I align the fancyhdr with the document?

  • 1
    Load fancyhdr after you load geometry. – Johannes_B Nov 15 '18 at 6:25
  • 1
    also don't set \textwidth "by hand" if you are using geometry which will change it again later – David Carlisle Nov 15 '18 at 10:44
  • @DavidCarlisle Thanks for the tip! Where can I read more about these dependencies? Like which package should be loaded before others. – rationalbeing Nov 15 '18 at 13:19
  • 1
    there are no general rules, but in this case it just follows some kind of natural logic that fancyhdr needs to know what page size it is making a header for, and geometry sets the page size.... – David Carlisle Nov 15 '18 at 13:34
  • OTOH, fancyhdr has a nasty habit of changing the geometry after the first page. – John Kormylo Nov 15 '18 at 16:39
2

Well, in your code you have some issues:

  1. You are using definitions you do not need if you use package geometry:

    %\setlength{\textwidth}{16cm} % <=======================================
    %\setlength{\textheight}{23cm}
    %\setlength{\oddsidemargin}{0cm}
    
  2. The order of calling packages geometry and fancyhdr is important! You need to call geometry first to define all margins. This correct defined margins can then be used by fancyhdr to insert the needed header and footers ...

    \usepackage{geometry} % <===============================================
    \geometry{%
      letterpaper, 
      landscape,
      top=2cm,
      bottom=2cm,
      left=3cm,
      right=2cm,
      includehead,includefoot,
      heightrounded
    }
    
    \usepackage{fancyhdr} % <===============================================
    \pagestyle{fancy}
    \fancyhf{}
    \rhead{My name}
    \lhead{document title}
    \cfoot{Page \thepage}
    
  3. fancyhdr creates on the first page the header and footer and test if the available space for header and footer is enough or not. In your case you get the warning (see your log file):

    Package Fancyhdr Warning: \headheight is too small (12.0pt): 
    Make it at least 13.59999pt. 
    

    So you need to add the line

    \setlength{\headheight}{14pt} % <=======================================
    

    to your preamble ...

Please see the following complete and compilable mwe

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{multirow}
\usepackage{longtable}
%\setlength{\textwidth}{16cm} % <=======================================
%\setlength{\textheight}{23cm}
%\setlength{\oddsidemargin}{0cm}

\usepackage{geometry} % <===============================================
\geometry{%
  letterpaper, 
  landscape,
  top=2cm,
  bottom=2cm,
  left=3cm,
  right=2cm,
  includehead,includefoot,
  heightrounded
}

\usepackage{fancyhdr} % <===============================================
\pagestyle{fancy}
\fancyhf{}
\rhead{My name}
\lhead{document title}
\cfoot{Page \thepage}
\setlength{\headheight}{14pt} % <=======================================

\setlength\parindent{0pt}


\begin{document}

\begin{center}
\begin{longtable}{p{2cm}p{4cm}p{1cm}p{2cm}p{4cm}}
\multicolumn{5}{c}{\Large\textbf{Name:} My Name} \\
College:& My college & & University: & My university
\end{longtable}
\vspace{3em}

\begin{large}
    Title of document
\end{large}
\end{center}

\begin{footnotesize}
some notes
\end{footnotesize} 

\clearpage
\begin{footnotesize}
some notes
\end{footnotesize} 
\end{document}

and its result:

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.