2

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. May 23, 2021 at 9:29

1 Answer 1

3

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

6
  • This answer solved my problem - I guess you know what you're talking about :D It should be documented so we don't need to rely on StackExchange. Is it possible to have a single, consistent width of the footer/header on every page, regardless of the geometry? (I'm not changing my paper size of A3 landscape). Because I'm writing a graphical school application and I'm changing the margins, textwidth etc on different pages with package geometry. However, I would like a consistent footer that fills the whole page width (excluding margins) on any page for meta information like application number.
    – telephone
    Mar 11 at 10:20
  • 1
    Yes, I have already updated the documentation for a future release. Mar 11 at 11:22
  • Then it would be nice to have a new command like \fancynewgeometry instead of setting \pdfpagewidth/height because that's seems strange, and I did not change the paper dimension, only options like margins and textwidth. Or better do it automatically in \pagestyle/\thispagestyleso you can call this whenever you change the geometry. Is it currently possible to have a consistent footer/header width, so it does not change when the geometry changes? Right now it depends on the page width and margins.
    – telephone
    Mar 11 at 15:38
  • This was almost 3 years ago, so I will have to dive into this again. But \pagestyle and \thispagestyle are standard LaTeX commands, so I doubt if I can do much about this. Mar 11 at 16:08
  • @telephone The changes in \pdfpagewidth/height are in the original problem description, they are not my suggestion. However, to have these affect the page layout, something like \newgeometry is necessary. For your case, please open a new question and give a complete, but minimal document (MWE) showing the problem. Mar 11 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .