I have created a R markdown
(RStudio) document and I would like to have that output in pdf combined with some part (i.e. first page) of my LaTeX document. So basically, I would like to do some bits with R Markdown (generating some R output) and write something down in LaTeX and have this two combined into 1 pdf document.
我已经创建了一个R markdown(RStudio)文档,我希望将pdf中的输出与我的LaTeX文档的某些部分(即第一页)结合起来。所以基本上,我想用R Markdown做一些比特(生成一些R输出)并在LaTeX中写下一些内容并将这两个结合成1 pdf文档。
What would be the steps? Thx.
步骤是什么?谢谢。
1 个解决方案
#1
1
To demonstrate how to insert pdf pages using the pdfpages package, I first create a pdf file with four pages using the pdf()
command.
为了演示如何使用pdfpages包插入pdf页面,我首先使用pdf()命令创建一个包含四个页面的pdf文件。
The .Rnw (Knitr) script is as following
.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}
To insert pages for the external document use
要插入外部文档的页面
\includepdf[<key=val>]{<filename>}
The simplest will be \includepdf[pages=-]
which will include all the pages from the document.
最简单的是\ includepdf [pages = - ],它将包含文档中的所有页面。
The code to include pages is
包含页面的代码是
\includepdf[pages={1-2,{},4}],nup=2x2]{figure/mtcarsplot.pdf}
{1-2,{},4} means that I included page 1 to 2, a blank page, then page 4.
{1-2,{},4}表示我包含第1页到第2页,空白页,第4页。
The other command is nup=2x2 which has included four pages in two rows and two columns withing the same page.
另一个命令是nup = 2x2,其中包含两行和两列具有相同页面的四个页面。
It is often usefull to include external pdf as sections or subsections of the document created, this is done with :
将外部pdf作为创建文档的部分或子部分包含在内是很有用的,这可以通过以下方式完成:
\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}
The output shows the four pages in one page, with one left blank, and the table of contents with the phantom section.
输出显示一页中的四个页面,其中一个留空,而内容表带有幻像部分。
#1
1
To demonstrate how to insert pdf pages using the pdfpages package, I first create a pdf file with four pages using the pdf()
command.
为了演示如何使用pdfpages包插入pdf页面,我首先使用pdf()命令创建一个包含四个页面的pdf文件。
The .Rnw (Knitr) script is as following
.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}
To insert pages for the external document use
要插入外部文档的页面
\includepdf[<key=val>]{<filename>}
The simplest will be \includepdf[pages=-]
which will include all the pages from the document.
最简单的是\ includepdf [pages = - ],它将包含文档中的所有页面。
The code to include pages is
包含页面的代码是
\includepdf[pages={1-2,{},4}],nup=2x2]{figure/mtcarsplot.pdf}
{1-2,{},4} means that I included page 1 to 2, a blank page, then page 4.
{1-2,{},4}表示我包含第1页到第2页,空白页,第4页。
The other command is nup=2x2 which has included four pages in two rows and two columns withing the same page.
另一个命令是nup = 2x2,其中包含两行和两列具有相同页面的四个页面。
It is often usefull to include external pdf as sections or subsections of the document created, this is done with :
将外部pdf作为创建文档的部分或子部分包含在内是很有用的,这可以通过以下方式完成:
\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}
The output shows the four pages in one page, with one left blank, and the table of contents with the phantom section.
输出显示一页中的四个页面,其中一个留空,而内容表带有幻像部分。