4

我正在使用 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)

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

谢谢,

大卫

4

3 回答 3

3

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

> 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。

于 2013-10-07T09:37:26.617 回答
2

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

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

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

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 = "")

处理完这些,继续printxtable:

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 不会让您感到困惑。继续编织!

于 2013-10-07T18:20:07.087 回答
1

你可能有更多的运气在乳胶论坛上发布这个。您应该注意 xcolor/longtable 不兼容:http ://www.ukern.de/tex/xcolor.html 。

于 2013-10-07T16:02:52.920 回答