Prevent pagebreak in kableExtra landscape table

偶尔善良 提交于 2021-01-26 17:31:58

问题


How can a landscape table be plotted in R Markdown (PDF output) without causing a page break to be inserted?

There is the function landscape from the kableExtra package, but this forces a page break to be inserted.

Example:

The normal behaviour for tables in R Markdown is that the will float to minimise the breaking up of text.

---
output: pdf_document
---

Some Text

```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", caption = "A table")
```

More Text

Landscape:

---
output: pdf_document
---

Some Text

```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", booktabs = T, caption = "A table") %>%
  landscape()
```

More Text


回答1:


You can use the LaTeX package realboxes to do what you want

---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
  - \usepackage[graphicx]{realboxes}
  - \usepackage{booktabs}
---

Some text

\Rotatebox{90}{
```{r, echo=FALSE, warning=FALSE}

knitr::kable(mtcars, "latex", booktabs = TRUE)
```
}
More text

This produces a single page pdf with the table presented in landscape. The problem with this approach is that it does not seem to work with a caption.

Edit You can use the caption latex package to add the caption

---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
  - \usepackage[graphicx]{realboxes}
  - \usepackage{booktabs}
  - \usepackage{caption}
---

Some text


\begingroup 
\captionsetup{type=table}
\caption{A table}
\Rotatebox{90}{

```{r, echo=FALSE, warning=FALSE}

knitr::kable(mtcars, "latex", booktabs = TRUE)
```

}
\endgroup

More text

This produces the table in landscape with caption



来源:https://stackoverflow.com/questions/51633434/prevent-pagebreak-in-kableextra-landscape-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!