xtable 中的双线
Double line in xtable
我正在用 xtable
在 R 中打印 LaTeX table。
我想插入双行 (\[-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}
刚刚从 KableExtra 的作者那里得到了一个很酷的答案:
如何使用 KableExtra 在 table 的顶部和底部添加双线? #546
https://github.com/haozhu233/kableExtra/issues/546
haoshu233 11 小时前评论 •
最直接的解决方案是使用一些简单的正则表达式。请注意,您最好将它们放在最后,因为我记得 kableExtra 的某些功能依赖于 toprule 的位置来完成它的技巧。
library(kableExtra)
kbl(mtcars[1:5, 1:5], booktabs = T) %>%
sub("\\toprule", "\\midrule\\midrule", .) %>%
sub("\\bottomrule", "\\midrule\\midrule", .)
这样就解决了同时在顶部和底部添加双线的问题
我正在用 xtable
在 R 中打印 LaTeX table。
我想插入双行 (\[-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}
刚刚从 KableExtra 的作者那里得到了一个很酷的答案:
如何使用 KableExtra 在 table 的顶部和底部添加双线? #546 https://github.com/haozhu233/kableExtra/issues/546 haoshu233 11 小时前评论 • 最直接的解决方案是使用一些简单的正则表达式。请注意,您最好将它们放在最后,因为我记得 kableExtra 的某些功能依赖于 toprule 的位置来完成它的技巧。
library(kableExtra)
kbl(mtcars[1:5, 1:5], booktabs = T) %>%
sub("\\toprule", "\\midrule\\midrule", .) %>%
sub("\\bottomrule", "\\midrule\\midrule", .)
这样就解决了同时在顶部和底部添加双线的问题