我正在尝试使用 r markdown、kable 和 kableExtra 输出乳胶表。当我使用选项 row.names=FALSE 而不是 row.names=TRUE 时,乳胶代码会生成 \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")
错误是:
!扫描使用时发现禁止的控制序列
\检查@nocorr@。
\par l.105 ...颜色{红色} \textbf{21.0 &\vphantom{1} 6}
& \textbf{160} & \textbf{...
怎么了?
这是由数据帧中的重复行引起的,因为第 1 行和第 2 行相同。
检查代码行规范乳胶 https://github.com/cran/kableExtra/blob/master/R/row_spec.R,当 kableExtra 用于 kable 表时,它会检查重复的行。如果找到,则插入vphantom
内的论证修复重复行乳胶 https://github.com/cran/kableExtra/blob/master/R/util.R内部功能。这种 vphantom 插入会弄乱textbf
功能。
这似乎是一个小错误,因此可能值得将其作为 kableExtra 中的问题报告:https://github.com/haozhu233/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")
```
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)