我正在尝试使用 r markdown、kable 和 kableExtra 输出 latex 表。当我使用选项 row.names=FALSE 而不是 row.names=TRUE 时, latex 代码生成\vphantom 代码,该代码会产生错误以创建 pdf 。
问题似乎与 row_spec 选项有关。

这是 Rmarkdown 代码(.Rmd 文件):

---
title: "Test"
output:
pdf_document:
fig_caption: true
keep_tex: true
---

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)


{r}
library(knitr)
library(kableExtra)

temp <- mtcars[1:5,1:5]

kable(temp, format = "latex", booktabs = F,row.names=F)  %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, background = "red")

错误是:



你有什么问题吗?

最佳答案

这是由数据框中的重复行引起的,因为第 1 行和第 2 行相同。

查看 row_spec_latex 的代码,当 kableExtra 用于 kable 表时,它会检查重复的行。如果找到,它会在 fix_duplicated_rows_latex 内部函数中插入 vphantom 参数。这个 vphantom 插入会弄乱 textbf 函数的格式。

这似乎是一个小错误,因此可能值得将其报告为 kableExtra 中的问题: https://github.com/haozhu233/kableExtra 。我确信添加 vphantom 是有充分理由的,但怀疑这是预期的结果。

支持代码:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
```

```{r}
# Keeping the row names (means all rows are unique)
kable(temp, format = "latex", booktabs = F)  %>%
  kable_styling(position = "center") %>%
  row_spec(1, bold = T, color = "red")
```

```{r}
# Highlighting second row (which doesn't have the vphantom statement)
kable(temp, format = "latex", booktabs = F, row.names=F)  %>%
  kable_styling(position = "center") %>%
  row_spec(2, bold = T, color = "red")
```

使用 latex 中的条件颜色渲染表作为具有 rownames = TRUE 的 pdf 文档(rmarkdown、kable 和 kableExtra)-LMLPHP

关于使用 latex 中的条件颜色渲染表作为具有 rownames = TRUE 的 pdf 文档(rmarkdown、kable 和 kableExtra),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49016615/

10-12 17:08