I was wondering if there's a package that would help me set a custom page header as the one in the picture.
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
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
makes no sense. You have specified the left and right headers within the specification for the centre header.
The
\shortstack
is centring it. I'd just use atabular
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.)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 at
geometry. To create a table across the width, we can usetabularx
. To prevent bad boxes, we can lie about its width.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.
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.
Eliminating the box helps a bit. I'd also eliminate the
minipage
and simplify the table specification. You have entirely pointless\multicolumn
s 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.