13

I am trying to automate the solution proposed by egreg to How define a fixed width page, but length > some minimum length, but only as long as needed by hooking into \begin{document} and \end{document}.

So, egreg's solution works fine:

\documentclass{article}
\def\MinimumPaperHeight{12cm}
\usepackage{geometry}
\geometry{paperwidth=\MinimumPaperHeight,paperheight=\maxdimen,margin=1cm}
\usepackage{lipsum}

\begin{document}
\setbox0=\vbox{
\lipsum[1-6]
}
\dimen0=\dp0
\pdfpageheight=\dimexpr\ht0+2cm\relax
\ifdim\pdfpageheight<\MinimumPaperHeight \pdfpageheight=\MinimumPaperHeight \fi
\unvbox0\kern-\dimen0
\end{document}

But can't seem to get this to work:

\documentclass{article}
\def\MinimumPaperHeight{12cm}
\usepackage{geometry}
\geometry{paperwidth=\MinimumPaperHeight,paperheight=\maxdimen,margin=1cm}
\usepackage{lipsum}
\usepackage{environ}

\NewEnviron{MyBox}{
    \setbox0=\vbox{\BODY}%
}{%
    \dimen0=\dp0%
    \pdfpageheight=\dimexpr\ht0+2cm\relax%
    \ifdim\pdfpageheight<\MinimumPaperHeight \pdfpageheight=\MinimumPaperHeight \fi%
    \unvbox0\kern-\dimen0%
}

\AtBeginDocument{\begin{MyBox}}
\AtEndDocument{\end{MyBox}}

\begin{document}
\lipsum[1-6]
\end{document}

Hooking into \AtBeginDocument is per this solution on How to automatically add text immediately after \begin{document}.

Peter Grill
  • 223,288

1 Answers1

11

This can't work, because \begin{MyBox} needs to see \end{MyBox}, but it doesn't because the document ends before the tokens are actually present: the contents of an environment defined with \NewEnviron is not expanded until it's processed with the expansion of \BODY.

One solution is to hook directly into \document and \enddocument:

\usepackage{etoolbox}

\AtBeginDocument{
  \setbox0=\vbox\bgroup
  \preto\enddocument{\egroup
    \dimen0=\dp0
    \pdfpageheight=\dimexpr\ht0+2cm\relax
    \ifdim\pdfpageheight<\MinimumPaperHeight
      \pdfpageheight=\MinimumPaperHeight
    \fi
    \unvbox0\kern-\dimen0 }
}

(Not heavily tested, sorry.)

egreg
  • 1,121,712
  • This works great. But, if using the standalone package, need to use [preview=false] option. – Peter Grill Sep 04 '11 at 19:38
  • This works great, except I can't figure out how to get a footer back in the pdf (using KOMA classes). It seems the footer is arranged based on the original page height (\maxdimen) then cropped off when the document ends. Should I ask another question about this or can you think of an easy way to integrate it as a fix in this solution? – Caleb Apr 14 '14 at 09:06
  • @Caleb My first attempt would be to add the footer just before \egroup (maybe as a prebuilt box). If you don't succeed, please ask a new question. – egreg Apr 14 '14 at 09:09
  • This doesnt seem to work for me in overleaf. It makes each page giant, but the contents of each page appear to be unchanged. So I have the same pages as before, only each page is giant.. – Hugh Perkins Jul 09 '23 at 16:36
  • @HughPerkins sorry, but this hacks is outdated and wii definitely break with current LaTeX – egreg Jul 09 '23 at 18:03
  • Alright. Thoughts on how to do this in recent latex, overleaf, etc? – Hugh Perkins Jul 12 '23 at 11:21