Rstudio Rmarkdown:在一个PDF中,纵向和横向布局

我想知道如何使用rmarkdown来生成在同一个文档中同时具有纵向和横向布局的pdf。 如果有一个纯粹的rmarkdown选项,会比使用胶乳更好。

这是一个小的,可重复的例子。 首先,在RStudio中渲染这个.Rmd (按下Knit PDFbutton)将生成一个pdf格式,其中所有的页面都是横向布局:

 --- title: "All pages landscape" output: pdf_document classoption: landscape --- ```{r} summary(cars) ``` \newpage ```{r} summary(cars) ``` 

然后尝试创build一个混合纵向和横向布局的文档。 YAML的基本设置是根据这里的“包含”部分完成的。 in_header文件“header.tex”只包含\usepackage{lscape} ,这是一个针对knitr横向布局的软件包。 .tex文件与.tex文件位于同一目录中。

 --- title: "Mixing portrait and landscape" output: pdf_document: includes: in_header: header.tex --- Portrait: ```{r} summary(cars) ``` \newpage \begin{landscape} Landscape: ```{r} summary(cars) ``` \end{landscape} \newpage More portrait: ```{r} summary(cars) ``` 

但是,此代码导致错误:

 # ! You can't use `macro parameter character #' in horizontal mode. # l.116 # # pandoc.exe: Error producing PDF from TeX source # Error: pandoc document conversion failed with error 43 

任何帮助深表感谢。

所以, pandoc 不会分析latex环境的内容,但可以通过在header.tex文件中重新定义命令来欺骗它:

 \usepackage{lscape} \newcommand{\blandscape}{\begin{landscape}} \newcommand{\elandscape}{\end{landscape}} 

因此,这里将\begin{landscape}重新定义为\blandscape\end{landscape}\elandscape 。 在.Rmd文件中使用这些新定义的命令似乎工作:

 --- title: "Mixing portrait and landscape" output: pdf_document: includes: in_header: header.tex --- Portrait ```{r} summary(cars) ``` \newpage \blandscape Landscape ```{r} summary(cars) ``` \elandscape \newpage More portrait ```{r} summary(cars) ``` 

对于最常见的情况。

有3个条件。

  1. 一切都在肖像模式。
  2. 一切都在风景模式。
  3. 纵向和横向模式的混合。

让我们缩小到每个条件。

  1. 第一个,我们有一个降价文件,从下面的代码开始。 这是创buildrmd文件时Rstudio中的默认设置。 当你编织它。 它会毫无疑问地自动认为这是一个肖像模式。

     title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document 
  2. 当你想把PDF文件编织成横向模式时,你只需要添加classoption:landscape

      title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document classoption: landscape 
  3. 如果你想混合使用两者,你​​需要在YAML中添加.tex文件。 通过参考我上面提到的链接。 您可以在这里下载.tex代码。 http://goo.gl/cptOqg或者只是简单地复制代码并保存为header.tex然后,为了使生活更轻松,把这个;.tex文件和rmd文件一起编织。 确保你做了这两件事情:复制tex文件并将其与rmd文件一起移动。 将rmd的开头更改为:

      title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document: includes: in_header: header.tex 

这是我玩这个问题后的总结,大部分都受益于Baptiste的回答。

我在我的博主中包含了一些快照和示例。

希望这可以帮助。 祝你好运。

基于以前的解决scheme,以下解决scheme不需要辅助header.tex文件。 所有内容都包含在.Rmd文件中。 LaTeX命令是在YAML头部的header-includes块中定义的。 更多信息可以在这里find。

另外,我注意到使用lscape LaTeX包可以旋转页面的内容,而不是PDF页面本身。 这是使用pdflscape软件包解决的。

 --- title: "Mixing portrait and landscape WITHOUT a header.tex file" header-includes: - \usepackage{pdflscape} - \newcommand{\blandscape}{\begin{landscape}} - \newcommand{\elandscape}{\end{landscape}} output: pdf_document --- Portrait ```{r} summary(cars) ``` \newpage \blandscape Landscape ```{r} summary(cars) ``` \elandscape \newpage More portrait ```{r} summary(cars) ``` 

正如baptiste提到的,如果将R命令放在LaTeX环境中,pandoc将不会parsing它们,并将它们放入生成的LaTeX中:这是导致错误的原因。 除了baptiste的很好和简单的修复,你可以使用xtable R包,它提供了从R输出创build看起来更性感的LaTeX表的可能性。 对于下面的例子来说,你需要在header.tex文件中添加\usepackage{rotating}

 --- title: "Mixing portrait and landscape" output: pdf_document: keep_tex: true includes: in_header: header.tex --- ```{r, echo=FALSE} library(xtable) ``` Portrait ```{r, results='asis', echo=FALSE} print(xtable(summary(cars), caption="Landscape table"), comment=FALSE) ``` Landscape: ```{r, results='asis', echo=FALSE} print(xtable(summary(cars), caption="Landscape table"), floating.environment="sidewaystable", comment=FALSE) ``` 

第二个表格将打印在sidewaystable环境中,而不是通常的table :因此,它将以横向模式打印在一个单独的页面中。 请注意,通过lscape软件包或sideways环境以横向模式放置的表格和graphics将始终放置在单独的页面中,请参阅此非常重要的文档的第91页:

http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf

因为我觉得这有点烦人,所以我设法find了一个方法,将肖像和风景表保存在同一个页面中(浪费了整个下午的时间):

 --- title: "Mixing portrait and landscape" output: pdf_document: keep_tex: true includes: in_header: header.tex --- ```{r, echo=FALSE} library(xtable) ``` Portrait: ```{r, results='asis', echo=FALSE} print(xtable(summary(cars), caption="Portrait table."), comment=FALSE) ``` Landscape: ```{r, results='asis', echo=FALSE} cat(paste0( "\\begin{table}[ht]\\centering\\rotatebox{90}{", paste0(capture.output( print(xtable(summary(cars)), floating=FALSE, comment=FALSE)), collapse="\n"), "}\\caption{Landscape table.}\\end{table}")) ``` 

对于景观表,我使用了这里提供的\rotateboxbuild议:

http://en.wikibooks.org/wiki/LaTeX/Rotations

为了这个工作,我只需要生成tabular部分print(xtable(...部分,然后我必须捕获输出并用tablerotatebox命令“手动”包围它,将所有内容转换为一个stringR的输出,使pandoc不会看到他们作为LaTeX环境。对于一个纯粹的rmarkdown解决scheme…祝你好运!

Interesting Posts