r - xtable 中的双线

标签 r latex xtable

我正在使用 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)

生产

enter image description here

或者更优雅一点,删除顶行并将其替换为双行

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}

enter image description here

关于r - xtable 中的双线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57142671/

相关文章:

r - 如何在R中的xtable中将换行符放入列标题中

r - 匹配两个向量的子串并创建一个组合它们的新向量

r - 匹配字符串,但前提是不带其他字符串

pdf - pdflatex 的相对优点是什么?

latex \多行 : giving multirow an inverted argument shifts text

html - Rmarkdown 中 html 和 pdf 格式的对齐公式

R + ggplot : plotting irregular time series

r - 从 glm 系数中提取引用水平

r - 你能在 R 的 xtable 中合并单元格吗

r - 如何删除 Knit 输出的 xtable 表中的 % 行