Home > OS >  add subtitle to kablextra in R markdown
add subtitle to kablextra in R markdown

Time:05-17

for reporting in R-Markdown pdf:
I want to either break the title line in kbl() table from the desired point ( which I do not know how to since \n does not do the job) OR add a subtitle under the main title.

```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title Main Title 
      Main Title Main Title Main Title Main Title Main Title Main Title",
      align = "c") %>%
  kable_styling()
```

enter image description here

Any idea or reference will be highly appreciated.

EDIT. If I add format="pandoc", the extra header will disappear and footnote loses its style.

df %>%
  kbl(caption = "Main Title Main Title Main Title Main Title \nMain Title 
      Main Title Main Title Main Title Main Title \nMain Title Main Title",
      align = "c",
      format = "pandoc") %>%
  kable_styling() %>%
  add_header_above(c(" ", " ", "params" = 2)) %>%
  add_footnote(c("note1", "note2"),
               notation = "number")

enter image description here

CodePudding user response:

Try setting the format = "pandoc" in your kbl and use \n for subtitle like this:

---
title: "Test"
author: "Author"
date: '2022-05-16'
output: pdf_document
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "Main Title \n
      subtitle: Blabla",
      align = "c", format = "pandoc") %>%
  kable_styling() 
```

Output:

enter image description here

CodePudding user response:

---
title: "table subscript title"
output: pdf_document

header-includes:
- \usepackage{caption}
---


```{r}
library(kableExtra)
df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1)
)

df %>%
  kbl(caption = "{Main Title\\\\
      \\scriptsize subscript title  subscript title subscript title\\\\
      \\scriptsize subscript title line 2  subscript title  line 2 subscript title  line 2}",
      align = "c",
      escape = FALSE) %>%
  kable_styling()


    ```


  • Related