Rstudio rmarkdown: both portrait and landscape layout in one PDF file

I wonder how to use rmarkdown to create a pdf file that has both portrait and landscape layouts in the same document. If there is a clean rmarkdown option, that would be even better than using latex.

Here is a small, reproducible example. Firstly, rendering this .Rmd in RStudio (click the Knit PDF button) leads to the creation of a PDF with all the pages in the landscape layout:

 --- title: "All pages landscape" output: pdf_document classoption: landscape --- ```{r} summary(cars) ``` \newpage ```{r} summary(cars) ``` 

Then try to create a document that mixes portrait and landscape layouts. The basic installation in YAML is performed according to the "Includes" section here . The in_header file 'header.tex' contains only \usepackage{lscape} , the package suggested for the knitr landscape layout here . The .tex file is in the same directory as the .Rmd file.

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

However, this code leads to an error:

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

Any help is greatly appreciated.

+48
r pdf rstudio knitr r-markdown
Sep 15 '14 at 13:58 on
source share
4 answers

So pandoc does not parse the contents of latex media, but you can trick it by overriding the commands in your header.tex file:

 \usepackage{lscape} \newcommand{\blandscape}{\begin{landscape}} \newcommand{\elandscape}{\end{landscape}} 

So here \begin{landscape} redefined to \blandscape and \end{landscape} to \elandscape . Using this new command in a .Rmd file seems to work:

 --- 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) ``` 
+44
Dec 06 '14 at 17:16
source share

For the most common cases.

There are 3 conditions.

  • All in portrait mode.
  • All in landscape mode.
  • A mixture of portrait and landscape modes.

Shortens to each condition.

  • First, let's say we have a markdown document starting with the code below. And this is the default value in Rstudio when creating the rmd file. When you knit it. He will automatically consider his portrait mode without a doubt.

     title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document 
  • When you want to knit a PDF file in landscape mode, you only need to add a class: landscape

      title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document classoption: landscape 
  • If you need a mixture of both, you will need to add the .tex file to YAML. By listing the link mentioned above. You can download the .tex code here. http://goo.gl/cptOqg Or just copy the code and save it as header.tex. Then, to make life easier, put this .tex file along with the rmd file you want to knit. Make sure that you have completed the following two tasks: Copy the tex file and move it along with the rmd file. Change the start of rmd as follows:

      title: "Landscape and Portrait" author: "Jung-Han Wang" date: "Thursday, March 19, 2015" output: pdf_document: includes: in_header: header.tex 

This is a summary after I played with this issue and mainly benefited from the Baptist's answer.

I have included some snapshots and examples in my blogger as my blogger .

Hope this helps. Good luck.

+13
Mar 20 '15 at 13:21
source share

Based on previous solutions, the following solution does not require an auxiliary header.tex file. All content is contained in a .Rmd file. Instead, LaTeX commands are defined in the header-includes block in the YAML header. More information can be found here .

In addition, I noticed that using the lscape LaTeX package lscape contents of the page, but not the PDF page itself. This is resolved using the pdflscape package.

 --- 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) ``` 
+12
Jan 30 '17 at 21:10
source share

As mentioned in baptiste, if you insert R commands in LaTeX, pandoc will not parse them and place them as they were in the generated LaTeX: this causes an error. In addition to the Baptist with a good and simple fix, you can use the xtable R package, which offers the possibility of creating more sexier LaTeX tables from the output of R. For the following example, you need to add \usepackage{rotating} to 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 in a sidewaystable environment, and not in a regular table : therefore, it will be printed in landscape mode on a separate page. Please note that tables and figures placed in landscape mode with the lscape package or in the sideways environment will always be placed on 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 save both portrait and landscape tables on one page (losing all day 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 terrain table, I used the \rotatebox given here:

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

To do this, I only need to create part of the tabular table with the print(xtable(... , then I have to capture the output and manually surround it with the table and rotatebox commands, converting everything to the output of the R string so that pandoc does not treat them as LaTeX environment For a clean rmarkdown solution ... good luck!

+1
Dec 07 '14 at 16:26
source share



All Articles