Rstudio rmarkdown : both portrait and landscape layout in a single PDF

标签 r pdf rstudio knitr r-markdown

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

这是一个可重现的小示例。首先,在 RStudio 中渲染此 .Rmd(按 Knit PDF 按钮)会生成所有页面均采用横向布局的 pdf:

---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---

```{r}
summary(cars)
```

\newpage
```{r}
summary(cars)
```

然后尝试创建一个混合纵向和横向布局的文档。 YAML 中的基本设置是根据“包含”部分 here 完成的。 。 in_header 文件“header.tex”仅包含 \usepackage{lscape},这是建议用于 knitr 横向布局的包 here.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)
```

但是,此代码会导致错误:

# ! 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 does not解析 latex 环境的内容,但你可以通过 redefining the commands 来欺骗它在您的 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)
```

关于Rstudio rmarkdown : both portrait and landscape layout in a single PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849814/

相关文章:

r - 使用 ggsave 可见 pdf 零大小元素

css - 将 PDF 图标应用于链接 - 缓存更改扩展

r - 在 RStudio/Interactive R session 中出错时停止执行

java - R/RStudio、Yosemite 和 Java

r - data.table::fread 中的 dec 参数

r - 如何在 R Bookdown 中渲染实际的 a6 纸张尺寸?

javascript - 如何在 R 或 Javascript 中使用 uvCharts 隐藏图例

r - If 语句基于数据帧中的另一列 : in R

java - 使用 iText 4.2.1 将 RTF 转换为 PDF 时遇到 NullPointerException

r - R更新: unable to access index for repository: internet routines cannot be loaded后无法安装软件包