16 Page Management

This section describes the pgfpages packages. Although this package is not concerned with creating pictures, its implementation relies so heavily on PGF that it is documented here. Currently, pgfpages only works with LATEX, but if you are adventurous, feel free to hack the code so that it also works with plain TEX.

The aim of pgfpages is to provide a flexible way of putting multiple pages on a single page inside TEX. Thus, pgfpages is quite different from useful tools like psnup or pdfnup insofar as it creates its output in a single pass. Furthermore, it works uniformly with both latex and pdflatex, making it easy to put multiple pages on a single page without any fuss.

A word of warning: using pgfpages will destroy hyperlinks. Actually, the hyperlinks are not destroyed, only they will appear at totally wrong positions on the final output. This is due to a fundamental flaw in the PDF specification: In PDF the bounding rectangle of a hyperlink is given in “absolute page coordinates” and translations or rotations do not affect them. Thus, the transformations applied by pgfpages to put the pages where you want them are (cannot, even) be applied to the coordinates of hyperlinks. It is unlikely that this will change in the foreseeable future.

16.1 Basic Usage

The internals of pgfpages are complex since the package can do all sorts of interesting tricks. For this reason, so-called layouts are predefined that setup all option in appropriate ways.

You use a layout as follows:


\documentclass{article}

\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,landscape,border shrink=5mm]

\begin{document}
This text is shown on the left.
\clearpage
This text is shown on the right.
\end{document}

The layout 2 on 1 puts two pages on a single page. The option a4paper tells pgfpages that the resulting page (called the physical page in the following) should be a4paper and it should be landscape (which is quite logical since putting two portrait pages next to each other gives a landscape page). Normally, the logical pages, that is, the pages that TEX “thinks” that it is typesetting, will have the same sizes, but this need not be the case. pgfpages will automatically scale down the logical pages such that two logical pages fit next to each other inside a DIN A4 page.

The border shrink tells pgfpages that it should add an additional 5mm to the shrinking such that a 5mm-wide border is shown around the resulting logical pages.

As a second example, let us put two pages produced by the BEAMER class on a single page:


\documentclass{beamer}

\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

\begin{document}
\begin{frame}
  This text is shown at the top.
\end{frame}
\begin{frame}
  This text is shown at the bottom.
\end{frame}
\end{document}

Note that we do not use the landscape option since BEAMER’s logical pages are already in landscape mode and putting two landscape pages on top of each other results in a portrait page. However, if you had used the 4 on 1 layout, you would have had to add landscape once more, using the 8 on 1 you must not, using 16 on 1 you need it yet again. And, no, there is no 32 on 1 layout.

Another word of caution: using pgfpages will produce wrong page numbers in the .aux file. The reason is that TEX instantiates the page numbers when writing an .aux file only when the physical page is shipped out. Fortunately, this problem is easy to fix: First, typeset our file normally without using the \pgfpagesuselayout command (just put the comment marker % before it) Then, rerun TEX with the \pgfpagesuselayout command included and add the command \nofiles. This command ensures that the .aux file is not modified, which is exactly what you want. So, to typeset the above example, you should actually first TEX the following file:


\documentclass{article}

\usepackage{pgfpages}
%%\pgfpagesuselayout{2 on 1}[a4paper,landscape,border shrink=5mm]
%%\nofiles

\begin{document}
This text is shown on the left.
\clearpage
This text is shown on the right.
\end{document}

and then typeset


\documentclass{article}

\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,landscape,border shrink=5mm]
\nofiles

\begin{document}
This text is shown on the left.
\clearpage
This text is shown on the right.
\end{document}

The final basic example is the resize to layout (it works a bit like a hypothetical 1 on 1 layout). This layout resizes the logical page such that is fits the specified physical size. Since this does not change the page numbering, you need not worry about the .aux files with this layout. For example, adding the following lines will ensure that the physical output will fit on DIN A4 paper:


\usepackage{pgfpages}
\pgfpagesuselayout{resize to}[a4paper]

This can be very useful when you have to handle lots of papers that are typeset for, say, letter paper and you have an A4 printer or the other way round. For example, the following article will be fit for printing on letter paper:


\documentclass[a4paper]{article}
%% a4 is currently the logical size and also the physical size

\usepackage{pgfpages}
\pgfpagesuselayout{resize to}[letterpaper]
%% a4 is still the logical size, but letter is the physical one

\begin{document}
  \title{My Great Article}
...
\end{document}

16.2 The Predefined Layouts

This section explains the predefined layouts in more detail. You select a layout using the following command:

\pgfpagesuselayout{<layout>}[<options>]

Installs the specified <layout> with the given <options> set. The predefined layouts and their permissible options are explained below.

If this function is called multiple times, only the last call “wins.” You can thereby overwrite any previous settings. In particular, layouts do not accumulate.

Example: \pgfpagesuselayout{resize to}[a4paper]

\pgfpagesuselayout{resize to}[<options>]

This layout is used to resize every logical page to a specified physical size. To determine the target size, the following options may be given:

\pgfpagesuselayout{2 on 1}[<options>]

Puts two logical pages alongside each other on each physical page if the logical height is larger than the logical width (logical pages are in portrait mode). Otherwise, two logical pages are put on top of each other (logical pages are in landscape mode). When using this layout, it is advisable to use the \nofiles command, but this is not done automatically.

The same <options> as for the resize to layout an be used, plus the following option:

\pgfpagesuselayout{4 on 1}[<options>]

Puts four logical pages on a single physical page. The same <options> as for the resize to layout an be used.

\pgfpagesuselayout{8 on 1}[<options>]

Puts eight logical pages on a single physical page. As for 2 on 1, the orientation depends on whether the logical pages are in landscape mode or in portrait mode.

\pgfpagesuselayout{16 on 1}[<options>]

This is for the CEO.

\pgfpagesuselayout{rounded corners}[<options>]

This layout adds “rounded corners” to every page, which, supposedly, looks nicer during presentations with projectors (personally, I doubt this). This is done by (possibly) resizing the page to the physical page size. Then four black rectangles are drawn in each corner. Next, a clipping region is set up that contains all of the logical page except for little rounded corners. Finally, the logical page is draw, clipped against the clipping region.

Note that every logical page should fill its background for this to work.

In addition to the <options> that can be given to resize to the following options may be given.


\documentclass{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{rounded corners}[corner width=5pt]
\begin{document}
...
\end{document}

\pgfpagesuselayout{two screens with lagging second}[<options>]

This layout puts two logical pages alongside each other. The second page always shows what the main page showed on the previous physical page. Thus, the second page “lags behind” the main page. This can be useful when you have to projectors attached to your computer and can show different parts of a physical page on different projectors.

The following <options> may be given:

\pgfpagesuselayout{two screens with optional second}[<options>]

This layout works similarly to two screens with lagging second. The difference is that the contents of the second screen only changes when one of the commands \pgfshipoutlogicalpage{2}{<box>} or \pgfcurrentpagewillbelogicalpage{2} is called. The first puts the given <box> on the second page. The second specifies that the current page should be put there, once it is finished.

The same options as for two screens with lagging second may be given.

You can define your own predefined layouts using the following command:

\pgfpagesdeclarelayout{<layout>}{<before actions>}{<after actions>}

This command predefines a <layout> that can later be installed using the \pgfpagesuselayout command.

When \pgfpagesuselayout{<layout>}[<options>] is called, the following happens: First, the <before actions> are executed. They can be used, for example, to setup default values for keys. Next, \setkeys{pgfpagesuselayoutoption}{<options>} is executed. Finally, the <after actions> are executed.

Here is an example:


\pgfpagesdeclarelayout{resize to}
{
  \def\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=1,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth%
  }
  \pgfpageslogicalpageoptions{1}
  {%
    resized width=\pgfphysicalwidth,%
    resized height=\pgfphysicalheight,%
    border shrink=\pgfpageoptionborder,%
    center=\pgfpoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight}%
  }%
}

16.3 Defining a Layout

If none of the predefined layouts meets your problem or if you wish to modify them, you can create layouts from scratch. This section explains how this is done.

Basically, pgfpages hooks into TEX’s \shipout function. This function is called whenever TEX has completed typesetting a page and wishes to send this page to the .dvi or .pdf file. The pgfpages package redefines this command. Instead of sending the page to the output file, pgfpages stores it in an internal box and then acts as if the page had been output. When TEX tries to output the next page using \shipout, this call is once more intercepted and the page is stored in another box. These boxes are called logical pages.

At some point, enough logical pages have been accumulated such that a physical page can be output. When this happens, pgfpages possibly scales, rotates, and translates the logical pages (and possibly even does further modifications) and then puts them at certain positions of the physical page. Once this page is fully assembled, the “real” or “original” \shipout is called to send the physical page to the output file.

In reality, things are slightly more complicated. First, once a physical page has been shipped out, the logical pages are usually voided, but this need not be the case. Instead, it is possible that certain logical page just retain their contents after the physical page has been shipped out and these pages need not be filled once more before a physical shipout can occur. However, the contents of these logical pages can still be changed using special commands. It is also possible that after a shipout certain logical pages are filled with the contents of other logical pages.

A layout defines for each logical page where it will go on the physical page and which further modifications should be done. The following two commands are used to define the layout:

\pgfpagesphysicalpageoptions{<options>}

This command sets the characteristic of the “physical” page. For example, it is used to specify how many logical pages there are and how many logical pages must be accumulated before a physical page is shipped out. How each individual logical page is typeset is specified using the command \pgfpageslogicalpageoptions, described later.

Example: A layout for putting two portrait pages on a single landscape page:


\pgfpagesphysicalpageoptions
{%
  logical pages=2,%
  physical height=\paperwidth,%
  physical width=\paperheight,%
}

\pgfpageslogicalpageoptions{1}
{%
  resized width=.5\pgfphysicalwidth,%
  resized height=\pgfphysicalheight,%
  center=\pgfpoint{.25\pgfphysicalwidth}{.5\pgfphysicalheight}%
}%
\pgfpageslogicalpageoptions{2}
{%
  resized width=.5\pgfphysicalwidth,%
  resized height=\pgfphysicalheight,%
  center=\pgfpoint{.75\pgfphysicalwidth}{.5\pgfphysicalheight}%
}%

The following <options> may be set:

\pgfpageslogicalpageoptions{<logical page number>}{<options>}

This command is used to specify where the logical page number <logical page number> will be placed on the physical page. In addition, this command can be used to install additional “code” to be executed when this page is put on the physical page.

The number <logical page number> should be between 1 and <logical pages>, which has previously been installed using the \pgfpagesphysicalpageoptions command.

The following <options> may be given:

16.4 Creating Logical Pages

Logical pages are created whenever a TEX thinks that a page is full and performs a \shipout command. This will cause pgfpages to store the box that was supposed to be shipped out internally until enough logical pages have been collected such that a physical shipout can occur.

Normally, whenever a logical shipout occurs that current page is stored in logical page number <current logical page>. This counter is then incremented, until it is larger than <last logical shipout>. You can, however, directly change the value of <current logical page> by calling \pgfpagesphysicalpageoptions.

Another way to set the contents of a logical page is to use the following command:

\pgfpagesshipoutlogicalpage{<number>}<box>

This command sets to logical page <number> to <box>. The <box> should be the code of a TEX box command. This command does not influence the counter <current logical page> and does not cause a physical shipout.


\pgfpagesshipoutlogicalpage{0}\vbox{Hi!}

This command can be used to set the contents of logical pages that are normally not filled.

The final way of setting a logical page is using the following command:

\pgfpagescurrentpagewillbelogicalpage{<number>}

When the current TEX page has been typeset, it will be become the given logical page <number>. This command “interrupts” the normal order of logical pages, that is, it behaves like the previous command and does not update the <current logical page> counter.


\pgfpagesuselayout{two screens with optional second}
...
Text for main page.
\clearpage

\pgfpagescurrentpagewillbelogicalpage{2}
Text that goes to second page
\clearpage

Text for main page.