# Prevent overhanging of header on included PDF

I have a headrule which overhangs the text, whereas I would like it to be the same length as the text and aligned with the text. The misalignment seems to come when including a PDF rather than having it as part of the same document. Below are left and right page examples.

The misalignment seems to occur regardless of what margins/paging I set in the included document. I have been looking at this as a fancyhdr problem, and tried to adjust the header with a combination of setting a shorter \headwidth and adding in \fancyheadoffset to shift it all to the right/left, but I cannot figure out the syntax for achieving this. If I use just \headwidth, the rule is shorter but still misaligned. If I add \fancyheadoffset after it, \headwidth is ignored. If I put \headwidth after \fancyheadoffset, it gives a compilation error. My attempts are included with comments in the example.

However, it is also possible that this could be considered a pdfpages problem, and I need to include the PDF differently so that content is not shifted. (NB: In my actual document, the outer file also has content, so margins do need to be set in it.)

This is the minimal example producing the included content (save output as Include.pdf) :

\documentclass[twoside,12pt,openany]{article}
\usepackage{setspace}
\usepackage{lipsum}

\setlength{\oddsidemargin}{1.5cm}
\setlength{\evensidemargin}{0 cm}
\setlength{\topmargin}{1mm}
\setlength{\textheight}{20.84cm}
\setlength{\textwidth}{14.5cm}
\setlength{\marginparsep}{1mm}
\setlength{\marginparwidth}{3cm}
\setlength{\footskip}{2.36cm}

\begin{document}
\doublespacing
\lipsum

\end{document}


And here is the code that uses it to generate the example images above:

\documentclass[twoside,12pt,openany]{book}
\usepackage{pdfpages}
\usepackage{fancyhdr}

\setlength{\oddsidemargin}{1.5cm}
\setlength{\evensidemargin}{0 cm}
\setlength{\topmargin}{1mm}
\setlength{\textheight}{20.84cm}
\setlength{\textwidth}{14.5cm}
\setlength{\marginparsep}{1mm}
\setlength{\marginparwidth}{3cm}
\setlength{\footskip}{2.36cm}

\fancypagestyle{papertwostyle}
{
\fancyhf{}
\fancyfoot[C]{\thepage}
}

\begin{document}

\pagestyle{papertwostyle}
\includepdf[pages={1-3}, pagecommand={}]{Include}

\end{document}


Your two PDF files don't have the same paper size, fix this and the problem should disappear.

By passing the draft option to the pdfpages package, you can see a frame corresponding to the included PDF file, and from there guess that the problem is a mismatching paper size. If I run pdfinfo Include.pdf (on Linux), it tells me that Include.pdf is in A4 format:

\$ pdfinfo Include.pdf
Creator:        TeX
Producer:       pdfTeX-1.40.19
CreationDate:   Sat Apr  6 14:54:05 2019 CEST
ModDate:        Sat Apr  6 14:54:05 2019 CEST
Tagged:         no
UserProperties: no
Suspects:       no
Form:           none
JavaScript:     no
Pages:          3
Encrypted:      no
Page size:      595.276 x 841.89 pts (A4)

(...)


However, as surprising as it can seem, your main document (the one that includes Include.pdf) is in letter format. This seems to be triggered by the \usepackage{pdfpages} line. Don't ask me why, but if you comment out that line as well as the \includepdf call, add a \null box after \begin{document} to make sure there is at least one page of output, then your main document will be in A4 format (at least, this is the case here with TeX Live 2018).

In order to solve your problem, you have to make sure both PDF files have the same paper size. I suggest using the geometry package for that. You can insert \usepackage{geometry} after the \documentclass line and, for your main document, before the \usepackage{fancyhdr} line. Make sure to load geometry after any font change (especially if you use its lines option) and before fancyhdr (see here for advice on package loading order).

For example, if you want A4 documents, you can start Include.tex like this:

\documentclass[twoside,12pt,openany]{article}
\usepackage[paper=a4paper]{geometry}
\usepackage{setspace}
\usepackage{lipsum}


and your main document this way:

\documentclass[twoside,12pt,openany]{book}
\usepackage[paper=a4paper]{geometry}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\usepackage{lipsum}


Just replace a4paper with letterpaper if you want letter paper size. Once you have done so, you should get the expected result, but I'd suggest doing all your page geometry settings with the options offered by the geometry package (I fear that setting the parameters manually as you did isn't really supported with geometry) . You can find its manual here. Look for options like inner, left or lmargin to replace your \evensidemargin, etc.

• That's a very undesired side-effect from pdfpages. Thank you for identifying the issue, and providing a work-around. I still need to try to fix the margins so that the header isn't overwriting the text, but at least now it is correctly aligned. – Ann Apr 6 at 15:45
• @Ann Adjusting the margins should be easy with the geometry package; you've never used it before? – frougon Apr 6 at 16:10
• I'm reasonably confident at figuring it out, given a bit of time. It's the usual case of using another person's complex template/class and not knowing how everything works inside. – Ann Apr 6 at 18:39