【问题标题】:Add two commands to the add.to.row arguments in xtable将两个命令添加到 xtable 中的 add.to.row 参数
【发布时间】:2016-02-04 07:48:14
【问题描述】:

我已使用下面的两个答案为 xtable 中的交替行添加颜色或为长表添加页脚,但我需要弄清楚如何做到这两点。

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

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

有没有办法同时使用两者?

【问题讨论】:

    标签: r latex r-markdown xtable


    【解决方案1】:

    从您列出的答案看来,您使用 LaTeX 作为输出。您可以通过为每个命令分配一个位置来组合两个或多个 add.to.row 命令。命令必须是模式character 的向量,并且位置必须在列表中。在下文中,我创建一个列表addtorow <- list(),然后分配位置:addtorow$pos <- as.list(c(rowIndexForFirstCommands, rowIndexForSecondCommands)) 及其对应的命令到addtorow$command <- as.vector(c(firstCommands, secondCommands),mode="character")。这是一个最小的工作示例:

    \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}
    

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 2015-08-12
      • 2021-12-17
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多