首页 文章

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

提问于
浏览
58

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

这是一个小的,可重复的例子 . 首先,在RStudio中渲染此 .Rmd (按 Knit PDF 按钮)会生成包含横向布局中所有页面的pdf:

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

```{r}
summary(cars)

\newpage

summary(cars)

然后尝试创建一个混合纵向和横向布局的文档 .   `YAML` 中的基本设置是根据'Includes'部分[here](http://rmarkdown.rstudio.com/pdf_document_format.html)完成的 .   `in_header` 文件'header.tex'仅包含 `\usepackage{lscape}` ,建议用于 `knitr` 横向布局[here](https://github.com/yihui/knitr-examples/blob/master/030-landscape.Rnw)的包 .   `.tex` 文件与 `.Rmd` 文件位于同一目录中 . 

```java
---
title: "Mixing portrait and landscape"
output:
    pdf_document:
        includes:
            in_header: header.tex
---

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

\newpage
\begin

Landscape:

summary(cars)

\end

\newpage
More portrait:

summary(cars)

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

```java
# ! 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

任何帮助深表感谢 .

4 回答

  • 55

    所以, pandoc does not解析了乳胶环境的内容,但你可以通过 header.tex 文件中的redefining the commands来欺骗它:

    \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

    summary(cars)
    

    \elandscape

    \newpage
    More portrait

    summary(cars)
    
  • 26

    基于以前的解决方案,以下解决方案不需要辅助 header.tex 文件 . 所有内容都包含在 .Rmd 文件中 . 而是在YAML标头的 header-includes 块中定义LaTeX命令 . 更多信息可以在here找到 .

    另外,我注意到使用 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

    summary(cars)
    

    \elandscape

    \newpage
    More portrait

    summary(cars)
    
  • 1

    对于最常见的情况 .

    有3个条件 .

    • 纵向模式下的所有内容 .

    • 横向模式中的所有内容 .

    • 纵向和横向模式的混合 .

    让我们缩小到每个条件 .

    • 第一个,比如说我们有一个降价文件,下面是代码 . 这是创建rmd文件时Rstudio中的默认设置 . 当你编织它 . 毫无疑问,它会自动假设它是一个肖像模式 .
    title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output: pdf_document
    
    • 当您想要将PDF文件编织为横向模式时,您唯一需要添加的是classoption:landscape
    title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output: pdf_document
        classoption: landscape
    
    • 如果您想要两者混合,则需要在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
    

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

    我在博客my blogger中包含了一些快照和示例 .

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

  • 15

    正如baptiste所提到的,如果你将R命令包含在LaTeX环境中,pandoc将不会解析它们并将它们放入生成的LaTeX中:这就是导致错误的原因 . 除了baptiste的简单修复之外,您还可以使用 xtable R包,它可以从R输出创建更性感的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

    print(xtable(summary(cars), caption="Landscape table"), comment=FALSE)
    

    Landscape:

    print(xtable(summary(cars), caption="Landscape table"),
          floating.environment="sidewaystable", comment=FALSE)
    
    
    第二个表格将在 `sidewaystable` 环境中打印,而不是通常的 `table` :因此它将以横向模式打印在单独的页面中 . 请注意, `lscape` 包或 `sideways` 环境中以横向模式放置的表格和图形将始终放在单独的页面中,请参阅此非常重要的文档的第91页:
    
    [http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf](http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf)
    
    由于我觉得这有点烦人,我设法找到一种方法将肖像和风景表保持在同一页面中(在整个过程中浪费了整个下午):
    
    ```java
    ---
    title: "Mixing portrait and landscape"
    output:
        pdf_document:
            keep_tex: true
            includes:
                in_header: header.tex
    ---
    ```{r, echo=FALSE}
    library(xtable)
    

    Portrait:

    print(xtable(summary(cars), caption="Portrait table."), comment=FALSE)
    

    Landscape:

    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}"))
    
    
    对于横向表,我使用了此处提供的 `\rotatebox` 建议:
    
    [http://en.wikibooks.org/wiki/LaTeX/Rotations](http://en.wikibooks.org/wiki/LaTeX/Rotations)
    
    为了实现这一点,我必须仅使用 `print(xtable(...` 部分生成表的 `tabular` 部分,然后我必须捕获输出并使用 `table` 和 `rotatebox` 命令将其环绕,将所有内容转换为字符串R输出,以便pandoc执行此操作不要把它们视为LaTeX环境 . 对于纯粹的rmarkdown解决方案......祝你好运!

相关问题