Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown setting the position of kable

I have the following issue, once knitting the Rmarkdown in Rstudio to PDF, my tables appear not in the position where they are in Rmarkdown file, but on the top of the page. I tried to add:

header-includes:
  - \usepackage{float}

and

```{r setup, include=FALSE}
knitr::opts_chunk$set(... fig.pos = "H")
```

But it didn't work. R and Rstudio run on Linux, the LaTeX engine is "pdflatex"

Fully reproducible example:

---
title: "Untitled"
output: pdf_document
header-includes:
  - \usepackage{float}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning = FALSE, fig.align = "center", dev = "cairo_pdf", fig.pos = "H")
```

```{r}
library(kableExtra)
library(tidyverse)
```
## 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>.

\newpage

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

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance")
```
like image 629
Justas Mundeikis Avatar asked Nov 05 '18 11:11

Justas Mundeikis


1 Answers

You can replace "hold_position" from claudius answer with "HOLD_position":

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
  kable_styling(latex_options = "HOLD_position")
```

As mentionned in the kableExtra package:

if you find hold_position is not powerful enough to literally PIN your table in the exact position, you may want to use HOLD_position, which is a more powerful version of this feature. For those who are familiar with TeX, hold_position uses[!h] and HOLD_position uses [H] and the float package.

ref: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

You may also want to control figure position by adding fig.pos='H' to the figure chunk header.

like image 138
GaelS Avatar answered Nov 13 '22 14:11

GaelS