【问题标题】:Combine LaTeX with R markdown and pandoc将 LaTeX 与 R markdown 和 pandoc 结合使用
【发布时间】:2013-08-26 15:28:38
【问题描述】:

我创建了一个R markdown (RStudio) 文档,我希望将 pdf 格式的输出与我的 LaTeX 文档的某些部分(即第一页)结合起来。所以基本上,我想用 R Markdown 做一些事情(生成一些 R 输出)并在 LaTeX 中写下一些东西,并将这两个组合成一个 pdf 文档。

步骤是什么?谢谢。

【问题讨论】:

  • 在 LaTeX 文档中从 Rmd (pandoc -o stuff.tex stuff.rmd) 和 \include{} 生成 TeX?
  • @Ben:这可能是一种选择,但仍然想知道 Rmd+tex=pdf 是否可行。
  • 好的,您更喜欢生成两个PDF文档并合并它们吗?
  • 您是否尝试过在 .Rmd 文件中简单地编写 LaTeX 代码?通常 pandoc 识别 LaTeX 代码,并在转换为 PDF 时使用它。我一直在我的 .md/.Rmd 文件中使用基本的 LaTeX、/换行符、特殊符号、表格等。
  • 您可以将 Latex 中的 pdf 与 pdfpages 合并。

标签: r latex rstudio


【解决方案1】:

为了演示如何使用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 包含为所创建文档的 sectionssubsections 通常很有用,这是通过以下方式完成的:

\phantomsection
\addcontentsline{toc}{subsection}{Second subsection (phantom)}

输出显示一页中的四页,其中一页留空,目录带有幻像部分

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2019-04-14
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多