1

I want the page height to be adjusted by the contents. I find this answer works. However, I also want to use fancyhdr to modify the header.

Here is the MWE:

\documentclass{article}
\usepackage{everyshi,lipsum}
\textheight500cm
\EveryShipout{%
  \pdfpageheight=\pagetotal
  \advance\pdfpageheight by 2in
  \advance\pdfpageheight by 2\topmargin
  \advance\pdfpageheight by \textheight
  \advance\pdfpageheight by -\pagegoal}

\usepackage{fancyhdr}

\makeatletter
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{}
\fancyfoot[L]{\ttfamily\@title}
\fancyfoot[R]{\ttfamily\@author}
\makeatother

\title{This is Title}
\author{Name}

\begin{document}
\lipsum

\end{document}

As you can see, the header didn't show up, only a header rule is there.

Question: how to make fancyhdr work with variable page height?

1

1 Answer 1

1

The page dimensions are changed at shipout, so fancyhdr places them off the visual page (500cm or 50m into nothingness). You can raise them into position though:

enter image description here

\documentclass{article}
\usepackage{everyshi,lipsum}

\textheight500cm
\EveryShipout{%
  \pdfpageheight=\pagetotal
  \advance\pdfpageheight by 2in
  \advance\pdfpageheight by 2\topmargin
  \advance\pdfpageheight by \textheight
  \advance\pdfpageheight by -\pagegoal}

\usepackage{fancyhdr}

\makeatletter
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{}
\fancyfoot[L]{\raisebox{\dimexpr\textheight-\pagetotal}[0pt][0pt]{\ttfamily\@title}}
\fancyfoot[R]{\raisebox{\dimexpr\textheight-\pagetotal}[0pt][0pt]{\ttfamily\@author}}
\makeatother

\title{This is Title}
\author{Name}

\begin{document}

\lipsum

\end{document}

You may have to play around with the value it's raised by to get your position correct. An alternative is to place the content without fancyhdr, either as part of the text, or as part of the shipout routine.

2
  • May I ask how to place the header as part of the shipout ourtine?
    – Gau-Syu
    Nov 29, 2022 at 6:12
  • 1
    @Gau-Syu: For a basic header, you can just add it to \fancyhead (as in, \fancyhead[C]{This is a header}. This works since you're technically only changing the bottom of the page (the page height/length), not the top, so headers would work as expected. Does this work for you?
    – Werner
    Nov 29, 2022 at 21:31

You must log in to answer this question.

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