你走在正确的轨道上,但我有点困惑:你想要用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 = "")
处理完这些,继续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 不会让您感到困惑。继续编织!