Home > Back-end >  How do I center a table caption using Kable and kblextra in R when knitting PDF file?
How do I center a table caption using Kable and kblextra in R when knitting PDF file?

Time:11-28

This problem occurs when using the kbl() function to create tables in R if the table's caption is too long. If the caption is too long, the text begins to wrap to the next line, but then it becomes left-justified.

Sometimes the caption is short enough can be solved by adding the landscape argument to the style options, but I'd like a solution that is more flexible. The styling options for kbl offer lots of tools to manipulate the table cells, but I'm having difficulty doing that to the table captions. Here is example code that would result in the problem I'm having.

kbl(mtcars, caption = "This is a really long title that will go past the borders when knitting to PDF in portrait mode. I'd love to figure out how to keep it centered even if the text is really long.", 
    booktabs = TRUE, linesep = "", align = "c") %>% 
    kable_styling(latex_options = c("striped", "hold_position"))

Adding the "hold_position" argument works for keeping the table in centered and in place, but the caption doesn't behave the same. Also, adding around the caption works if knitting to HTML, but I'd like a solution that works when knitting to PDF.

Additionally, using \n to do linebreaks doesn't seem to work like it does for other packages like ggplot().

CodePudding user response:

You may need to use LaTeX's caption package with the option of justification=centering (i.e. \usepackage[justification=centering]{caption} in the header-includes field of the YAML section).

---
header-includes:
  - \usepackage[justification=centering]{caption}
output:
  pdf_document: default
---

```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
library(tidyverse)
library(kableExtra)
```

```{r echo=FALSE}
kbl(mtcars,
  caption = "This is a really long title that will go past the borders when knitting to PDF in portrait mode. I'd love to figure out how to keep it centered even if the text is really long.",
  booktabs = TRUE, linesep = "", align = "c"
)  %>%
  kable_styling(latex_options = c("striped", "hold_position"))
```

enter image description here

CodePudding user response:

Without the additional LaTeX package which @Carlos Luis Rivera has shown, I failed to find any possible way to center the caption by using kable. Therefore, in case you don't want additional LaTeX package, I would recommend using flextable package instead of kable and kableExtra. In the following example I use pdf-latex:

```{r}
library(flextable)
library(tidyverse)

set_flextable_defaults(fonts_ignore = TRUE)
mtcars |> rownames_to_column(var = "carmodel") |> 
    flextable() |> theme_zebra() |>
    align(j = ~.-carmodel, align = "center", part = "all") |>
    autofit(add_w = 0) |>
    set_caption(caption = "This is a really long title that will go past the borders 
        when knitting to PDF in portrait mode. I'd love to figure out how to keep it 
        centered even if the text is really long.")

The resulted PDF table:

enter image description here

  • Related