Landscape table with flextable in pdf and word output

126 views Asked by At

I am knitting a document from rmarkdown to pdf and .docx using bookdown. I have tables that I would like to put on landscape pages. I have made the tables with flextable because I need versions of the document in word for my supervisors, but ultimately, want to knit to a pdf, as some of my other sections have kable items and other features that I don't want to change. For kable tables I can just use kableExtra::landscape(). Is there a simple version with flextable?

At the moment I am putting \landscape and \endlandscape either side of my code chunk for all the tables that need to be landscape, to produce the pdf, but this isn't ideal.

MWE:

header-includes:
- \usepackage{pdflscape}
output:
  bookdown::pdf_document2:
    number_sections: false
    latex_engine: xelatex
  bookdown::word_document2: 
    number_sections: false
always_allow_html: true
geometry: "left=2.5cm,right=2cm,top=2cm,bottom=2cm"
fontsize: 12pt
linestretch: 1.5
---
```{r setup, include=FALSE}
  library(knitr)
  knitr::opts_chunk$set(echo = FALSE, warning = F, message = F)
   library(dplyr)
  library(flextable)
   library(ftExtra)
```
Some text on portrait page

\landscape
```{r table}
flextable(mtcars)%>%
autofit()%>%
theme_booktabs(bold_header = T)%>%
set_caption("Table caption")%>%
  fontsize(size = 11, part =  "all")
```

\endlandscape

 More text, next line back to portrait

I am rendering with commands:

bookdown::render_book(".", "bookdown::word_document2", config_file = "_bookdown.yml", preview = F, clean = T)

bookdown::render_book(".", "bookdown::pdf_document2", config_file = "_bookdown.yml", preview = F, clean = T)

0

There are 0 answers