[Tex/LaTex] Imposition for an 8 page booklet using pgfpages

pgfpagestikz-pgf

I want to prepare an 8 page booklet by imposing the 8 pages, four at a time on a larger sheet of paper. The front side will look like this:

Front of imposition

and the back will look like this:

Back of imposition

When printed correctly, looking through the front to the back will look like this:

Both sides of the printed imposition

Folding: With the back side face up (the '7' at the lower right hand corner) fold the sheet in half left-to-right. The '4' is now at the top, the '5' at the bottom — fold this in half top-to-bottom. You now have an 8 page booklet — just trim a little off the bottom and staple. With Werner's code from this answer and pgfpages I can typeset first the FRONT (save the PDF of the front), typeset the BACK, combine the two PDFs into one and then print the two pages onto one sheet back-to-back:

\documentclass{article}

\usepackage[papersize={5.5in,8.5in},margin=0.25in]{geometry}
\usepackage{pgfpages}
\usepackage{tikz}
\usepackage{lipsum,atbegshi,etoolbox,everypage,tikzpagenodes}

\makeatletter
\newcommand{\discardpages}[1]{% \discardpages{<csv list>}
  \xdef\discard@pages{#1}% Store pages to discard
  \AtBeginShipout{% At shipout, decide whether to discard page/not
    \renewcommand*{\do}[1]{% How to handle each page entry in csv list
      \ifnum\value{page}=##1\relax%
        \AtBeginShipoutDiscard% Discard page/not
        \gdef\do####1{}% Do nothing further
      \fi%
    }%
    \expandafter\docsvlist\expandafter{\discard@pages}% Process list of pages to discard
  }%
}
\newif\ifkeeppage
\newcommand{\keeppages}[1]{% \keeppages{<csv list>}
  \xdef\keep@pages{#1}% Store pages to keep
  \AtBeginShipout{% At shipout, decide whether to discard page/not
    \keeppagefalse%
    \renewcommand*{\do}[1]{% How to handle each page entry in csv list
      \ifnum\value{page}=##1\relax%
        \keeppagetrue% Page should be kept
        \gdef\do####1{}% Do nothing further
      \fi%
    }%
    \expandafter\docsvlist\expandafter{\keep@pages}% Process list of pages to keep
    \ifkeeppage\else\AtBeginShipoutDiscard\fi% Discard page/not
  }%
}

\pgfpagesdeclarelayout{Impose4upFront}
{
  \edef\pgfpageoptionheight{\the\paperheight} 
  \edef\pgfpageoptionwidth{\the\paperwidth}
  \edef\pgfpageoptionborder{15pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=4,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{1}%{2} UL
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.25\pgfphysicalwidth}{.75\pgfphysicalheight},
    rotation = 90%
  }%
  \pgfpageslogicalpageoptions{4}%{3} LL
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.25\pgfphysicalwidth}{.25\pgfphysicalheight},%
    rotation=90%
  }%
  \pgfpageslogicalpageoptions{2}%{1} UR
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.75\pgfphysicalwidth}{.75\pgfphysicalheight},%
    rotation = -90%
  }%
  \pgfpageslogicalpageoptions{3}%{4} LR
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.75\pgfphysicalwidth}{.25\pgfphysicalheight},%
    rotation =-90
  }%
}


\pgfpagesdeclarelayout{Impose4upBack}
{
  \edef\pgfpageoptionheight{\the\paperheight} 
  \edef\pgfpageoptionwidth{\the\paperwidth}
  \edef\pgfpageoptionborder{15pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=4,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{2}%{1} UL
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.25\pgfphysicalwidth}{.75\pgfphysicalheight},%
    rotation = 90%
  }%
  \pgfpageslogicalpageoptions{3}%{4} LL
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.25\pgfphysicalwidth}{.25\pgfphysicalheight},%
    rotation=90%
  }%
  \pgfpageslogicalpageoptions{1}%{2} UR
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.75\pgfphysicalwidth}{.75\pgfphysicalheight},%
    rotation = -90%
  }%
  \pgfpageslogicalpageoptions{4}%{3} LR
  {%
    border shrink=\pgfpageoptionborder,%
    resized width=0.5\pgfphysicalheight,%
    resized height=0.5\pgfphysicalwidth,%
    center=\pgfpoint{.75\pgfphysicalwidth}{.25\pgfphysicalheight},%
    rotation =-90%
  }%
}

%% To use, comment-out either pair of the next four lines.
%% Comment-out all four lines to typeset the document normally.

%\keeppages{1,4,5,8}% Front
%\pgfpagesuselayout{Impose4upFront}[physical paper height = 11in, physical paper width = 17in]  
\keeppages{2,3,6,7}% Back
\pgfpagesuselayout{Impose4upBack}[physical paper height = 11in, physical paper width = 17in]

\pagestyle{empty}

%% From everypage.sty:
\AddEverypageHook{%
    \tikz[remember picture,overlay] \draw[thin,red] (current page text area.south west) rectangle (current page text area.north east);%
}

\begin{document}

\centering

\foreach \i in {1,...,8}{%
\resizebox{!}{2in}{\Huge\i}%
\clearpage
}

\end{document}

The question is this: Can this be done in one pass? The manual labor is not great using two passes, but curiosity and laziness urge me to pose the question. I would also point out that pgfpages is extraordinarily useful. Actually, I'm kind of amazed this works at all…

Best Answer

(Note: this solution uses one pass... but three compilations ! ;-))

Here is a solution using three documents and pdfpages. Compile these three documents in order to get your final booklet.

First document with 8 pages A4 (book-pages.tex):

\documentclass[a4paper]{article}
\begin{document}
\Huge 1 \newpage
\Huge 2 \newpage
\Huge 3 \newpage
\Huge 4 \newpage
\Huge 5 \newpage
\Huge 6 \newpage
\Huge 7 \newpage
\Huge 8
\end{document}

Second document (booklet1.tex):

\documentclass[a4paper]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=-,signature*=8,landscape,angle=180]{book-pages.pdf}
\end{document}

Third document (booklet2.tex):

\documentclass[a4paper,landscape]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=-,signature=2]{booklet1.pdf}
\end{document}
Related Question