Rstudio rmarkdown:单个PDF中的纵向和横向布局

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)

  • 这是一个复杂的工具链,有三个/四个不同的玩家(knitr-rmarkdown/pandoc-latex),我发现在记录的东西之外,很难弄清楚事情在哪里破裂.最好的方法似乎是独立运行它们:首先编织,看看生成的`.md`(精细,这里),然后是md-> tex转换(那就是它出错的地方).错误消息没有帮助,因为它已经是下一步(乳胶). (4认同)
  • 我想dumb latex问题:header.tex文件在哪里(或应该)存在,以便它被读取?我大量使用RMarkdown,但它相对较新,并且尚未了解所有互锁包以及它们如何协同工作. (3认同)

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)

  • 这是一个很棒的解决方案! (4认同)
  • 这很有效,但页码打印在横向页面的左侧。有没有一种简单的方法可以处理这个问题并且仍然有页码? (2认同)

Jun*_*ang 18

对于最常见的情况.

有3个条件.

  1. 一切都处于纵向模式.
  2. 景观模式中的一切.
  3. 纵向和横向模式的混合.

让我们缩小到每个条件.

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

    title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output: pdf_document
    
    Run Code Online (Sandbox Code Playgroud)
  2. 当您想要将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)
  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
    
    Run Code Online (Sandbox Code Playgroud)

这是我玩这个问题后的总结,大部分都受益于巴普蒂斯特的回答.

我在我的博客博客中加入了一些快照和示例.

希望这可以帮助.祝好运.

  • 你的方法有效。为了更容易理解,我认为 pandoc 的问题在于,当您使用环境而不是宏时,它会变得很奇怪。这就是为什么我按照您的建议将 \newcommand{\blandscape}{\begin{landscape}} 和 \newcommand{\elandscape}{\end{landscape}} 包含在我的标题(pdflandscape 包)中,并且效果很好。谢谢! (2认同)

ren*_*olo 7

正如 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 解决方案......祝你好运!