Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove automatic table labels when using kable and bookdown for HTML

I am creating a bookdown project which includes kable tables. The final output is HTML. However, I do not want the automatic "Table 1:" labels and instead want only my custom caption I create in my kable table. Is there a way to turn this part of the kable/bookdown automatic label setting off globally for the entire bookdown project? An example table looks like below:

library(knitr)
library(kableExtra)

df <- head(mtcars)
mytable <- df %>%
  kbl(caption = "The caption I want") %>%
  kable_classic(full_width = F, html_font = "Cambria")

And I reference the file that contains this table and the tbale creation script in my Rmd bookdown file:

source("file_with_table.R")
mytable

The output caption will read Table 1: The caption I want instead of just The caption I want

I don't need a list of tables or for it to be linked to text for the purposes of this specific project. I have a lot of tables and want to avoid manually adding text since right now my table captions are generated using a function that loops through different measures in my data.

I tried editing the _output.yml of my book but this didn't work. I have a lot of html files in this book or I would just edit the html directly. I know people have managed this for pdf or latex, but I'm not sure about strictly HTML.

like image 469
MattCatfish Avatar asked May 24 '26 15:05

MattCatfish


1 Answers

The approach outlined in my answer works here too.

Add a file _bookdown.yml to the same directory of your bookdown file with the following content:

language:
  label:
    tab: !expr "function(i) ''"

res To quote bookdown chapter 4.5:

If the language of your book is not English, you will need to translate certain English words and phrases into your language, such as the words “Figure” and “Table” when figures/tables are automatically numbered in the HTML output.(...) For example, if you want FIGURE x.x instead of Figure x.x, you can change fig to "FIGURE " (...) or you can pass an R function that takes a figure reference number as the input and returns a string. (e.g., !expr function(i) paste('Figure', i))

We can use this, to remove the "Table XX:" entirely. I had to wrap the whole expression in quotes, because the colon : has it's own meaning within YAML.


You can also use writeLines() to create the yaml header within the setup chunk:

Full Reprex for demonstration purposes

---
title: "Table caption things"
output: bookdown::html_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)

fileConn<-file("_bookdown.yml")
writeLines(r"{
language:
  label:
    tab: !expr "function(i) ''"
           }", fileConn)
close(fileConn)

```

```{r }
head(mtcars) %>%
  kbl(caption = "The caption I want") %>%
  kable_classic(full_width = F, html_font = "Cambria")
```
like image 97
Tim G Avatar answered May 27 '26 07:05

Tim G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!