26

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")
```
2
  • 1
    It seems like the figure environment is only added when you defined a caption using fig.cap. Then the positioning parameter should be used as expected. Nov 5, 2018 at 11:56
  • The solution was, deleting the caption="..." in kable (), once deleted, the positioning began to work. Nov 5, 2018 at 12:27

3 Answers 3

37

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.

1
  • 2
    Yes, it was crucial to fig.pos='H' AND latex_options = "HOLD_position" at the same time. Thanks a lot! Oct 9, 2021 at 9:45
11

To fix the table position in your latex document, you don't need include header options nor set opts_chunk configs. To do that you should specify the latex_options as HOLD_position by adding the kable_styling() function in the kable [1].

So the last chunk will be:

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
  kable_styling(latex_options = "hold_position")
```
1
  • I tried to do this, but all the tables go to the end of my document. Is there another solution or method to hold the tables at the same location
    – Mr.M
    Feb 14, 2020 at 0:29
1

An alternative approach for setting the position of a kable object for R Markdown Latex or Beamer output would be to include the table in a "textblock" environment, as posted here.

The YAML would include:

header-includes:
- \usepackage[absolute,overlay]{textpos}
  \setlength{\TPHorizModule}{1mm}
  \setlength{\TPVertModule}{1mm}

The R code chunk (preceded and followed by the latex code for the "textblock") would look like:

\begin{textblock}{}(21.5, 51.5)
\footnotesize
```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") 
```
\end{textblock} 

This approach allows for a more fine tuned positioning of the kable output on the page, or beamer slide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.