Xtable灰色行覆盖垂直线

时间:2016-07-18 15:47:54

标签: r-markdown xtable

    ---
title: "Title"
author: ''
date: ''
output:
 pdf_document:
  template: default.tex
geometry: top=0.5cm, bottom=0.5cm, left=0.5cm, right=0.5cm
header-includes: null
fontsize: 4pt
classoption: portrait
sansfont: Calibri Light
---
#Name1: `r "Name1"`  
#Name2: `r "Name2"`

```{r,  echo=FALSE,  message=FALSE, warning=FALSE, results='asis'}
df <- mtcars
n = nrow(df)
hlines=c(-1,0,(n-1),n)
my_align = "c|c|c|ccccc|ccc|c|"
rws <- seq(1, (n-1), by = 2)
col <- rep("\\rowcolor[gray]{.90} ", length(rws))
xtable::print.xtable(xtable(df
, align = my_align)
, add.to.row = list(pos = as.list(rws), command = col)
, booktabs = F
, hline.after = hlines, type = "latex") 
```

我正在使用Rmarkdown来打印具有大量格式的表格。当我添加add.to.rwo部分以获得灰色和白色交替行时,将在灰色行中删除垂直线。

我该如何纠正?创建一个可重复的例子是非常困难的,但希望同样的问题将适用于任何df(背后有正确的Latex包)

谢谢:)

1 个答案:

答案 0 :(得分:1)

尝试比较这两个表。第一个是您编码的表格,第二个是pixiedusthhline选项设置为TRUE

---
title: "Title"
author: ''
date: ''
output:
 pdf_document:
geometry: top=0.5cm, bottom=0.5cm, left=0.5cm, right=0.5cm
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 
fontsize: 4pt
classoption: portrait
sansfont: Calibri Light
---
#Name1: `r "Name1"`  
#Name2: `r "Name2"`

```{r,  echo=FALSE,  message=FALSE, warning=FALSE, results='asis'}
library(xtable)
df <- mtcars
n = nrow(df)
hlines=c(-1,0,(n-1),n)
my_align = "c|c|c|ccccc|ccc|c|"
rws <- seq(1, (n-1), by = 2)
col <- rep("\\rowcolor[gray]{.90} ", length(rws))
xtable::print.xtable(xtable(df
, align = my_align)
, add.to.row = list(pos = as.list(rws), command = col)
, booktabs = F
, hline.after = hlines, type = "latex") 
```

```{r}
library(pixiedust)
dust(df,
     hhline = TRUE,
     keep_rownames = TRUE) %>%
  medley_bw() %>%
  sprinkle_colnames(.rownames = "") %>%
  sprinkle(cols = c(".rownames", "mpg", "cyl", "qsec", "gear", "carb"),
           border = "right") %>%
  sprinkle(rows = nrow(mtcars),
           border = "top") %>%
  sprinkle(bg_pattern_by = "rows")
```