你是在正確的軌道上,但我有點困惑:你想選擇的行通過hline
和rowcolor
突出?根據我的經驗,rowcolor本身看起來更好,所以我會假設在下面的答案中(但是您可以輕鬆使用兩者,只需附加\\hline
命令)。
作爲獎勵,下面的所有代碼都假設您使用LaTeX booktabs
包,該包提供正確的加權規則(與Hline不同)。說實話,我總是使用booktabs,並且我無法調整代碼以使用hline - 但是如果您更喜歡hline,請將所有\toprule
,\midrule
和\bottomrule
宏替換爲\hline
。
你似乎已經錯過了LaTeX的longtables需要一個特殊的頭,我們需要太提供,作爲一個元素到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並沒有混淆你太多。 繼續編織!