Formatting R markdown kable for PDF / xelatex

20 Views Asked by At

I am trying to format an R markdown table (kable) for exporting to PDF via xelatex. See image below for what I would like to do:

  1. Add some padding in the header above and below the header text
  2. Remove the vertical lines inside the table

Everything else would ideally stay the same. Requested changes to table

This is my code so far. I am using kableExtra to format the table. I assume "extra_css = "padding: 5px" isn't doing me much good since I'm not exporting to HTML, but I am wondering if there is a PDF alternative.

library(tidyverse)
library(knitr)
library(kableExtra)

top_n_projects <- data.frame(
  num = c(1, 2, 3),
  funding_source_AB = c("AAA", "BBB", "CCC"),
  year = c(2020, 2021, 2022), 
  project_title = c("TEST", "TEST", "TEST"), 
  recipient = c("RECIP1", "RECIP2", "RECIP3"), 
  TotalCost = c("Blah blah", "Blah", "Blah")
)

# Make title for table
header <- length(names(top_n_projects)) # Have to specify number of columns wide
names(header) <- "MY HEADER - TEST "

project_kable <- top_n_projects %>%
  kable(., "latex",
    col.names = c(
      "#", "Funder", "Year", "Project title",
      "Recipient", "Total funded"
    ), linesep = ""
  ) %>%
  # Format the header
  row_spec(0,
    bold = TRUE,
    background = "#d8f0ff",
    color = "black"
  ) %>%
  # allow for multiple rows for really long titles
  column_spec(1, bold = TRUE) %>%
  column_spec(4, width = "20em") %>%
  column_spec(5, width = "15em") %>%
  column_spec(6, width = "7em") %>%
  # Add header above table
  add_header_above(
    header = header,
    font_size = 14,
    background = "#6fcdf4", # Change background
    color = "white", # Change text color
    bold = T,
    extra_css = "padding: 5px"
  )
0

There are 0 best solutions below