将两组行规则传递给xtable

时间:2014-01-01 10:30:03

标签: r latex knitr sweave

请考虑以下MWE(此answer的变体)

\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设定了规则。现在让我们说我也想为不同的行(2,4)设置不同的颜色[green]。有可能吗?

1 个答案:

答案 0 :(得分:2)

是的,您只需要构建一个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))
@