Mathica
Mathica

Reputation: 1303

how to center kable title/caption while keeping rows left aligned in R kableextra

I want to have the title(caption) of the table centered, but the values of columns left assigned. I also want my caption to be broken into two lines and each line to be centered.

align option changes both of them in a same way.

head(mtcars) %>%
   kbl(caption = "{first row\\\\  
                    second row}",
          align = "l") %>%
   kable_styling()

Upvotes: 3

Views: 2847

Answers (1)

Abdur Rohman
Abdur Rohman

Reputation: 2944

Maybe this helps:

head(mtcars) %>%    
    kbl(align = "l") %>% 
    add_header_above(data.frame("{first row \\\\ second row}", 12), 
                     monospace = TRUE) %>%
    kable_styling()

The result:

enter image description here

Update

I assume that you are trying to produce a table in html format. To center the caption and put a line break on the caption texts, you can use html elements: <center> for centering and <br> for line breaking.

head(mtcars) %>%
    kbl(caption = "<center>{first row <br>
                  second row}</center>", 
        align = "l") %>% 
    kable_styling()

The result:

enter image description here

References

  1. Format captions in kableExtra()
  2. https://developer.mozilla.org/en-US/docs/Web/HTML/Element

Upvotes: 3

Related Questions