Question

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}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top