我正在使用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}
这很好用,但是,如果我将最后一行代码调整为
,我正在使用的表应该是一个longtable。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)
输出非常难看,行未按预期突出显示。任何人都可能知道这个问题的答案吗?
感谢,
大卫
答案 0 :(得分: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。
答案 1 :(得分:2)
您走在正确的轨道上,但我有点困惑:您是否希望hline
和 rowcolor
突出显示所选行?根据我的经验,单独的rowcolor看起来更好,所以我将在下面的答案中假设(但你可以轻松地使用它们,只需附加\\hline
命令)。
作为奖励,下面的所有代码都假设您使用LaTeX booktabs
包,它提供了正确的加权规则(与hline不同)。说实话,我总是使用booktabs,我无法调整代码以使用hline - 但如果您更喜欢hline,请替换所有\toprule
,\midrule
和\bottomrule
带有\hline
的宏。
您似乎错过了LaTeX longtables需要特殊标题,我们还需要将其作为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 = "")
完成后,请继续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并不会让你太过困惑。 继续编织!
答案 2 :(得分:1)
您可能会在乳胶论坛上发布更多运气。您应该注意xcolor / longtable不兼容:http://www.ukern.de/tex/xcolor.html。