Quantcast
Channel: Active questions tagged kable - Stack Overflow
Viewing all articles
Browse latest Browse all 117

How to fix overlapping cell background in kableExtra when knitting to PDF?

$
0
0

I'm creating a table with kableExtra to include in my R Markdown report which is knitted to PDF. I've used the cell_spec() function from kableExtra to apply some simple conditional formatting to a few of the columns which works fine. However, when I knit to PDF, the result is an oddly formatted cell background which overlaps slightly with my row group headers (see image below). Its quite annoying that this happens, so I'm wondering if anyone has any tricks that I can add to my example code below that can fix this?

I'm quite new to coding in LaTeX and still don't really understand the language, so any help is really appreciated! Thanks.

enter image description here

---title: >  `\vspace{-1.8cm}`{=latex}header-includes:  - \usepackage{geometry} - \geometry{top=0.1in,left=0.1in,bottom=0.1in,right=0.1in} - \usepackage[default]{sourcesanspro} - \usepackage[T1]{fontenc} - \usepackage{fancyhdr} - \pagestyle{fancy} - \fancyhf{} - \addtolength{\headheight}{3.0cm} - \rhead{\LARGE Vertical Alignment of Header Text} - \fancypagestyle{plain}{\pagestyle{fancy}} - \renewcommand{\headrulewidth}{0pt}mainfont: SourceSansProoutput: pdf_documenteditor_options:  chunk_output_type: console---```{r setup, include=FALSE}knitr::opts_chunk$set(  echo = FALSE,  message = FALSE,  warning = FALSE,  dev = "cairo_pdf"  )``````{r}library(tidyverse)library(kableExtra)``````{r cars}# Create example data.set.seed(1)x <- paste0("var", 1:10)d <- data.frame(name = paste("Player", LETTERS[1:25]))d[x] <- round(rnorm(25, 100, 30), 1)d$group = rep(paste("Group", 1:5), each = 5)``````{r}# Apply cell_spec() to a few columns.tbl <- d %>%  mutate(across(var5:var9,                ~cell_spec(.x,                           background = case_when(                             .x > mean(.x) + sd(.x) ~ "#EAE2F6",                             .x < mean(.x) - sd(.x) ~ "#FEFEDA",                             .default = "white"                             )                           )                ))``````{r}# Table output.kbl(tbl[, 1:11],    booktabs = TRUE,    escape = FALSE,    align = c("l", rep("c", ncol(tbl) - 1))) %>%  kable_styling(latex_options = c("HOLD_position"),                font_size = 8.5) %>%  add_header_above(c(" " = 1, "Header 1" = 4, "Header 2" = 3,"Header" = 3)) %>%  pack_rows(index = table(fct_inorder(tbl$group)), background = "#ED1B2F",            color = "white", bold = TRUE)```

Viewing all articles
Browse latest Browse all 117

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>