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

后端 未结 4 1558
孤城傲影
孤城傲影 2020-11-27 10:26

I wonder how to use rmarkdown to generate a pdf which has both portrait and landscape layout in the same document. If there is a pure rmarkdown opt

4条回答
  •  时光说笑
    2020-11-27 10:43

    As baptiste mentioned, if you enclose R commands within a LaTeX environment, pandoc will not parse them and will place them as they are into the generated LaTeX: this is what causes the error. Beyond baptiste's nice and simple fix, you could use the xtable R package, which offers the possibility of creating sexier-looking LaTeX tables from R output. For the following example to work, you need to add \usepackage{rotating} in the header.tex file:

    ---
    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)
    ```
    

    The second table will be printed within the sidewaystable environment, rather than the usual table: therefore it will be printed in landscape mode, in a separate page. Note that tables and figures which are placed in landscape mode by the lscape package or in the sideways environment will always be placed in a separate page, see page 91 of this very important document:

    http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf

    Since I find this a bit annoying, I managed to find a way to keep both portrait and landscape tables within the same page (wasting my whole afternoon in the process):

    ---
    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}"))
    ```
    

    For the landscape table, I used the \rotatebox suggestion provided here:

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

    For this to work, I have to only generate the tabular part of the table with the print(xtable(... part, then I have to capture the output and "manually" surround it with the table and rotatebox commands, converting everything into a string R output so that pandoc does not see them as LaTeX environments. For a pure rmarkdown solution... good luck!

提交回复
热议问题