问题
Please consider the following MWE (variation from this 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}
With this I have set a rule for the rows 1,3,5,6,7. Now let's say I would like also to set a different colour [green]
for different rows (2,4). Is it possible?
回答1:
Yes, you just need to construct a col
vector that contains all corresponding colors for each position in the table. Your example with two differently colored rows:
<<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))
@
来源:https://stackoverflow.com/questions/20867670/pass-two-set-of-rules-for-rows-to-xtable