use*_*688 67 pdf r rstudio knitr r-markdown
我想知道如何使用rmarkdown生成在同一文档中同时具有纵向和横向布局的pdf.如果有一个纯粹的rmarkdown选择,甚至比使用乳胶更好.
这是一个小的,可重复的例子.首先,.Rmd在RStudio中渲染它(按Knit PDF按钮)会生成包含横向布局中所有页面的pdf:
---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---
```{r}
summary(cars)
```
\newpage
```{r}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)
然后尝试创建一个混合纵向和横向布局的文档.其中的基本设置YAML是根据此处的"包含"部分完成的.该in_header文件"header.tex"只包含\usepackage{lscape},包建议的knitr景观布置在这里.该.tex文件与文件位于同一目录中.Rmd.
---
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)
```
Run Code Online (Sandbox Code Playgroud)
但是,此代码会导致错误:
# ! 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
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.
bap*_*ste 67
所以,pandoc 不解析乳胶环境的内容,但你可以欺骗它重新定义的命令在你的header.tex文件:
\usepackage{lscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}
Run Code Online (Sandbox Code Playgroud)
因此,这里\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)
```
Run Code Online (Sandbox Code Playgroud)
Meg*_*ron 37
基于以前的解决方案,以下解决方案不需要辅助header.tex文件.所有内容都包含在.Rmd文件中.而是header-includes在YAML标头的块中定义LaTeX命令.更多信息可以在这里找到.
此外,我注意到使用lscapeLaTeX包旋转页面的内容,但不旋转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)
```
Run Code Online (Sandbox Code Playgroud)
Jun*_*ang 18
对于最常见的情况.
有3个条件.
让我们缩小到每个条件.
第一个,比如我们有一个降价文档,从下面的代码开始.这是创建rmd文件时Rstudio中的默认设置.当你编织它.毫无疑问,它会自动假设它是一个肖像模式.
title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output: pdf_document
Run Code Online (Sandbox Code Playgroud)当您想要将PDF文件编织为横向模式时,您唯一需要添加的是classoption:landscape
title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output: pdf_document
classoption: landscape
Run Code Online (Sandbox Code Playgroud)如果你想要两者混合,你需要在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
Run Code Online (Sandbox Code Playgroud)这是我玩这个问题后的总结,大部分都受益于巴普蒂斯特的回答.
我在我的博客博客中加入了一些快照和示例.
希望这可以帮助.祝好运.
正如 baptiste 提到的,如果您将 R 命令包含在 LaTeX 环境中,pandoc 将不会解析它们,而是将它们按原样放入生成的 LaTeX 中:这就是导致错误的原因。除了 baptiste 的简单易用的修复之外,您还可以使用xtableR 包,它提供了从 R 输出创建看起来更性感的 LaTeX 表格的可能性。为了使以下示例正常工作,您需要\usepackage{rotating}在header.tex文件中添加:
---
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)
```
Run Code Online (Sandbox Code Playgroud)
第二个表将在环境中打印sidewaystable,而不是通常的table:因此它将以横向模式打印在单独的页面中。lscape请注意,由包或环境以横向模式放置的表格和图形sideways将始终放置在单独的页面中,请参阅此非常重要的文档的第 91 页:
http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf
因为我觉得这有点烦人,所以我设法找到一种方法将纵向和横向表格保留在同一页面中(在此过程中浪费了我整个下午):
---
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}"))
```
Run Code Online (Sandbox Code Playgroud)
对于景观表,我使用了\rotatebox此处提供的建议:
http://en.wikibooks.org/wiki/LaTeX/Rotations
为此,我必须只生成tabular表的一部分print(xtable(...,然后我必须捕获输出并用tableandrotatebox命令“手动”包围它,将所有内容转换为字符串 R 输出,以便 pandoc 看不到它们作为 LaTeX 环境。对于纯粹的 rmarkdown 解决方案......祝你好运!