为了演示如何使用pdfpages package 插入 pdf 页面,我首先创建
使用pdf() 命令的四页pdf 文件。
.Rnw (Knitr) 脚本如下
\documentclass[a4paper]{article}
\usepackage[final]{pdfpages}
\usepackage[pagestyles]{titlesec}
\usepackage{hyperref}
\title{An example on how to add external pdf pages}
\begin{document}
\maketitle
\tableofcontents
\section{First section}
\subsection{First subsection}
This is an empty section with a chunk to create one pdf with four pages
<<mtcarsplot, echo = TRUE, eval = TRUE, fig.show='hide'>>=
library(knitr)
pdf(file="figure/mtcarsplot.pdf",onefile=TRUE)
ggplot(mpg, aes(drv, model)) +
geom_point() +
facet_grid(manufacturer ~ ., scales = "free", space = "free") +
theme(strip.text.y = element_text(angle = 0))
ggplot(mtcars, aes(wt, mpg))+ geom_point(aes(colour=factor(cyl), size = qsec))
ggplot(mpg, aes(x=factor(cyl), y=hwy, fill=factor(cyl)))+ geom_violin(scale = "width")
mosaicplot(Titanic, color = TRUE)
dev.off()
@
\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}
\includepdf[pages={1-2,{},4},nup=2x2]{figure/mtcarsplot.pdf}
\end{document}
要为外部文档插入页面,请使用
\includepdf[<key=val>]{<filename>}
最简单的是\includepdf[pages=-],它将包含文档中的所有页面。
包含页面的代码是
\includepdf[pages={1-2,{},4}],nup=2x2]{figure/mtcarsplot.pdf}
{1-2,{},4} 表示我包含了第 1 到 2 页,一个空白页,然后是第 4 页。
另一个命令是nup=2x2,它包含两行两列的四页,同一页。
将外部 pdf 包含为所创建文档的 sections 或 subsections 通常很有用,这是通过以下方式完成的:
\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}
输出显示一页中的四页,其中一页留空,目录带有幻像部分。