threeparttable does not work in quarto
MarcoPortmann opened this issue · comments
Kable with threeparttable = TRUE
in quarto as shown in the reproducible example below produces an error. The error doesn't occur if no caption and label are set.
---
title: "Reproducible Quarto Document"
format: pdf
---
```{r include=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
```
```{r, echo = FALSE}
#| label: tbl-cars
#| tbl-cap: "Cars"
kbl(head(cars),
booktabs = TRUE) %>%
add_footnote("This is a footnote.", threeparttable = TRUE)
```
For the record, a similar issue has been mentioned here quarto-dev/quarto-cli#7264, on the quarto side, confirming that the bug is on the kableExtra side.
The table
environment is added in knitr::kable. You can reproduce it without loading kableextra. From the issue @jchiquet mentioned, it seems that the quarto team has fixed it in a recent fix.
In the mean time, just use the caption and label option in kable.
knitr::kable(mtcars[1:2, 1:2], 'latex', caption='cars', label='car_table') %>%
cat()
\begin{table}
\caption{\label{tab:car_table}cars}
\centering
\begin{tabular}[t]{l|r|r}
\hline
& mpg & cyl\\
\hline
Mazda RX4 & 21 & 6\\
\hline
Mazda RX4 Wag & 21 & 6\\
\hline
\end{tabular}
\end{table}
@haozhu233, is there a way to get the cross-reference to work? It doesn't work like this:
---
title: "Reproducible Quarto Document"
format: pdf
keep-tex: true
---
```{r include=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
knitr::opts_knit$set(label.prefix = c(table = 'tbl-'))
```
```{r, echo = FALSE}
knitr::kable(mtcars[1:2, 1:2],
'latex',
caption='cars',
label='cartable',
booktabs = TRUE) %>%
add_footnote("This is a footnote.", threeparttable = TRUE)
```
Where is @tbl-cartable?