How to print PDF tables created with kable in landscape layout with a caption?

1.1k Views Asked by At

Context: I am knitting an Rmarkdown report mixing landscape and portrait layouts. I use the technique described here: https://stackoverflow.com/a/41945462/10264278.

Problem: there is an unexpected table layout problem. when the table is made using kableExtra::kable() it does not follow the expected orientationif a caption is filled in kable(caption =...).

Reproducible example:

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

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, comment=FALSE, message=FALSE)

library(dplyr)
library(kableExtra)
```

\blandscape
```{r}
mtcars # displays properly

mtcars %>% kable(caption = "test", digits = 2) # the table follows the landscape layout and does not remains "still"/"horizontal"
```
\elandscape

The output:

Table on the left (mtcars) is displayed as expected. But, if I use kable() the table "turns with the page" witch is not expected.

enter image description here

EDIT:

I am aware that kableExtra::landscape() exists. But this function creates a new page for each table. Here I have many few-rows tables that are very wide. Thus I do not want to have a single table per page.

Example:

```{r}
head(mtcars, 5) %>% kable(caption = "test", digits = 2) %>% landscape(margin = NULL)
head(mtcars, 5) %>% kable(caption = "test", digits = 2) %>% landscape(margin = NULL)

```

Output: enter code here

EDIT 2:

This problem occurs only if a caption is specified in kable(caption = ...).

Example:

\blandscape
```{r}
mtcars # displays properly

mtcars %>% kable(digits = 2) # the table follows the landscape layout and does not remains "still"/"horizontal"
```
\elandscape

Output: enter image description here

0

There are 0 best solutions below