我正在用xtable在R中打印一个LaTeX表。我想插入双行(\\[-1.8ex]\hline \hline \\[-1.8ex])而不是第一行(简单的\hline或\topline)。
我怎么才能自动完成呢?
示例:
table <- data.frame(a=rep(1,2),b=rep(2,2))
print(xtable(table,type = "latex"),
hline.after = c(-1, 0, nrow(table)-1,nrow(table)))结果
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
& a & b \\
\hline
1 & 1.00 & 2.00 \\
\hline
2 & 1.00 & 2.00 \\
\hline
\end{tabular}
\end{table}Desiderata:
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& a & b \\
\hline
1 & 1.00 & 2.00 \\
\hline
2 & 1.00 & 2.00 \\
\hline
\end{tabular}
\end{table}发布于 2019-07-23 00:07:35
我认为您最好的选择是使用add.to.row,如5.9 here中所述。
在您的情况下,它可能是这样的
library(xtable)
table <- data.frame(a=rep(1,2),b=rep(2,2))
tab <- xtable(table, type="latex")
addtorow <- list(
pos=list(-1),
command=c("\\\\[-1.8ex]\\hline")
)
print(tab, type="latex", add.to.row=addtorow)生产

或者更优雅一点,去掉顶行,代之以双行
add <- list(
pos=list(-1),
command=c(
"\\\\[-2ex]\\hline
\\hline \\\\[-2ex]")
)
print(tab, type="latex", add.to.row=add, hline.after=c(0:nrow(table)))% latex table generated in R 3.5.0 by xtable 1.8-2 package
% Mon Jul 22 18:32:44 2019
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\\[-2ex]\hline
\hline \\[-2ex] & a & b \\
\hline
1 & 1.00 & 2.00 \\
\hline
2 & 1.00 & 2.00 \\
\hline
\end{tabular}
\end{table}

发布于 2020-10-12 00:28:50
刚刚从KableExtra的作者那里得到了一个很酷的答案:
如何使用KableExtra在表格的顶部和底部添加双行?#546 https://github.com/haozhu233/kableExtra/issues/546 haozhu233在11小时前发表了评论·最直接的解决方案是使用一些简单的正则表达式。请注意,您最好将它们放在最后,因为我记得kableExtra的一些功能依赖于toprule的位置来实现它的技巧。
library(kableExtra)
kbl(mtcars[1:5, 1:5], booktabs = T) %>%
sub("\\\\toprule", "\\\\midrule\\\\midrule", .) %>%
sub("\\\\bottomrule", "\\\\midrule\\\\midrule", .)这解决了在顶部和底部同时添加双线的问题。
https://stackoverflow.com/questions/57142671
复制相似问题