[Tex/LaTex] Automating LaTeX tables with specifications from R

rtables

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..

Best Answer

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