left- align table caption with kable or kableExtra
Asked Answered
C

1

13

Is it possible to left-align the table caption? I am annoyed by the APA6th rules but I have to left-align the table caption somehow.

Take for example this table:

library(knitr)
library(kableExtra) 

kable(mtcars[1:10, 1:6], format = "latex", caption = "I need this left-aligned.", booktabs = T) %>% 
      kable_styling(position = "left") %>%
      group_rows("Group 1", 4, 7) %>%
      group_rows("Group 2", 8, 10)

The caption will always be centered above the table, even if I left-align the table position with kable_styling(position = "left").

EDIT: See here for a temporary solution that worked at least for me.

Cranberry answered 6/5, 2018 at 14:23 Comment(0)
B
2

You only need to add the Latex package caption in the YAML header section with singlelinecheck=false specified to have the caption of your title left-aligned. Here is a reproducible example:

---
output:
  pdf_document: default
header-includes:
   - \usepackage[singlelinecheck=false]{caption}
---
```{r, echo = FALSE, message = FALSE}
library(knitr)
library(dplyr)
library(kableExtra)
```

```{r, echo = FALSE, message = FALSE}
kable(mtcars[1:10, 1:6], format = "latex", caption = "I need this left-aligned.", booktabs = T) %>% 
  kable_styling(position = "left") %>%
  group_rows("Group 1", 4, 7) %>%
  group_rows("Group 2", 8, 10)
```

Output:

enter image description here

Barrington answered 18/9, 2022 at 17:12 Comment(1)
can you please elaborate what does singlelinecheck=false doesShaunshauna

© 2022 - 2024 — McMap. All rights reserved.