xtable - background colour of added rows

白昼怎懂夜的黑 提交于 2019-12-11 04:05:41

问题


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.

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}

回答1:


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}'
                        )
                              )
              )

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



来源:https://stackoverflow.com/questions/18280547/xtable-background-colour-of-added-rows

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