0

I am writing a report using fancyhdr for headers and footers. My report is mostly in A4 page, but there are some pages where I may need to fit bigger tables and images, so I manually change the pdf page dimensions midway through the document. However, every time I change the page dimensions, fancyhdr applies the header line as the same length as the A4 page, and the footer line which has the page number completely disappear.

This is just a part of the code copied from my main report, with filler text, but it should demonstrate the problem I'm having:

\documentclass{article}
\usepackage[a4paper,width=160mm,top=25mm,bottom=25mm]{geometry}
\usepackage{fancyhdr}

\pagestyle{fancy}

\begin{document}

\fancyhf{}
\renewcommand{\sectionmark}[1]{\markright{#1}} % 
\renewcommand{\headrulewidth}{0.5pt} % Thickness
\renewcommand{\subsectionmark}[1]{}
\renewcommand{\footrulewidth}{0.5pt} % Thickness
\fancyhead[L]{test 1}
\fancyhead[R]{\rightmark}
\fancyfoot[L]{test 3}
\fancyfoot[C]{}
\fancyfoot[R]{\thepage}

\pdfpagewidth=8.25in \pdfpageheight=11.75in
\section{test 1}
Page 1

\newpage
\pdfpagewidth=15in \pdfpageheight=10in
\section{Test 2}
Page 2

\newpage
\pdfpagewidth=8.25in \pdfpageheight=11.75in
\section{Test 3}
Page 3

\end{document}

So from the code above, page 1 and page 3 are fine with the header and footer working as intended, but page 2 is a bigger page with a different dimension. How do I make it so that the the fancyhdr header and footer adjust automatically to the page dimensions? I want the width to adjust automatically, but the height of the header and footer from the margin to remain the same for all pages.

Should I change the paper dimensions differently, rather than using \pdfpaperwidth or \pdfpaperheight, or maybe use a different package? I would ideally like to stick with fancyhdr but any help is appreciated thanks.

1
  • 1
    with \pdfpagewidth you are only changing the mediabox, the "background" size. You would need to change the whole page layout and that is not easy as they are lots of lengths involved. The KOMA classes have code for this, with geometry it works only with tricks tex.stackexchange.com/a/527031/2388. Personally I wouldn't try to change the header and footer, but set the pagestyle to empty for such pages. – Ulrike Fischer May 23 at 9:29
0

In fact if you set \pdfpagewidth and \pdfpageheight, and then you call \newgeometry with the proper parameters, it will pick up the new page size. This is not documented, so it may be considered tricky.

And to have fancyhdr use the new \textwidth, just use \fancyhfoffset{0pt}, which makes fancyhdr recalculate \headwidth for each page, based on the then current \textwidth.

\documentclass{article}
\usepackage[a4paper,width=160mm,top=25mm,bottom=25mm]{geometry}
\usepackage{fancyhdr}

\pagestyle{fancy}
\usepackage{lipsum}

\begin{document}

\fancyhf{}
\renewcommand{\sectionmark}[1]{\markright{#1}} % 
\renewcommand{\headrulewidth}{0.5pt} % Thickness
\renewcommand{\subsectionmark}[1]{}
\renewcommand{\footrulewidth}{0.5pt} % Thickness
\fancyhead[L]{test 1}
\fancyhead[R]{\rightmark}
\fancyfoot[L]{test 3}
\fancyfoot[C]{}
\fancyfoot[R]{\thepage}
\fancyhfoffset{0pt}

\section{test 1}
Page 1

\lipsum[1]
\newpage

\pdfpagewidth=15in \pdfpageheight=10in
\newgeometry{textwidth=13in, textheight=8in,top=25mm,left=25mm}
\section{Test 2}
Page 2

\lipsum[2]
\newpage
\pdfpagewidth=8.25in \pdfpageheight=11.75in
\newgeometry{margin=25mm}
\section{Test 3}
Page 3

\lipsum[3]
\end{document}

enter image description here

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.