How do I specify [H] for HOLD_position?

I am trying to get a table in an Rmarkdown pdf to stay in the right place. I used kableExtra to make the table and it looks like this:

kable(site_info, "latex", caption = "Site Information", booktabs = T, align = "c") %>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle")%>%
kable_styling(latex_options = "hold_position")   

It doesn't place the table where I want it, so I want to try the stronger option in the kableExtra documentation:

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 LaTeX, hold_position uses [!h] and HOLD_position uses [H] and the float package.

I added the float package in my YAML, but how do I use the [H]?

For figures, I could use the knitr option fig.pos = "H", but this didn't work for the table.


Here is some example reproducible code of the problem. The table ends up at the top, just like in my real document:

---
title: "Put things in order"
author: "Ranae N. Dietzel"
header-includes:
  - \usepackage{float}
output:
  pdf_document: default
fig_caption: yes
---
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)

library(tidyverse)
library(kableExtra)

ggplot(diamonds, aes(x=cut, y=price))+
  geom_boxplot()
diamonds_summary<-diamonds%>%
  group_by(cut)%>%
  summarise(mean = mean(price))

kable(diamonds_summary, "latex", caption = "Price Summary", booktabs = T, align = "c") %>%
  collapse_rows(columns = 1, latex_hline = "major", valign = "middle")%>%
  kable_styling(latex_options = "HOLD_position")


ggplot(diamonds, aes(x=carat, y=price))+
  geom_point()
1 Like

If you've added the float package, then the following should work:

kable(site_info, "latex", caption = "Site Information", booktabs = T, align = "c") %>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle")%>%
kable_styling(latex_options = "HOLD_position")   # Change will be here

Thanks! But it doesn't work. I feel like there must be one more step I am missing.

I ran your example

---
title: "Put things in order"
author: "JDB"
header-includes:
  - \usepackage{float}
output:
  pdf_document: default
fig_caption: yes
---

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

library(dplyr)
library(ggplot2)
library(kableExtra)
```

```{r}
ggplot(diamonds, aes(x=cut, y=price))+
  geom_boxplot()
```


```{r}
diamonds_summary<-diamonds%>%
  group_by(cut)%>%
  summarise(mean = mean(price))

kable(diamonds_summary, "latex", caption = "Price Summary", booktabs = T, align = "c") %>%
  collapse_rows(columns = 1, latex_hline = "major", valign = "middle")%>%
  kable_styling(latex_options = "HOLD_position")
```

```{r}
sessioninfo::session_info()
```

which produced this file document.pdf (234.9 KB)

The table seems to hold its position. Is this what you'd like to see?

At least the table is not at top! Yes, it looks better.

But I am wondering why you are getting echos and messages if they are set to FALSE? Also missing the figure caption and third plot.

Mine looks a lot different: ordering_example.pdf (1.6 MB)

So I think what's going on is that the figures are allowed to float, and so they can get pushed to different pages. (When I rendered your sample, it looked the same as @Ranae's with the table on the first page, and the figures on the remaining pages.)

I found this link with description of how to set the figures to fix in place -- you may have to play around with the chunk options to fix only the figures that you want:

1 Like

Does the rmarkdown, knitr, and kableExtra version in my document match yours, @Ranae?

Thank you!!! It is working now.

It seems that when I used the fig.cap = "caption" option, this kept my figures from floating, but the table still floated and ended up in random places. I added one line to my YAML, as suggested by the second answerer and then the table ended up between figures. My final YAML was:

---
title: "Arising From Supplementary Materials"
author: "Ranae N. Dietzel"
header-includes:
  - \usepackage{float}
  - \floatplacement{figure}{H}  #make every figure with caption = h, this was the fix
output:
  pdf_document: default
fig_caption: yes
---

Cool - glad it worked out! That's definitely a better way than my backup work-around, which was to poke at the intermediate .tex file. :smiley:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.