1. Introduction

In this tutorial, we’ll explain how to include .pdf files in LaTeX documents.

We can include .pdf figures using the graphicx package and the \includegraphics command. However, sometimes, we need to include a range of pages from an existing PDF in a new LaTeX document.

2. Package pdfpages

The package pdfpages offers precisely such functionalities:

\usepackage{pdfpages}

It requires the following packages: atbegshi, pdflscape, graphicx, ifthen, and calc. These are available in all standard LaTeX distributions.

We also require a recent version of pdftex.def.

To use it, we declare pdfpages in the preamble, and thus any missing packages are automatically loaded so we don’t need to load them explicitly.

The package pdfpages runs on pdflatex, xelatex, and lualatex engines.

3. The \includepdf Command

The package offers the command \includepdf for including pages from an existing PDF. Its syntax is:

\includepdf[options]{filename}

There, options is a comma-separated list of key=value pairs, each denoting a specific option of the command. The filename shouldn’t have blank spaces, otherwise, the inclusion will fail.

We explain the main options to \includepdf in the following subsections.

3.1. Option pages

The option pages specifies which pages to include. We can specify them as a list, e.g., pages={3,4,5, 7, 11}.

However, consecutive pages can be specified by their start and end: pages={3-5, 7, 11}.

To insert an empty page, we add {}, e.g., pages={3-5, 7, {}, 11}. That will insert a blank page between the seventh and the eleventh pages of the specified PDF.

Let’s say that we have a PDF named alice_in_wonderland.pdf and that we want to include its third and fourth pages:

\documentclass{article}
\usepackage{lipsum}
\usepackage{pdfpages}
\begin{document}
{\raggedright
\lipsum[1]
\includepdf[pages={3-4}]{alice_in_wonderland.pdf}
\lipsum[2]
}
\end{document}

The lipsum package generates a pseudo-Latin text for test purposes. We can see the output includes two pages from alice_in_wonderland.pdf, which we’ve set in a different font to distinguish them from the main text:

First page of original file Second page of original file First page of included file Second page of included file Third page of original file

3.2. Options landscape and angle

To rotate the sheet of paper by 90º, we set landscape=true. This only affects the sheet and not the inserted pages.

To rotate the included pages, we use the option angle. For instance, rotating by 45º:

\includepdf[angle=45,pages={3}]{alice_in_wonderland.pdf}

gives us this result:

First page of original file Second page of original file Page three of alice_in_wonderland rotated through forty-five degrees Fourth page of original file

The default values are landscape=false and angle=0.

3.3. Options nup, delta, scale, and offset

The option nup lets us place multiple pages on each sheet of paper. Its syntax is: nup=⟨xnup⟩x⟨ynup⟩. Here, ⟨xnup⟩ and ⟨ynup⟩ designate the number of inserted pages in the horizontal and vertical directions on each sheet of paper. The default is nup=1×1, i.e, an included page per sheet.

On the other hand, delta controls the horizontal and vertical spacing between the inserted pages (when including multiple pages per sheet). The argument is two-dimensional:

delta = xdelta ydelta

For example, delta=-1.75in -2.0in, reduces the space between the inserted pages by 1.75in and 2in in the horizontal and vertical directions.

The option offset lets us translate the origin of the inserted pages. It’s a two-dimensional option, so we set the offset by specifying the horizontal and vertical offsets: offset=xoffset yoffset. We can make them positive or negative. The default offsets are zeroes.

Finally, scale lets us resize the pages we include.

Here’s an example of using all four options:

\lipsum[1-2]

\includepdf[scale=0.65, nup=3x2, pages={3-8},
            offset=-0.25in -0.25in,
            delta=-1.75in -2.00in]{alice_in_wonderland.pdf}

\lipsum[3]

Here’s the result we get:

First page of original file Second page of original file Pages 3-8 of alice_in_wonderland file inserted into one page of original file fourth page of original file

The nup=3×2 specification means that we would like the alice_in_wonderland.pdf pages to be arranged in 3 columns of 2 rows. We scale the included pages by 65% and set the offsets from the edgesof the page to -0.25in. Further, we adjust the spaces between the included pages. Usually, we’ll need some experimentation to set the scale, offset, and delta correctly.

3.4. Option reflect

This option lets us reflect the included pages. To do that, we set reflect=true. The default is reflect=false.

Here’s an example:

\lipsum[1]

\includepdf[scale=0.65,nup=2x2,
            reflect,pages={3-6},
            offset=-0.25in -0.25in,
            delta=-2.0in -3.00in]{alice_in_wonderland.pdf}

\lipsum[2]

This is the result:

First page of original file four pages of alice_in_wonderland file, reflected and included on one page Fourth page of original file

As we see, the inserted pages were reflected around their middles.

4. Coloring the Included Pages

Sometimes, we want to color the included pages to make them appear different. We can do that by using \pagecolor{}.

However, **the \pagecolor{} command must come before the \usepackage{pdfpages} declaration for this to work:
**

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\pagecolor{white}
\usepackage{pdfpages}
\begin{document}
{
\raggedright

\lipsum[2]

\newpage{
\pagecolor{olive}
\includepdf[pages={3}]{alice_in_wonderland.pdf}
}

\pagecolor{white}
\lipsum[3]
}
\end{document}

Here’s the output:

First page of original file thirs page of alice_in_wonderland file, colored olive third page of original file

We can change the color later (as we did in the example, from white to olive), but the first \pagecolor command must come before \usepackage{pdfpages}.

5. Conclusion

In this article, we talked about importing pages from an existing .pdf file into a LaTeX document. We used the pdfpages package for this purpose. **It can include multiple pages and arrange them in various ways.
**


» 下一篇: 重力/珠子排序