xtable 中的水平虚线或点线

horizontal dashed or dotted lines in xtable

我想知道是否有办法使用命令 hline.after 使 xtable 中的一些 hlines 变成虚线或点线。我找不到解决方案。

MWE(Rnw):

\documentclass{article}
\usepackage{booktabs}
\begin{document}

<<mytable, results='asis', echo=F>>=
library("xtable")
print(xtable(matrix(rnorm(32), 8,4), align="lllll"), booktabs=T, hline.after = c(-1,0,4,8))
@

\end{document}

给出此胶乳 table 代码:

\begin{table}[ht]
\centering
\begin{tabular}{lllll}
\toprule 
 & 1 & 2 & 3 & 4 \ 
 \midrule
 1 & 0.50 & -0.49 & -1.32 & -0.29 \ 
 2 & 1.01 & 0.57 & 1.35 & 0.54 \ 
 3 & -1.20 & 1.02 & 1.12 & 0.07 \ 
 4 & 0.04 & 0.14 & 0.23 & -0.38 \ 
  \midrule
 5 & -1.71 & -0.90 & 0.59 & -0.05 \ 
 6 & 0.14 & -0.82 & -0.44 & 0.52 \ 
 7 & -0.28 & 1.91 & -1.80 & 0.53 \ 
 8 & 0.38 & 2.66 & 0.26 & -1.38 \ 
 \bottomrule
 \end{tabular}
 \end{table}

第 4 行后的实线 \midrule 不是虚线,最好是虚线。

我知道,在乳胶水平虚线中我可以使用 arydshln 包制作,虚线也可以操纵 \dashlinedash{}, \dashlinegap{} 和 \arrayrulewidth{}.

但是在打印 xtable 时如何制作 dotted/dashed 行?有人有解决办法吗?

add.to.row 就是您要找的。来自 print.xtable:

add.to.row: a list of two components. The first component (which should be called 'pos') is a list contains the position of rows on which extra commands should be added at the end, The second component (which should be called 'command') is a character vector of the same length of the first component which contains the command that should be added at the end of the specified rows. Default value is NULL, i.e. do not add commands.

不要忘记转义反斜杠,所以你必须写 \hdashline 而不是 \hdashline。换行符 \n 是可选的,但它使您的 TEX 代码更清晰。

\documentclass{article}
\usepackage{booktabs}
\usepackage{arydshln}
\begin{document}

<<mytable, results='asis', echo=F>>=
  library("xtable")
print(xtable(matrix(rnorm(32), 8,4), align="lllll"), 
      booktabs=T, 
      hline.after = c(-1,0,8), 
      add.to.row = list(pos=list(4), command="\hdashline \n"))
@

\end{document}

输出:

\begin{table}[ht]
\centering
\begin{tabular}{lllll}
  \toprule
 & 1 & 2 & 3 & 4 \ 
  \midrule
1 & -0.44 & 1.18 & -1.14 & -0.50 \ 
  2 & 1.71 & -0.24 & 1.06 & -0.21 \ 
  3 & -0.50 & -0.91 & 1.84 & 1.45 \ 
  4 & -1.64 & -1.68 & -0.70 & 1.25 \ 
   \hdashline 
5 & -2.03 & -0.81 & 0.35 & 1.12 \ 
  6 & 0.46 & 0.47 & -1.05 & -0.98 \ 
  7 & 0.45 & -0.05 & -0.79 & 0.17 \ 
  8 & 1.31 & -2.96 & -2.50 & 0.02 \ 
   \bottomrule
\end{tabular}
\end{table}