haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

Double "Table" in caption #831

Closed EricMarcon closed 5 months ago

EricMarcon commented 6 months ago

kable_styling() makes "Table" appear twice in captions when knitted by bookdown to HTML with kableExtra v. 1.4.0. This was not the case with v. 1.3.

This is a MWE of .Rmd file:

---
title: "Double Table"
output:
  bookdown::html_document2
---

```{r}
cars |>
  knitr::kable(caption = "Cars data") |>
  kableExtra::kable_styling()


The caption obtained in the HTML output is "**Table 1: Table 2: Cars data**". This does not happen when the output format is `bookdown::pdf_document2`.
Removing `kable_styling()` from the pipeline solves the issue.

Thanks for your great package.
dmurdoch commented 6 months ago

That happens when you use knitr::kable without specifying the format, because kable produces a markdown table, and kable_styling can't recognize the existing label in it. Workarounds are to specify format = "html" in the call to kable, or use kableExtra::kbl() instead. Then things are fine.

dmurdoch commented 6 months ago

The reason kable_styling has trouble here is that it's looking for a caption using the format that html_document produces, but bookdown::html_document2 produces captions in a different format, since it includes table numbers.

EricMarcon commented 5 months ago

Using kableExtra::kbl() instead of knitr:kable() works perfectly. I suggest to write it in the documentation: always replace knitr:kable() by kableExtra::kbl() to avoid trouble with bookdown.

The other workaround, i.e. specify format = "html" is not so good because markdown documents should be as independent as possible from the output format in my opinion.

Thanks for the solution.

NewGraphEnvironment commented 5 months ago

I just dealt with this same double table in the captions of bookdown reports and was able to get around it by specifying param label = NA within the knitr::kable() function.