rmarkdown knit to pdf kableExtra header and column alignment

Hi! I am using Rmarkdown to knit to pdf, and producing tables with kableExtra. I am interested in getting the column headers (names) and column values to align.

To mimic my scenario, I converted all values to character and created multiline table headers.

Here is my Rmarkdown, with some pretty custom set up.

---
title: "Untitled"
output:
  pdf_document: 
    toc: true
    toc_depth: 2
    number_sections: true
    keep_tex: yes
geometry: margin=1in
latex_engine: pdflatex
classoption: landscape
header-includes:
  \usepackage{helvet}
  \renewcommand\familydefault{\sfdefault}
include-before:
- '`\newpage{}`{=latex}'
---
library(tidyverse)
library(kableExtra)
library(palmerpenguins)
penguins_names <- penguins %>% 
  names() %>% 
  str_replace_all( "_", "\n")
penguins %>% 
  slice(1:10) %>% 
  mutate_all(as.character) %>% 
  kbl(
    format = "latex",
    booktabs = TRUE,
    longtable = TRUE,
    linesep = "",
    align = "l",
    col.names = linebreak(penguins_names),
    escape = FALSE
    ) %>%
  kable_styling(
      position = "left",
      latex_options = c("striped", "repeat_header"),
      stripe_color = "gray!15"
    )

and the knitted result:

You can see that the alignment of the header differs among the columns, even though they are all character values.

Does any one have any ideas on how to make all column headers left align? Thank you!

I am using knitr 1.37, kableExtra 1.3.4, and tinytex 0.34 on Windows 10.

1 Like

Just solved it myself with

col.names = linebreak(penguins_names, align = "l") 🤦

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.