【问题标题】:knitr xtable highlight and add horizontal lines for the same row,knitr xtable 突出显示并为同一行添加水平线,
【发布时间】:2013-10-13 19:29:13
【问题描述】:

我正在使用 knitr 和 xtable 来自动化我的报告程序。我想突出显示表格的几行,并在每行上方突出显示一条水平线。我使用的 .Rnw 文件如下所示:

\usepackage{colortbl, xcolor}
\usepackage{longtable}

\begin{document}

<<do_table, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))

print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2))
@

\end{document}

这很好用,但是,如果我将最后一行代码调整为

,我正在使用的表应该是一个长表
print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2),tabular.environment="longtable",floating=FALSE)

输出非常难看,并且行没有按预期突出显示。有人可能知道这个问题的答案吗?

谢谢,

大卫

【问题讨论】:

    标签: r knitr xtable longtable


    【解决方案1】:

    抱歉,有点离题,但演示了一种仅用于轻松突出单元格/行的降价解决方案:

    > mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
    > library(pander)
    > emphasize.strong.rows(c(1, 3))
    > pander(mydf)
    
    ---------------------------
     id      var1       var2   
    ----- ---------- ----------
    **1** **0.7194** **0.6199**
    
      2     0.8094     0.1392  
    
    **3** **-1.254** **0.5308**
    
      4     0.4505     0.8235  
    
      5    -0.3779     0.7534  
    
      6    -0.3518     0.3055  
    
      7     1.759      0.5366  
    
      8     0.9822     0.9938  
    
      9     1.549      0.3589  
    
     10     -1.077     0.5153  
    ---------------------------
    

    可以直接转换成LaTeX或者pdf。

    【讨论】:

      【解决方案2】:

      您可能会更幸运地在乳胶论坛上发布此内容。您应该注意 xcolor/longtable 不兼容:http://www.ukern.de/tex/xcolor.html

      【讨论】:

        【解决方案3】:

        你在正确的轨道上,但我有点困惑:你想要hline rowcolor 突出显示的选定行吗?根据我的经验,单独的 rowcolor 看起来更好,所以我会在下面的答案中假设(但您可以轻松地同时使用这两种方法,只需附加 \\hline 命令)。

        作为奖励,下面的所有代码都假设您使用 LaTeX booktabs 包,它给出了正确的加权规则(与 hline 不同)。老实说,我一直使用 booktabs,我懒得调整代码以使用 hline —— 但如果您更喜欢 hline,请将所有 \toprule\midrule\bottomrule 宏替换为 \hline

        您似乎错过了 LaTeX 长表需要一个特殊的标题,我们也需要将它作为元素提供给 add.to.row 列表的 command 向量(这可能是您的排版表看起来不好的原因) .

        longtable.xheader <-
           paste("\\caption{Set your table caption.}",
             "\\label{tab:setyourlabel}\\\\ ",
             "\\toprule ",
             attr(xtable(mydf), "names")[1],
             paste(" &", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""),
             "\\\\\\midrule ",
             "\\endfirsthead ",
             paste0("\\multicolumn{", ncol(xtable(mydf)), "}{c}{{\\tablename\\ \\thetable{} -- continued from previous page}}\\\\ "),
             "\\toprule ",
             attr(xtable(mydf), "names")[1],
             paste("&", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""),
             "\\\\\\midrule ",
             "\\endhead ",
             "\\midrule ",
             paste0("\\multicolumn{", as.character(ncol(xtable(mydf))), "}{r}{{Continued on next page}}\\\\ "),
             "\\bottomrule \\endfoot ",
             "\\bottomrule \\endlastfoot ",
             collapse = "")
        

        处理完这些,继续print xtable:

        print(xtable(mydf), 
              floating = FALSE, % since longtable never floats
              hline.after = NULL, % hline off since I use booktabs
              add.to.row = list(pos = list(-1, 
                                       c(0, 2),
                                       nrow(xtable(mydf))),
                            command = c(longtable.xheader, 
                                        "\\rowcolor[gray]{0.75}\n",
                                        "%")), % comments out a spurious \hline by xtable
              include.rownames = FALSE, % depends on your preference
              include.colnames = FALSE, % depends on your preference
              type = "latex", 
              tabular.environment = "longtable",
              % xtable tries to escape TeX special chars, can be annoying sometimes
              sanitize.text.function = function(x){x},
              % not all dashes are meant to be math negative sign, set according to your data
              math.style.negative = FALSE)
        

        我希望我在答案中使用 booktabs 不会让您感到困惑。 继续编织!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-18
          • 1970-01-01
          • 2013-05-10
          • 2011-02-26
          • 2015-03-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多