1

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.
  • Please provide the code you have which 'sort of' works. It doesn't make any sense for us to try to copy stuff from your image or to guess what your class etc. is. It will be much easier to help if you give us a minimal but complete document showing what you've got so far. To format the code you paste into your question, highlight it and use the {} button. – cfr Nov 1 '16 at 2:40
  • 2
    Note that it really doesn't make sense to ask us for TeX code which will work in MS Word. At least, I don't think it does, though I should say that I know almost nothing about the current incarnation of the word processor from hell ;). (But it is Halloween, after all.) – cfr Nov 1 '16 at 2:42
  • @cfr Added some code and a live example. – Barbara Nov 1 '16 at 12:38
  • @cfr it's a layout for standard operating procedures and work instructions. These documents are sent to the clients but none of them knows Latex. I think in the long run Latex will the best choice for me (for updating, tracking changes, diff, etc) but at the same time I need to send something they are able to edit by themselves. This is why I'm stuck with .docx and also why I'm using Pandoc. But while it's super easy to set a template for the content I'm still trying to understand if it's possible to compile the contents of the header as well. – Barbara Nov 1 '16 at 12:52
1
  • 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

  • I thought the OP wanted to align the header box with the body text, rather than spreading it out over the entire paper width. Either way, your explanation is very detailed and educational. – Michael Palmer Nov 2 '16 at 12:44
  • @cfr Thanks for the explanation. I'm using your code to learn and have a better understanding of Latex. What do you mean with "underfull box with infinite badness"? I see no warning in the console, how would I know about it? – Barbara Nov 2 '16 at 20:38
  • @Barbara I see it on the console. It will also be in the log file. Some editors will also parse the log and pick this kind of information out for you. Note that it may be that in your real document, you don't have a bad box there. I don't know anything about the version history package you're using, so no idea if this is likely or not. – cfr Nov 2 '16 at 23:22
  • 1
    @MichaelPalmer I went by the second bullet point: I want to expand the header to fill the width of the a4 paper but keep the actual margins for the main text. Maybe the question was edited after you answered? – cfr Nov 2 '16 at 23:23
  • @cfr - you are right. I probably just read hastily. – Michael Palmer Nov 3 '16 at 1:17
3

enter image description hereThe content of your header savebox gets frozen before the document starts, and then just pasted unchanged every time. If you do away with the savebox:

% changes affecting table layout
\usepackage{calc}  % for setting table dimensions relative to \textwidth
% change parameters for package geometry to make space for big header 
\usepackage[a4paper,margin=1in,tmargin=2in,headheight=2in]{geometry}

% change code for header generation
\newcommand{\makeheader}{
\begin{minipage}{\textwidth}
\renewcommand{\arraystretch}{3}
\begin{tabular*}{\textwidth}[t]{@{\extracolsep{\fill}}|p{0.3\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|}
\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}}

\chead{\makeheader}

the page counter works.

BTW are you sure about those commas in the column width specifications? One would normally use decimal points, but maybe you loaded some package that makes commas work also (? although I'm not aware of any such package).

Edit: I also changed the table and page dimensions a bit.

  • Thanks for the explanation. I don't know much about saveboxes does, I've just edited an example I found online and it worked...somehow. Thanks for the warning about the commas as well. – Barbara Nov 2 '16 at 20:42

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.