# Automating LaTeX tables with specifications from R

I have some latex tables generated from R-code, the sample latex table code is below

\begin{table}[ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c}
\hline
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\
\hline
1 & 186.30 & 101.52 & 128.33 & 148.02 & 132.08 & 138.63 & 107.98 & 133.09 & 134.12 &   119.23 & 133.21 & 165.73 \\
2 & 104.03 & 184.67 & 184.07 & 169.27 & 117.85 & 107.67 & 169.89 & 111.93 & 185.41 &  134.30 & 198.12 & 120.83 \\
3 & 170.20 & 189.47 & 142.98 & 106.06 & 116.85 & 120.74 & 100.51 & 195.18 & 196.31 & 189.16 & 180.00 & 191.78 \\
4 & 176.23 & 187.77 & 127.99 & 194.66 & 135.87 & 152.42 & 137.35 & 109.88 & 162.07 & 171.81 & 136.07 & 122.29 \\
\hline
\end{tabular}
\end{center}
\end{table}


I will have about 50 or 60 tables generated like this. I want to input '\hdashline' after every row in a table and want to separate the tables by '\vspace{2em}' How Can I do this automatically from R code fro every table as I have hell lot of tables, I could not find a function to write these in x-table..Any help is appreciated..

• Maybe link to the R code that generates these tables? This seems like more of an R question, really. Maybe an R specific forum would be better? – Faheem Mitha Aug 19 '11 at 19:49

@Alex: Regarding the spacing between tables -- I take it there's no text above, between, or below the tables, right? -- the following command (to be placed in the preamble) will get the job done:

\setlength\floatsep{2em}


You should also replace all of the \begin{center} ... \end{center} pairs with a single \centering instruction (to occur immediately before \begin{tabular}...) in order to eliminate the extra spacing that the pair of instructions inserts. Next, consider changing the [ht] placement directive with [h].

On the automatic insertion of the command \hdashline (I must confess I'm not familiar with it): I'd say that this is (i) something that you should program up in R, to print out along with the remaining contents of each table, or (ii) do a global search for \\ and replace it with \\ \hdashline, followed by a related search-and-replace for instances of clashes between \hline and hdashline.

Speaking from a purely aesthetic point of view, though, I would recommend that you not add all these extra (dashed?) horizontal lines at all, since IMHO they're not likely to enhance the readability of the tables. Moreover, you may even want to consider eliminating all of the vertical lines as well. The code below contrasts the two approaches: the first table has lots of vertical and horizontal rules, the second has very few of them. (What I've done is to reproduce a shortened version of your sample table.) The second table also uses the rule-drawing commands of the booktabs package, instead of LaTeX's \hline command, to achieve more "breathing space" between the rules and rows of text/numbers.

\documentclass[letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{booktabs,lscape}
\setlength{\floatsep}{2em}

\begin{document}
\begin{landscape}

\begin{table}[t]
\caption{Table with lots of rules''} \label{tab:manyrules}
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\
\hline
1 & 186.30 & 101.52 & 128.33 & 148.02 & 132.08 & 138.63 & 107.98 & 133.09 & 134.12 &   119.23 & 133.21 & 165.73 \\ \hline
2 & 104.03 & 184.67 & 184.07 & 169.27 & 117.85 & 107.67 & 169.89 & 111.93 & 185.41 &  134.30 & 198.12 & 120.83 \\
\hline
\end{tabular}
\end{center}
\end{table}

\begin{table}[h]
\caption{A table with very few rules''}
\label{tab:fewrules}
\centering
\begin{tabular}{*{14}{c}}
\midrule[\heavyrulewidth]
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\
\midrule
1 & 186.30 & 101.52 & 128.33 & 148.02 & 132.08 & 138.63 & 107.98 & 133.09 & 134.12 &   119.23 & 133.21 & 165.73 \\
2 & 104.03 & 184.67 & 184.07 & 169.27 & 117.85 & 107.67 & 169.89 & 111.93 & 185.41 &  134.30 & 198.12 & 120.83 \\
\bottomrule
\end{tabular}
\end{table}

\end{landscape}
\end{document}


The simplest suggestion would be to use the xtable R package and the add.to.row argument:

dat <- data.frame(A = 1:10, B = runif(10))
xdat <- xtable(dat)
command = "\\hdashline \n")


Here, addtorow$pos is a list containing the indices 1, 2, ..., 9 which means after rows 1, 2, ... 9, include the latex function/command listed in string addtorow$command.

The above code produces:

> print(xdat, add.to.row = addtorow)
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Fri Aug 19 21:51:21 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& A & B \\
\hline
1 &   1 & 0.94 \\

\hdashline
2 &   2 & 0.50 \\

\hdashline
3 &   3 & 0.86 \\

\hdashline
4 &   4 & 0.43 \\

\hdashline
5 &   5 & 0.34 \\

\hdashline
6 &   6 & 0.41 \\

\hdashline
7 &   7 & 0.30 \\

\hdashline
8 &   8 & 0.38 \\

\hdashline
9 &   9 & 0.74 \\

\hdashline
10 &  10 & 0.98 \\
\hline
\end{tabular}
\end{center}
\end{table}


Take a look at the help for ?xtable and ?print.xtable for info on controlling other options.

• thanks for the suggestion, but I have lot of tables,so I tried to put the addtorow statement in a for loop and it is a list but is not working for(ii in seq_along(xres)) { addtorow[ii]<-list(pos=list(seq_len(nrow(xres[[ii]]-1))),command="\\hdashline \n") } – alex Aug 19 '11 at 22:19
• Now, this is an R question and not a LaTeX question. – Alan Munn Aug 19 '11 at 22:33