【问题标题】:Rstudio rmarkdown: both portrait and landscape layout in a single PDFRstudio rmarkdown:单个PDF中的纵向和横向布局
【发布时间】:2014-11-09 02:00:59
【问题描述】:

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

这是一个可重现的小例子。首先,在 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

非常感谢任何帮助。

【问题讨论】:

    标签: r pdf rstudio knitr r-markdown


    【解决方案1】:

    所以,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
    ```{r}
    summary(cars)
    ```
    \elandscape
    
    \newpage
    More portrait
    ```{r}
    summary(cars)
    ```
    

    【讨论】:

    • 感谢您的研究和回答 (+1)。 Includes section 中的“不解析乳胶环境的内容”并不完全清楚。但我认为我的latex 无知也是罪魁祸首。
    • 这是一个复杂的工具链,有三/四个不同的参与者(knitr-rmarkdown/pandoc-latex),我发现除了记录在案的东西之外,很难弄清楚哪里出了问题。最好的方法似乎是独立运行它们:首先编织,查看生成的.md(很好,这里),然后是 md->tex 转换(这就是它出错的地方)。错误消息没有帮助,因为它已经是下一步(乳胶)。
    • 使用此解决方案,在 pdf 中,而不是由“#Introduction”创建的结构化标题,# 显示为符号
    • 我猜是愚蠢的乳胶问题:header.tex 文件在哪里(或应该)在哪里,以便读取它?我大量使用 RMarkdown,但相对较新,还没有理解所有的互锁包以及它们如何协同工作。
    • header.tex文件应该在同一目录下
    【解决方案2】:

    在以前的解决方案的基础上,以下解决方案不需要辅助header.tex 文件。所有内容都包含在.Rmd 文件中。 LaTeX 命令改为在 YAML 标头中的 header-includes 块中定义。更多信息可以找到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
    ```{r}
    summary(cars)
    ```
    \elandscape
    
    \newpage
    More portrait
    ```{r}
    summary(cars)
    ```
    

    【讨论】:

    • 在我的系统上,此解决方案不起作用。我在 Mac OS_10.13.4 上运行 R-3.4.4、rmarkdown_1.9、knitr_1.20。想知道可能是什么问题?
    • @GeochemB 是否正确安装了必要的 LaTeX 包?我最近在TinyTeX 上取得了成功并推荐它。
    • 我安装它们时没有抛出错误,但我会仔细检查并报告。感谢您的提醒,我没有想到这一点,并且是输出到 PDF/Latex 的新手。
    • @Megatron 我浏览了 Tex Live 实用程序并安装和更新了 Oberdiek 软件包。所以要求在那里,但仍然没有骰子。即使我复制/粘贴上面的代码,方向也没有变化。
    • @GeochemB 我使用这段代码遇到了同样的问题。我尝试在 SumatraPDF v3.1.1 和 Adob​​e Acrobat DC 和 Pro 中查看文档。从文档 Oberdeik 包含在 MikTex 中。根据我的理解,如果我安装了 MikTex,pdflscape 应该很好。好奇是否有人有解决方案。
    【解决方案3】:

    对于最常见的情况。

    有 3 个条件。

    1. 纵向模式下的一切。
    2. 横向模式下的一切。
    3. 纵向和横向模式的混合。

    让我们缩小到每个条件。

    1. 第一个,假设我们有一个以以下代码开头的降价文档。这是创建 rmd 文件时 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
      

    这是我玩过这个问题后的总结,主要受益于巴蒂斯特的回答。

    我在我的博主my blogger 中加入了一些快照和示例。

    希望这会有所帮助。 祝你好运。

    【讨论】:

    • 你的方法有效。为了更容易理解,我认为 pandoc 的问题是当你使用环境而不是宏时它会变得很奇怪。这就是为什么我按照您的建议在我的标题(pdflandscape 包)中包含 \newcommand{\blandscape}{\begin{landscape}} 和 \newcommand{\elandscape}{\end{landscape}},并且效果很好。谢谢!
    • 我按照上面的这些步骤(查看博客有帮助),效果很好。谢谢!
    【解决方案4】:

    正如 baptiste 提到的,如果您在 LaTeX 环境中包含 R 命令,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
    ```{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 环境中以横向模式放置的表格和图形将始终放置在单独的页面中,请参阅此非常重要的文档的第 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}"))
    ```
    

    对于横向表,我使用了此处提供的\rotatebox 建议:

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

    为此,我必须只使用print(xtable(... 部分生成表的tabular 部分,然后我必须捕获输出并“手动”用tablerotatebox 将其包围命令,将所有内容转换为字符串 R 输出,以便 pandoc 不会将它们视为 LaTeX 环境。对于纯粹的 rmarkdown 解决方案...祝你好运!

    【讨论】:

    • 这是此页面上唯一对我有用的。谢谢,雷纳托!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多