I had Quarto around v1.2 and I just updated it to v1.6.4.
Before updating I had a PDF document which gave me no hardships with regards to placement of the tables.
My minimal working example is here:
---
title: "kableExtra tables"
subtitle: "Not behaving as expected anymore"
format:
pdf:
fig-pos: H
include-in-header:
text: |
\addtokomafont{disposition}{\rmfamily}
number-sections: true
execute:
echo: false
fig-cap-location: top
toc: false
---
```{=latex}
\vspace{-3.4cm}
\tableofcontents{}
```
```{r}
#| include: false
## Loading packages and data
library(tidyverse)
library(kableExtra)
data <- mtcars %>%
rownames_to_column(var="carnames")
```
## First section
```{r}
#| include: false
## Preparing the tables
data1.1 <- data %>% head(4) %>% select(carnames, mpg)
data1.2 <- data %>% head(4) %>% select(cyl, hp)
tbl1.1 <- data1.1 %>%
kbl(booktabs = T, digits = 2, format.args = list(big.mark = ".", decimal.mark = ","),
linesep = "\\addlinespace") %>%
kable_styling(latex_options = c("striped", "full_width"),
position = "center") %>%
row_spec(nrow(data1.1), bold = T, hline_after = T) %>%
add_header_above(c(" " = 1, "test 1" = 1))
tbl1.2 <- data1.2 %>%
kbl(booktabs = T, digits = 0, format.args = list(big.mark = ".", decimal.mark = ","),
linesep = "\\addlinespace") %>%
kable_styling(latex_options = c("striped", "full_width"),
position = "center") %>%
row_spec(nrow(data1.2), bold = T, hline_after = T) %>%
add_header_above(c("test 2" = 1))
```
Tables next to each other.
```{r}
#| label: tbl-example1
#| tbl-cap: "A very exciting caption"
#| tbl-subcap:
#| - "mpg of certain cars."
#| - "cylender sizes and horse power, I guess."
#| layout-ncol: 2
tbl1.1
tbl1.2
```
## Second section
```{r}
#| include: false
## Preparing the tables
data2 <- data %>% head(15) %>%
bind_rows(summarise(.,
across(where(is.numeric), sum),
across(where(is.character), ~ "Total")))
```
```{r}
#| label: tbl-mtcars
#| tbl-cap: "Full view of mtcars data set."
data2 %>%
kbl(booktabs = T, digits = 0, format.args = list(big.mark = ".", decimal.mark = ","),
linesep = "\\addlinespace") %>%
kable_styling(latex_options = c("striped", "full_width"),
position = "center") %>%
row_spec(nrow(data2), bold = T, hline_after = T) %>%
add_header_above(c(" " = 1, "details" = 7, "interesting" = 4))
```
It gives me the output in the screenshot below.
Prior to updating I had furthermore specified latex_options = c("striped", "full_width", "HOLD_position")
in the of kable_styling
. But the "HOLD_position"
did not do anything to the table positions in v1.6.4 (same positioning as in the screenshot) but add annoying [H] "tags" to each table. Therefore I removed it now. It did help with positioning in the earlier version of Quarto which I used before.
Now it freely chooses where to place the tables rather than follow the flow of the text in the .qmd file. I tried putting tbl-pos: H
in the YAML header to fix it but that had no impact.
What can I do to either individually or globally (like I did for figures in the YAML header) position the tables to follow the order of my text? The wanted output would have been the two tables appearing right after the text "Tables next to each other" and then the larger table appearing after the Second section header.
Try adding another line of LaTeX to force all table floats to use HOLD to the include-in-header
key in the YAML metadata:
include-in-header:
text: |
\addtokomafont{disposition}{\rmfamily}
\floatplacement{table}{H}
KableExtra and Quarto have had some trouble integrating, especially in LaTeX. But this solution has helped this specific issue, for me