带有 kableExtra 的 LaTeX 表格在编织为 PDF 时会呈现与行组重叠的单元格背景

带有 kableExtra 的 LaTeX 表格在编织为 PDF 时会呈现与行组重叠的单元格背景

我正在使用kableExtraR Markdown 中的包创建一个 LaTeX 表并将其转换为 PDF。我使用来自的cell_spec()函数kableExtra将一些简单的条件格式应用于几个列,效果很好。但是,当我将其转换为 PDF 时,结果是一个格式奇怪的单元格背景(请参阅变量5var6等等),与我的行组标题略有重叠(见下图)。这种情况很烦人,所以我想知道是否有人有什么技巧可以添加到下面的示例代码中以解决这个问题?

我对 LaTeX 编码还不太熟悉,还不太理解这门语言,所以非常感谢大家的帮助!谢谢。

在此处输入图片描述

---
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: SourceSansPro
output: pdf_document
editor_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)
```

相关内容