[Tex/LaTex] Customizing the height of the pages

paper-sizepdftex

The question

I would like to control the height of the pages of the PDF documents I produce with LaTeX.

Starting with a LaTeX document and two parameters:

  • a given page size (heightPage and widthPage)
  • a given length called initialVerticalInset,

I would like to produce a PDF whose page lengths follow the pattern

  • the normal height of the pages is heightPage
  • BUT, the height of the first page is (heightPage - initialVerticalInset)
  • AND, the height of the last page equals the "height of its content", i.e. "the last page is cut" after the end of its content

If possible, I would like also :

  • if any of these pages are put next to another one, the baselines should be aligned
  • the margins could be set a minimal value (such as 0pt)

Example 1

enter image description here

enter image description here


Example 2

enter image description here


Minimal documents to be tested

In a previous similar question, I noticed that one difficulty could be the compatibility with :

  • itemize environments
  • mdframed environments
  • the case of short documents has to be dealt with too.

So, I prepared three MWE to be tested.

Short document

\documentclass{article}

\usepackage[paperwidth=8cm, paperheight=10cm, hmargin=0mm, 
    top=0mm, bottom=0mm] {geometry}

\setlength{\initialVerticalInset}{4.5cm}


\begin{document}

This is a test. This is a longer sentence with some more words, isn't it? 

\end{document}

Documents with itemize environments

\documentclass{article}

\usepackage[paperwidth=8cm, paperheight=10cm, hmargin=0mm, 
    top=0mm, bottom=0mm] {geometry}


\pagestyle{empty}

\setlength{\initialVerticalInset}{4.5cm}


\begin{document}


This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test:
\begin{itemize}
\item Hello

\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.

\item Good Bye

\item Hello

\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\end{itemize}

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? 
\end{document}

Documents with mdframed environments

\documentclass{article}
\usepackage[
  paperwidth=15cm,
  paperheight=15cm,
  hmargin=1cm,
  vmargin=0cm,
]{geometry}

\usepackage[framemethod=tikz, needspace=1.5cm]{mdframed}

\newmdenv[%
innerleftmargin = 2mm,
innerrightmargin = 2mm,
innertopmargin = 2mm,
innerbottommargin = 2mm,
leftmargin = 0mm,
rightmargin = 0mm,
splitbottomskip = 2mm,
splittopskip = 4mm,
middlelinewidth = 0mm,
linecolor = red,
backgroundcolor = red,
roundcorner = 0pt,
skipbelow = 0mm,
skipabove = 0mm,
]{mybox}

\pagestyle{empty}

\setlength{\initialVerticalInset}{5cm}

\begin{document}

\begin{mybox}
\lipsum[1]
\end{mybox}
\lipsum[1-7]

\end{document}

Best Answer

I think this does the job. Thnaks to the idea \pdfpageheight=\pagetotal from wipet's answer How to have a precise control on the size of each page?

\documentclass{article}

\newlength{\initialVerticalInset}
\setlength{\initialVerticalInset}{4.5cm}

\usepackage[paperwidth=10cm, 
            paperheight=-\dimexpr\initialVerticalInset- 10cm\relax,
            margin=0mm]{geometry}

\makeatletter

\newcommand{\setnewvsize}[1]{%
\global\paperheight\dimexpr#1+\initialVerticalInset\relax%
\global\pdfpageheight\paperheight%
\global\textheight\paperheight%
\global\@colht\textheight \global\@colroom\textheight \global\vsize\textheight%
\global\initialVerticalInset=0pt}


\let\mt@outputpage\@outputpage
\def\@outputpage{\mt@outputpage\setnewvsize\paperheight}

\makeatother

\AtEndDocument{\initialVerticalInset=0pt\vskip-\lastskip\nobreak\par\vskip1pt\par\setnewvsize\pagetotal}

\usepackage{lipsum}

\begin{document}


This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test:
\begin{itemize}
\item Hello

\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.

\item Good Bye

\item Hello

\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\end{itemize}

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?

This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? 

\end{document}

Itemize

enter image description here

mdframed

enter image description here

Example 2 pages only

enter image description here

Example only one page

enter image description here

Related Question