Joe Crozier
Joe Crozier

Reputation: 1036

R Markdown, kable_styling(latex_options="HOLD_position") not working

I have a problem that I've seen several times on here, but the usual solutions aren't working. I can't share all of my code due to some PHI concerns, but I'll try to share as much as I can.

The problem

My kable tables are showing up below the respective headers.

Example code: enter image description here

Example output: enter image description here

What I've tried

So there are already a few answers on here about similar problems, like here and here and it seems like most of them have you add some sort of

%>%kable_styling(latex_options = "HOLD_position")

snippet at the end of each bit of code. Problem is, as you can see I did that and it didn't work. I also have tried it with/without

knitr::opts_chunk$set(echo = TRUE, fig.pos = "H")

up at the very top of the document. Still no dice.

One more thing that is weird is that there were a couple tables lower in the document which DID go into the right spot when I added the bit about hold_position. Not sure why those worked and the others didn't. Am I missing something?

I did see in this question, one of the comments mentioned that deleting the caption helped so I tried that (because the tables that worked in mine didn't have captions, so I figured maybe that was it).... whether I left the captions or not it stayed put.

One other thing I noticed was that some of the tables that worked had text/photos in those sections prior to the table, like so: enter image description here

So when I added some random text prior to the tables that weren't showing up in the right spot, they did show up correctly. (Pictured below) Problem is, I don't want to have to add text there. enter image description here enter image description here

Upvotes: 1

Views: 1481

Answers (1)

manro
manro

Reputation: 3677

Ok, let's solve it with the pure LaTeX ;)

---
title: "Test Table"
date: '2021-11-11'

header-includes: 
- \usepackage{float}
- \usepackage{caption}

output:
  pdf_document
---

```{r}
library(kableExtra)
tab1<- head(mtcars) %>% kbl()
```

\begin{table}[H]
\captionof{table}{Table, stay here!}
`r tab1`
\end{table}

enter image description here

Upvotes: 1

Related Questions