[Tex/LaTex] Custom header boxes

header-footer

I was wondering if there's a package that would help me set a custom page header as the one in the picture.

enter image description here

I'm very new to Latex, I sort of made it work with fancyhdr but I'm having trouble setting margins and aligning text. I'm looking for a simpler and easier way to achieve the same result.

I'm working with Pandoc (using markdown + latex) because I need a pdf copy and a .docx copy of the same file, so bonus point if the solution works with MS Word page headers as well.

Edited to add the code:

\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper]{geometry}

%% load packages
\usepackage{multirow}
\usepackage{array}
\usepackage{lastpage}
\usepackage{fancyhdr}\pagestyle{fancy}
\usepackage[owncaptions,tablegrid]{vhistory}
\usepackage{lipsum}

\newsavebox{\myTitleBlock}
\begin{lrbox}{\myTitleBlock}
\begin{minipage}{\textwidth}
\renewcommand{\arraystretch}{3}
\begin{tabular}{|p{5cm}|p{2,5cm}|p{2,5cm}|p{2,5cm}|p{2,5cm}|}
\hline
\multirow{2}{*}{\raggedleft{\shortstack{\textbf{Company}\\Address\\0001 New York NY}}} & \multicolumn{4}{l|}{Some text here} \\
\cline{2-5} & \multicolumn{1}{l|}{Form xx} & Rev. \vhCurrentVersion & dd/mm/yyyy & Page \thepage{} of \pageref{LastPage} \\
\hline
\end{tabular}
\end{minipage}
\end{lrbox}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\chead{\usebox{\myTitleBlock}
\lhead{}
\rhead{}}

\begin{document}

\begin{versionhistory}
%<Version> <Date> <Authors> <Changes>}
\vhEntry{1.1}{13.05.04}{JW|AK|KL}{Typos corrected.}
\vhEntry{1.0}{22.01.04}{JPW|KW}{created}
\vhEntry{1.1}{23.01.04}{DP|JPW}{correction}
\vhEntry{1.2}{03.02.04}{DP|JPW}{revised after review}
\end{versionhistory}

%the version number of the last entry
Last revision: \vhCurrentVersion

%Include a table of contents
\setcounter{tocdepth}{4}
\tableofcontents

\lipsum{2-5}

\end{document}

On overleaf: https://www.overleaf.com/6834124pxvthzbvjmvt#/23296145/

As you can see the issues are:

  • Page x of ??. It works inside the document but in the header is ??.
  • I don't know how to align the address to the left
  • I want to expand the header to fill the width of the a4 paper but keep the actual margins for the main text. Also reduce a bit of the top margin. More generally I want to specify different margins for the header and the text.
  • The header and the content overlap on the first page
  • Last but not the least, complicated code. Looking for something more intuitive.

Best Answer

  • Page x of ??. It works inside the document but in the header is ??.

Michael Palmer already partly explained the first issue: saving the header to a box 'freezes' its content, which is not what you want for page numbers. This is why it is always page 1 and never page 2. Because the reference for the last page is not known in the preamble - because the .aux file from the previous run (if any) has not yet been read - the last page is always an unresolved cross-reference at this point. If you specified the header after \begin{document}, this should work. But you'd still be stuck forever on page 1.

Note that this code

\chead{\usebox{\myTitleBlock}
  \lhead{}
  \rhead{}}

makes no sense. You have specified the left and right headers within the specification for the centre header.

  • I don't know how to align the address to the left

The \shortstack is centring it. I'd just use a tabular here, I think. (Well, I probably wouldn't as I wouldn't do it, but if I was going to do it, I probably would.)

  • I want to expand the header to fill the width of the a4 paper but keep the actual margins for the main text. Also reduce a bit of the top margin. More generally I want to specify different margins for the header and the text.

This comes in several parts. To change the header widths, we need to look at fancyhdr. To change the page dimensions, including the height of the header, we need to look atgeometry. To create a table across the width, we can use tabularx. To prevent bad boxes, we can lie about its width.

  • The header and the content overlap on the first page

You need to read the console output when compiling or consult the log afterwards. Occasionally ... just occasionally, all that information TeX writes tells you stuff you need to know. When things don't work as you expect, this should be the first thing to look at.

Here's what the console tells us.

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

It then tells you that the package is changing the dimension, but unfortunately it is too late for page 1. So this is one aspect of the layout we need to change.

  • Last but not the least, complicated code. Looking for something more intuitive.

Eliminating the box helps a bit. I'd also eliminate the minipage and simplify the table specification. You have entirely pointless \multicolumns so they can go.

Here's an example. This still generates a bad box, but that is caused by the version history environment and not part of this question.

\documentclass[a4paper]{article}
\usepackage[british]{babel}% or american or ...
\usepackage[utf8]{inputenc}% don't use utf8x
\usepackage[T1]{fontenc}
\usepackage{geometry}% no need to repeat papersize here
\usepackage{multirow}
\usepackage{array,calc,tabularx}
\usepackage{lastpage}
\usepackage{fancyhdr}
\usepackage[owncaptions,tablegrid]{vhistory}
\usepackage{lipsum}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyheadoffset{25.4mm+\leftmargin}
\chead{%
  \centering
  \renewcommand{\arraystretch}{3}%
  \begin{tabularx}{\paperwidth}{|p{5cm}|*{4}{X|}}% use something other than \paperwidth if you didn't really mean this - I've implemented what you asked for, even though I don't see why you want vertical rules at the ends in this case
    \hline
    \multirow{2}{*}{\renewcommand{\arraystretch}{1}\bfseries\begin{tabular}{@{}l@{}}Company \\ Address \\ 0001 New York NY\end{tabular}} & \multicolumn{4}{c|}{Some text here} \\ % tabular is simpler and deals automatically with the alignment @{} avoid additional horizontal space
    \cline{2-5} & Form xx & Rev.\ \vhCurrentVersion & dd/mm/yyyy & Page \thepage{} of \pageref{LastPage} \\ % no need for a multicolumn here 
    \hline
  \end{tabularx}%
}
\lhead{}% not part of the central header spec!
\rhead{}

\geometry{headheight=86pt}% change margins etc. here e.g. top=10mm for a 10mm top margin etc. - but I don't think you should make the top margin *less*!

\begin{document}

\begin{versionhistory}% this causes an underfull box with infinite badness as far as I can tell.
  \vhEntry{1.1}{13.05.04}{JW|AK|KL}{Typos corrected.}
  \vhEntry{1.0}{22.01.04}{JPW|KW}{created}
  \vhEntry{1.1}{23.01.04}{DP|JPW}{correction}
  \vhEntry{1.2}{03.02.04}{DP|JPW}{revised after review}
\end{versionhistory}

Last revision: \vhCurrentVersion

\setcounter{tocdepth}{4}
% nothing to see here - why is this in the minimal example?
\tableofcontents

\lipsum{2-5}


\end{document}

full-width header

Related Question