Pass two set of rules for rows to xtable

北慕城南 提交于 2019-12-11 05:07:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!