I have a LaTeX table generated like this,
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrrr}
\hline
& 1 & 2 & 3 & 4 & 5 & 6 \\
\hline
1 & 181.21 & 169.01 & 130.03 & 135.93 & 142.31 & 108.11 \\
2 & 106.80 & 130.71 & 186.43 & 126.46 & 167.28 & 137.91 \\
3 & 162.69 & 198.98 & 188.33 & 184.30 & 147.02 & 197.38 \\
4 & 132.42 & 150.50 & 148.69 & 134.31 & 129.52 & 172.72 \\
\hline
\end{tabular}
\end{center}
\end{table}
but when i compile it in MiKTeX I am getting the following error
! Undefined control sequence.
l.1 \begin
{table}[ht]
?
Process interrupted by user
I want a table generated from this i am compiling under pdfTeX. I am using LaTeX for first time, can anyone please give a suggestion?
Best Answer
Since you are generating the table from R, I'll assume that you are using R's
xtable
package to generate the table. This package has some deficiencies, but it is still quite usable.Here's a sample R session showing how to use the package.
R-session
Using xtable to produce the table
The output of this command is a fragment of LaTeX code, not a complete working document. It is designed to be cut and pasted into an existing LaTeX document. This is why you got the error that you got. Minimally, the document should look like the following:
And you would insert your table code in the
...
spot. You then compile this withpdflatex
.This produces the following table:
Problems with the standard xtable output
There are a number of problems with the standard
xtable
output.{table}
environmenttabular
in a{center}
environment\hline
as a separatorAlthough the floating table environment is useful when you are including the table into a larger LaTeX document, if you are generating a single table, it's not necessary. Furthermore, as a consequence of the floating environment,
xtable
encloses the table in a{center}
environment, which leaves too much extra space between the table and other elements of your document. Finally, it uses\hline
for the table rules, which leads to a cramped looking table.Most of these problems are solvable, although with some loss of auto-generation.
The first two problems are connected. The
print.xtable
function has two arguments, one to say whether the table should be floating or not, and another to define the environment to wrap the table in. So we can modify our initial command to print the table in the following way to suppress both of these:This produces the following LaTeX code:
If you have need for the table to float, then you should eliminate the
floating=FALSE
argument from the command, but leave thelatex.environments=NULL
.However, this still leaves us with the cramped looking table.
Using
booktabs
to produce the tableThe gold standard for high quality tables is the
booktabs
package, andxtable
provides a boolean to do exactly that.Final R print command
This produces the following, much nicer table: (you need to add
\usepackage{booktabs}
to the preamble of your LaTeX document.)If you want the table to float, (because you are including it in a larger LaTeX document, the you should eliminate the
floating=FALSE
argument, and addlatex.environments=NULL)
. In your LaTeX source, you will need to manually add\centering
right after\begin{table}
to center the table.