![带有 kableExtra 的 LaTeX 表格在编织为 PDF 时会呈现与行组重叠的单元格背景](https://linux22.com/image/470328/%E5%B8%A6%E6%9C%89%20kableExtra%20%E7%9A%84%20LaTeX%20%E8%A1%A8%E6%A0%BC%E5%9C%A8%E7%BC%96%E7%BB%87%E4%B8%BA%20PDF%20%E6%97%B6%E4%BC%9A%E5%91%88%E7%8E%B0%E4%B8%8E%E8%A1%8C%E7%BB%84%E9%87%8D%E5%8F%A0%E7%9A%84%E5%8D%95%E5%85%83%E6%A0%BC%E8%83%8C%E6%99%AF.png)
我正在使用kableExtra
R Markdown 中的包创建一个 LaTeX 表并将其转换为 PDF。我使用来自的cell_spec()
函数kableExtra
将一些简单的条件格式应用于几个列,效果很好。但是,当我将其转换为 PDF 时,结果是一个格式奇怪的单元格背景(请参阅变量5,var6等等),与我的行组标题略有重叠(见下图)。这种情况很烦人,所以我想知道是否有人有什么技巧可以添加到下面的示例代码中以解决这个问题?
我对 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)
```