1

I am trying to define a base class for some reports to share the same page layout. I use the fancyhdr package for creating the header and footer but I am experiencing some vertical alignment problems. I spent hours trying to figure out how to do what seems so simple in HTML. In the following images, I used the package showframe to help identifying the page layout.

Here is an image of what I would like to achieve:

![report base image]1

Here is an image of what I have right now:

![report base result]2

Apart from the general page layout, I cannot vertically align the report name and date to the top with the image:

![report header alignment]3

The page body overlaps the header:

![report body overlaps]4

The page footer overlaps the body:

![report footer overlaps]5

Finally, here is the Latex code used for the class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{basereport}

% base class
\LoadClass[letterpaper, 12pt]{article}

% packages
\usepackage[utf8]{inputenc} % The UTF-8 encoding is specified.
\usepackage{color}
\usepackage{lipsum}
\usepackage{helvet}
\usepackage{graphicx}
\usepackage{fancyhdr} % for header and footer
\usepackage{lastpage}
\usepackage[yyyymmdd,hhmmss]{datetime}

% debug frames
\usepackage{showframe}

% margins
% http://ctan.mirror.rafal.ca/macros/latex/contrib/geometry/geometry.pdf
\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm
]{geometry}

% macro for images
\DeclareGraphicsExtensions{.pdf,.png,.jpg}

% page style
% http://ctan.bppro.ca/macros/latex/contrib/fancyhdr/fancyhdr.pdf
\pagestyle{fancy} % for page style

% page header
\fancyhf{} % reset header and footer
\renewcommand{\headrulewidth}{0pt} % remove header rule
\renewcommand{\footrulewidth}{0pt} % remove footer rule

\fancyhead[L]{
    \includegraphics[width=4cm, height=2cm, keepaspectratio=true]{universe.jpg}
}
\fancyhead[C]{}
\fancyhead[R]{
    \large\textbf{
        \MakeUppercase{Report name}
        \linebreak
        2015-11-10
    }
}

% page footer
\fancyfoot[L]{
    \small\emph{Printed on \today\ at \currenttime}
}
\fancyfoot[C]{}
\fancyfoot[R]{
    \small\emph{Page \thepage\ of \pageref{LastPage}}
}

% commands
\renewcommand{\familydefault}{\sfdefault}

and the document:

\documentclass{basereport}
\begin{document}
\textcolor[rgb]{0,0,1}{
    \lipsum[1]
}
\end{document}

I tried to play with the length of those properties:

\hoffset
\voffset
\headheight
\textheight
\textwidth
\footskip
\headsep

EDIT: Here is what I got for using some of those properties:

\setlength{\hoffset}{0cm}
\setlength{\voffset}{1cm}

\setlength{\headheight}{2cm}
\setlength{\headsep}{0.5cm}
\setlength{\footskip}{0.5cm}

report base result with properties

It seems like the geometry package is being overridden by fancyheader? I am not sure to understand the conflicts. If I ask for 1cm margins, why do I have the set voffset? Also, is there any way of specifying the header and footer heights, and let the body use the remainder of the document without having to set textheight? How to vertical align the text in the header to the top?

EDIT 2:

I set the properties in the geometry package declaration instead of manually as suggested and it helped a lot. I am still usure why the top value must include the headheight and headsep plus the desired margin value.

report base result with geometry

Two questions remains:

1) how to vertically align my header so the title and date are at the top?

2) how to remove the line under the footer which appears even if footrulewidth is set to 0.0pt?

The updated code of the class for reference:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{basereport}

% base class
\LoadClass[letterpaper, 12pt]{article}

% packages
\RequirePackage[utf8]{inputenc} % The UTF-8 encoding is specified.
\RequirePackage{color}
\RequirePackage{lipsum}
\RequirePackage{helvet}
\RequirePackage{graphicx}
\RequirePackage{fancyhdr} % for header and footer
\RequirePackage{lastpage}
\RequirePackage[yyyymmdd,hhmmss]{datetime}

% debug frames
\RequirePackage{showframe}

% margins
% http://ctan.mirror.rafal.ca/macros/latex/contrib/geometry/geometry.pdf
\RequirePackage[
    headheight=30mm,
    headsep=5mm,
    footskip=10mm,
    top=45mm, 
    bottom=20mm, 
    left=10mm, 
    right=10mm
]{geometry}

% macro for images
\DeclareGraphicsExtensions{.pdf,.png,.jpg}

% page style
% http://ctan.bppro.ca/macros/latex/contrib/fancyhdr/fancyhdr.pdf
\pagestyle{fancy} % for page style

% page header
\fancyhf{} % reset header and footer
\renewcommand{\headrulewidth}{0pt} % remove header rule
\renewcommand{\footrulewidth}{0pt} % remove footer rule

\fancyhead[L]{
    \includegraphics[keepaspectratio=true]{universe.jpg}
}
\fancyhead[C]{}
\fancyhead[R]{
    \large\textbf{
        \MakeUppercase{Report name}
        \linebreak
        2015-11-10
    }
}

% page footer
\fancyfoot[L]{
    \small\emph{Printed on \today\ at \currenttime}
}
\fancyfoot[C]{}
\fancyfoot[R]{
    \small\emph{Page \thepage\ of \pageref{LastPage}}
}

% commands
\renewcommand{\familydefault}{\sfdefault}
  • 1
    Welcome to TeX.SX. Your headheight and topmargin are too small, most likely – user31729 Dec 14 '15 at 22:28
  • 1
    Don't you get a warning from fancyhdr that headheight is incorrect? – David Carlisle Dec 14 '15 at 22:36
  • 1
    \RequirePackage instead of \usepackage would be better – user31729 Dec 14 '15 at 22:37
  • 1
    It's normal to use \RequirePackage in class files and \usepackage in document preamble, but their definitions are (completely) identical so don't worry too much aboout that. – David Carlisle Dec 14 '15 at 22:43
  • 1
    The warning also tells you what the value needs to be as a minimum. Use that to set headheight. Only don't do page dimensions manually. Use \usepackage[headheight=<whatever>, headsep=5mm, ...]{geometry} and then don't specify dimensions manually. The package will then calculate everything correctly. – cfr Dec 14 '15 at 23:09
1

The basic error is that the \headheight length is too small, this leads to an overlapping at the top of the page header and the regular text.

fancyhdr will issue a warning and some recommendation about correct values on the console and in the log file.

Use \setlength{\headheight}{2cm} (or whatever appropiate value is reported by fancyhdr) or use \usepackage[headheight=2cm,...]{geometry}.

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.