Add two commands to the add.to.row arguments in xtable

别等时光非礼了梦想. 提交于 2021-02-08 16:56:56

问题


I have used the two answers below to add colors to alternating rows in xtable or to add a footer to a long table, but I need to figure out how to do both.

(1) How to only show table caption once in "list of table" for a table split onto multiple pages and

(2) R, knitr, xtable, alternating row colors

Is there a way to use both at the same time?


回答1:


From the answers you listed it seems you used LaTeX as output. You can combine two or more add.to.row command by assigning a position to each command. The commands must be a vector of mode character and the positions must be in a list. In the following, I create a list addtorow <- list() and then assign positions: addtorow$pos <- as.list(c(rowIndexForFirstCommands, rowIndexForSecondCommands)) and their corresponding commands to addtorow$command <- as.vector(c(firstCommands, secondCommands),mode="character"). Here is a minimal working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{longtable}
\usepackage[table]{xcolor}
\begin{document}

<<yoman,echo=FALSE,results='asis'>>=
library(xtable)
#define a data frame
mydf <- data.frame(id = make.unique(rep(letters, length.out = 100), sep=''), 
                   var1 = rnorm(100), 
                   var2 = runif(100),
                   var3=rexp(100),
                   var4=rpois(100,1.4))

#define row indexes to be highlighted (each two), 
#and repeat rowcolor command correspondingly
rws <- seq(1, (nrow(mydf)), by = 2)
col <- rep("\\rowcolor[gray]{0.95}", length(rws))

#create a list that will receive the instructions 
#for add.to.row and add the two instructions
addtorow <- list()

#assign a position argument to addtorow
#rws are the row indexes for the row to be colored, 
#0 is the row index for longtable argument
addtorow$pos <- as.list(c(
                         rws, #positions for first commands(highlighting rows)
                         0    #position for second command (longtable argument)
                         ))

#assign corresponding commands to addtorow
addtorow$command <- as.vector(c( col, #first command (highlighting rows)
                                 paste("\\hline \n",
                                       "\\endhead \n",
                                       "\\hline \n",
                                       "{\\footnotesize Continued on next page} \n",
                                       "\\endfoot \n",
                                       "\\endlastfoot \n",
                                       sep="")), #second command (longtable)
                              mode="character")
print(xtable(mydf,
             caption = "My caption "),
             tabular.environment = "longtable",
             floating = FALSE,
             include.colnames = TRUE,
             include.rownames = TRUE, 
             add.to.row = addtorow,    
             hline.after=c(-1),        # addtorow substitute default hline for first row
             caption.placement="top")

@

\end{document}


来源:https://stackoverflow.com/questions/33511311/add-two-commands-to-the-add-to-row-arguments-in-xtable

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