Search code examples
rpdfr-markdownlandscape

How to rotate a single page to landscape in R Markdown pdf output


I'm new to R. Any help would be greatly appreciated.

I would like to rotate a single page to landscape in R markdown (pdf).

In the YAML header I have added the following (based on a previous post):

header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document

and then:

\blandscape

where I wish to change to landscape (is it correct that this should not be added to the code chunk?).

However I get an error message:

! LaTeX Error: \begin{landscape} on input line 193 ended by \end{document}.

This is probably a stupid question but do I need to download this package pdflscape? Or what am I doing wrong?

Thanks!


Solution

  • You should also end your landscape page with \elandscape like this:

    ---
    header-includes:
    - \usepackage{pdflscape}
    - \newcommand{\blandscape}{\begin{landscape}}
    - \newcommand{\elandscape}{\end{landscape}}
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r cars}
    summary(cars)
    ```
    
    \blandscape
    
    ## Including Plots
    
    You can also embed plots, for example:
    
    ```{r pressure, echo=FALSE}
    plot(pressure)
    ```
    
    Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
    
    \elandscape
    

    Output:

    enter image description here