3
votes

I wonder if there is a way to make some hlines in xtable dashed or dotted using command hline.after. I couldn't find a solution.

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}

gives this latex table code:

\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}

Instead of the solid \midrule after line 4, I'd like a dashed or, preferably, dotted line.

I know, in latex dashed horizontal lines can me made using arydshln package, also dotted hlines are possible manipulating \dashlinedash{}, \dashlinegap{} and \arrayrulewidth{}.

But how can I make dotted/dashed lines when printing an xtable? Does anyone have a solution?

1

1 Answers

4
votes

add.to.row is what you are looking for. From 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.

Do not forget to escape the backslash, so you have to write \\hdashline instad of \hdashline. The newline \n is optional but it makes your TEX code cleaner.

\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}

Output:

\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}

Table with dashed line.