我正在使用 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}
需要的东西:
\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}
最佳答案
我认为最好的办法是使用 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}
关于r - xtable 中的双线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57142671/