以行名称= TRUE(rmarkdown,kable和kableExtra)的形式在乳胶中将条件颜色呈现为pdf文档 [英] Rendering Table with conditional color in latex as a pdf document with rownames = TRUE (rmarkdown, kable and kableExtra )

查看:79
本文介绍了以行名称= TRUE(rmarkdown,kable和kableExtra)的形式在乳胶中将条件颜色呈现为pdf文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用r markdown,kable和kableExtra输出乳胶表.当我使用选项row.names = FALSE而不是row.names = TRUE时,乳胶代码生成\ vphantom代码,从而产生错误以创建pdf.看来问题出在row_spec选项上.

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

  ---标题:测试"输出:pdf_document:fig_caption:正确keep_tex:是---{r设置,包含= FALSE}knitr :: opts_chunk $ set(echo = FALSE){r}图书馆(针织)图书馆(kableExtra)temp<-mtcars [1:5,1:5]kable(temp,format ="latex",booktabs = F,row.names = F)%&%;%kable_styling(position ="center")%&%;%row_spec(1,粗体= T,背景=红色") 

错误是:

!扫描使用时发现了禁止的控制序列\ check @ nocorr @.\ par l.105 ... color {red} \ textbf {21.0& \ vphantom {1} 6}&\ textbf {160}&\ textbf {...

您对正在发生的事情有任何疑问吗?

解决方案

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

在kableExtra为用于有线表时,它检查是否有重复的行.如果找到一个,则将 vphantom 参数插入

I am attempting to output a latex table using r markdown, kable and kableExtra. When i use the option row.names=FALSE instead of row.names=TRUE the latex code generates \vphantom code which produce an error to create the pdf . It seems the problem is linked to the row_spec option.

Here is Rmarkdown code (.Rmd File):

---
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")

The error is :

! Forbidden control sequence found while scanning use of \check@nocorr@. \par l.105 ...color{red} \textbf{21.0 &\vphantom{1} 6} & \textbf{160} & \textbf{...

Do you have any issue of what is happening ?

解决方案

This is caused by the duplicated rows in the dataframe, as both rows 1 and 2 are the same.

Reviewing the code for row_spec_latex, when kableExtra is used against a kable table, it checks for duplicated rows. If it finds one, it inserts the vphantom argument within the fix_duplicated_rows_latex internal function. This vphantom insertion is then messing up the formatting of the textbf function.

This seems like a slight bug, so it may be worth reporting it as an issue in kableExtra: https://github.com/haozhu233/kableExtra . I am sure that the vphantom is added for a good reason though, but doubt this was an intended consequence.

Supporting code:

---
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")
```

这篇关于以行名称= TRUE(rmarkdown,kable和kableExtra)的形式在乳胶中将条件颜色呈现为pdf文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆