xtable-添加的行的背景色 [英] xtable - background colour of added rows

查看:130
本文介绍了xtable-添加的行的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xtable和knitr创建一个带有插入的摘要行的表.我想用不同的颜色插入这些行.使用add.to.row选项,我设法插入了线,但是不能同时改变颜色.

I'm trying to make a table with inserted summary rows using xtable and knitr. I'd like to have these inserted lines in a different colour. Using the add.to.row option, I've managed to either insert the lines or change the colour, but not both at the same time.

之所以要在xtable中插入行而不是提前插入,是因为我希望总计"行由于其在原始数据集中的长度而分布在两列中.因此,所需的解决方案将在摘要行中同时包含多列单元格,并使用不同颜色的背景. 我正处于我的Latex学习曲线的开始,因此将不胜感激.

The reason I'm inserting the rows within xtable, rather than ahead of time, is that I want the "total" row to be spread over two columns due to its length in my original dataset. So the required solution would have both multicolumn cells in the summary row and a different-colour background. I'm at the very beginning of my Latex learning curve, and any help would be greatly appreciated.

下面是一个示例,其中包括一个伪造的数据集:

Here is an example, including a fake dataset:

\usepackage{booktabs}
\usepackage{colortbl, xcolor}

\begin{document}

<<try, echo = FALSE, eval = TRUE, results = 'asis'>>=  
library(xtable)
dat <- data.frame(type = c(rep("a", 5), rep("b", 5)), a = c(1:5, 1:5), b = 1:10, c = 21:30)
temp <- ddply(dat, .(type), summarize, SumB = sum(b))

rws <- which(dat$a == 5)
col <- rep("\\rowcolor[gray]{0.95}", length(rws)) ## colour definition prepared, but not used

#Making the command for inserting summary rows
temp$insert <- ""
for(i in 1:nrow(temp)){
    temp[i,]$insert <- sprintf("\\multicolumn{3}{l}{Total %s} &
        \\multicolumn{1}{c}{%d} \\\\ ", temp[i,]$type, temp[i,]$SumB)
            }

print(xtable(dat, align = "llccc"), 
    include.rownames=FALSE,
    booktabs = TRUE, 
    sanitize.text.function=function(x){x},
    add.to.row = list(pos = as.list(rws),
        command = paste(temp$insert, sep = ",")))

@
\end{document}

推荐答案

我会说colortbl就足够了,这里不需要xcolor. 尝试将rowcolor添加到您的命令中,如下所示:

I'd say colortbl is enough, no need for xcolor here. Try to add rowcolor to your command like this:

print(somextable,                  
              floating=FALSE, 
              hline.after=NULL,                  
              size="\\footnotesize",
              add.to.row=list(pos=list(-1,0,nrow(somextable),0,
                                       1,2,3,
                                       4,5,6,
                                       7,8,9,10,11
                                       ), 
              command=c('\\toprule ',
                        '\\midrule ',
                        '\\bottomrule',
                        '\\\\ \\rowcolor[gray]{.9}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}',
                        '\\\\ \\rowcolor[gray]{.9}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}',
                        '\\\\ \\rowcolor[gray]{.9}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}',
                        '\\rowcolor[gray]{.97}'
                        )
                              )
              )

还应该使用add.to.row中通用创建的行.

should also work with generically created rows in add.to.row.

这篇关于xtable-添加的行的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆