1. 简介

在本教程中,我们将介绍如何在 LaTeX 文档中嵌入 PDF 文件。

LaTeX 中插入 PDF 图像可以通过 graphicx 包和 \includegraphics 命令实现。但有时候,我们希望将一个现有 PDF 的某些页面嵌入到一个新的 LaTeX 文档中。这时候就需要使用 pdfpages 这个功能更强的包。

2. 使用 pdfpages

pdfpages 包提供了嵌入 PDF 页面的功能:

\usepackage{pdfpages}

它依赖以下几个包:atbegshipdflscapegraphicxifthencalc,这些包在标准的 LaTeX 发行版中都有提供。

你只需要在导言区声明 pdfpages,其他依赖包会自动加载,无需手动引入。

支持的编译引擎pdflatexxelatexlualatex 都支持该包。

⚠️ 注意:需要确保 pdftex.def 是较新的版本,否则可能导致某些功能不可用。

3. \includepdf 命令详解

pdfpages 提供了 \includepdf 命令用于插入 PDF 页面,其基本语法如下:

\includepdf[options]{filename}

其中 options 是一个逗号分隔的 key=value 列表,用于控制插入方式。filename 不应包含空格,否则插入会失败。

下面介绍几个常用的 options 选项。

3.1. pages:指定插入的页面

使用 pages 可以指定要插入的页面号。例如:

  • pages={3,4,5,7,11}:插入第 3、4、5、7 和 11 页
  • pages={3-5,7,11}:插入第 3 到 5 页、第 7 页和第 11 页

你也可以插入空白页,使用 {}

pages={3-5,7,{},11}

这将在第 7 页和第 11 页之间插入一个空白页。

示例代码

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

这个例子中,我们插入了 alice_in_wonderland.pdf 的第 3 和第 4 页。

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. landscapeangle:旋转页面

  • landscape=true:将整张纸张旋转 90 度(仅影响纸张方向,不旋转内容)
  • angle=45:将插入的页面内容旋转 45 度

示例代码

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

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

默认值:landscape=falseangle=0

3.3. nupdeltascaleoffset:多页布局与排版调整

  • nup=⟨xnup⟩x⟨ynup⟩:每页纸张上排布的 PDF 页面数,例如 nup=3x2 表示每页排 3 列 2 行
  • delta=x y:控制每页之间的间距,单位为 incm
  • scale=0.65:缩放页面大小为 65%
  • offset=x y:调整页面的偏移量

示例代码

\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]

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

⚠️ 提示:这些参数通常需要多次调整才能达到理想效果。

3.4. reflect:镜像翻转页面

设置 reflect=true 可以将插入的页面进行镜像翻转。

示例代码

\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]

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

插入的页面被以中间为轴进行了翻转。

4. 给插入页面添加背景颜色

有时候我们希望插入的页面具有不同的背景色以区分主文档内容。可以使用 \pagecolor{} 命令实现。

⚠️ 注意\pagecolor{} 必须放在 \usepackage{pdfpages} 之前才有效。

示例代码

\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}

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

你可以随时更改背景色,但首次设置必须在 pdfpages 包加载之前。

5. 总结

本文介绍了如何在 LaTeX 中插入 PDF 文件的某些页面。我们使用了 pdfpages 包提供的 \includepdf 命令,它支持插入多个页面,并可进行多页排版、缩放、旋转、翻转、着色等操作。

常用功能一览

功能 参数说明
插入页面 pages={1-5,7}
旋转纸张 landscape=true
旋转内容 angle=45
多页排版 nup=2x3
页面缩放 scale=0.5
页面偏移 offset=-0.25in -0.25in
页面间距 delta=-1.75in -2.00in
镜像翻转 reflect=true
设置背景色 \pagecolor{olive}(需提前声明)

如果你经常需要将 PDF 嵌入到 LaTeX 文档中,pdfpages 是一个非常实用的工具。


原始标题:How to Include PDF Files in LaTeX Documents

» 下一篇: Gravity/Bead Sort