Format captions in kableExtra()
Asked Answered
B

2

8

Is it possible to format captions in kableExtra? I would like to center and bold a caption for a table when knitting to HTML in RMarkdown. I have tried to wrap the table in a CSS div where the text was centered, but this did not produce the intended result.

Sample table:

library(kableExtra)
kable(data.frame(a = 1:3, b = 4:6), caption = "CENTER") %>%
  kable_styling(bootstrap_options = "striped")

Yields:

enter image description here

I would like:

enter image description here

Bradybradycardia answered 24/9, 2019 at 20:48 Comment(0)
N
9

If the final format is HTML:

write caption within HTML center and strong tag and set escape to FALSE.

kable(
    data.frame(a = 1:3, b = 4:6), 
    caption = "<center><strong>CENTER</strong></center>",
    escape = FALSE,
    format = "html"
) %>%
    kable_styling(bootstrap_options = "striped")
Nicholasnichole answered 24/9, 2019 at 21:20 Comment(0)
W
0

There is a solution through css stylesheets. The trick is to identify the correct element. Let's assume a default html document.

---
title: "test"
css: template.css
output: html_document 
---

The following css script will work.

.table-wrapper caption {
color: black;
text-align: center;
font-family: Arial;
font-weight: bold;
font-size: xx-large;
}

When in doubt, you can always identify the appropriate element by opening the html document in your browser and inspecting it via ctrl+shift+c.

Woeful answered 24/3, 2020 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.