When rendering a Rmarkdown file to pdf using the bookdown::pdf_document2
output, how can I obtain a table with a numbered caption (e.g. Table 1: caption) when rendering a table with the gt
package ?
It works if I render the table using knitr
, but not with gt
.
I also saw the following advice in the bookdown documentation:
If you decide to use other R packages to generate tables, you have to make sure the label for the table environment appears in the beginning of the table caption in the form
(#label)
(again,label
must have the prefixtab:
)
However, this does not work for me either.
Here is a MWE of a Rmarkdown file that reproduces the problem:
---
output: bookdown::pdf_document2
---
```{r, echo = FALSE}
gt::gt(head(iris)) |>
gt::tab_header("Iris data set using `gt`")
```
```{r, echo = FALSE}
gt::gt(head(iris)) |>
gt::tab_header("(\#tab:label) Iris data set using `gt`, with special label")
```
```{r, echo = FALSE}
knitr::kable(head(iris), caption = "Iris data set using `kable`")
```
And here is the output that I currently have: