1

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

Joe Crozier
  • 944
  • 8
  • 20

1 Answers1

1

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

manro
  • 3,529
  • 2
  • 9
  • 22
  • I'm sure the problem is on my end, but right now when I try that it fails while knitting and I get the error: "undefined control sequence \captionof {table}{Table, stay here!}" – Joe Crozier Nov 23 '21 at 19:57
  • 1
    Oops, probably had something to do with me not remembering to do the "include package caption" thing at the top. – Joe Crozier Nov 23 '21 at 20:01
  • @JoeCrozier Yes ;) Now is all right? – manro Nov 23 '21 at 20:10
  • Yup! Works great – Joe Crozier Nov 23 '21 at 20:19
  • 1
    @JoeCrozier And I should warn, make a caption with ```\captionof{}```. With ```kbl(caption = " ")``` and this "construction" you will be received an error. Good luck ;) – manro Nov 23 '21 at 20:23