通过两套规则行xtable(Pass two set of rules for rows to xt

2019-10-19 04:30发布

请考虑以下MWE(从这种变化的答案 )

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{colortbl, xcolor}

\begin{document}

<<do_table, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
rws <- c(1,3,5,6,7)
col <- rep("\\rowcolor[gray]{0.95}", length(rws))
print(xtable(mydf), booktabs = TRUE, 
   add.to.row = list(pos = as.list(rws), command = col))
@

\end{document}

有了这个,我已经为各行1,3,5,6,7的规则。 现在,让我们说,我想也设置不同的颜色[green]对不同行(2,4)。 可能吗?

Answer 1:

是的,你只需要构建一个col包含所有相应的颜色表中的每个位置矢量。 你有两个不同颜色的行例如:

<<do_table, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
rws <- c(1,4)
col <- c("\\rowcolor[gray]{0.95}", "\\rowcolor[gray]{0.7}")
print(xtable(mydf), booktabs = TRUE, 
   add.to.row = list(pos = as.list(rws), command = col))
@


文章来源: Pass two set of rules for rows to xtable