I have a LaTeX table generated by Excel. I want to place this table but not as a LaTeX code, but rather as a xtable. It is possible to convert LaTeX to xtable?
[Tex/LaTex] How to use a table generated by Latex in R(xtable)
rxtable
Related Solutions
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
data(tli)
## Demonstrate aov
## Taken from help(aov) in R 1.1.1
## From Venables and Ripley (1997) p.210.
N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0)
P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0)
K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0)
yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5,55.0,
+ 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield)
npk.aov <- aov(yield ~ block + N*P*K, npk)
op <- options(contrasts=c("contr.helmert", "contr.treatment"))
npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
options(op)
Using xtable to produce the table
## Standard xtable use
print(xtable(summary(npk.aov)))
% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Fri Aug 12 16:00:48 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{lrrrrr}
\hline
& Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\
\hline
block & 5 & 343.29 & 68.66 & 4.45 & 0.0159 \\
N & 1 & 189.28 & 189.28 & 12.26 & 0.0044 \\
P & 1 & 8.40 & 8.40 & 0.54 & 0.4749 \\
K & 1 & 95.20 & 95.20 & 6.17 & 0.0288 \\
N:P & 1 & 21.28 & 21.28 & 1.38 & 0.2632 \\
N:K & 1 & 33.13 & 33.13 & 2.15 & 0.1686 \\
P:K & 1 & 0.48 & 0.48 & 0.03 & 0.8628 \\
Residuals & 12 & 185.29 & 15.44 & & \\
\hline
\end{tabular}
\end{center}
\end{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:
\documentclass{article}
\begin{document}
...
\end{document}
And you would insert your table code in the ...
spot. You then compile this with pdflatex
.
This produces the following table:
Problems with the standard xtable output
There are a number of problems with the standard xtable
output.
- It generates a floating
{table}
environment - It encloses the
tabular
in a{center}
environment - It uses standard
\hline
as a separator
Although 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:
## Revised R print command
print(xtable(summary(npk.aov)),floating=FALSE,latex.environments=NULL)
This produces the following LaTeX code:
% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Fri Aug 12 16:29:29 2011
\begin{tabular}{lrrrrr}
\hline
& Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\
\hline
block & 5 & 343.29 & 68.66 & 4.45 & 0.0159 \\
N & 1 & 189.28 & 189.28 & 12.26 & 0.0044 \\
P & 1 & 8.40 & 8.40 & 0.54 & 0.4749 \\
K & 1 & 95.20 & 95.20 & 6.17 & 0.0288 \\
N:P & 1 & 21.28 & 21.28 & 1.38 & 0.2632 \\
N:K & 1 & 33.13 & 33.13 & 2.15 & 0.1686 \\
P:K & 1 & 0.48 & 0.48 & 0.03 & 0.8628 \\
Residuals & 12 & 185.29 & 15.44 & & \\
\hline
\end{tabular}
If you have need for the table to float, then you should eliminate the floating=FALSE
argument from the command, but leave the latex.environments=NULL
.
However, this still leaves us with the cramped looking table.
Using booktabs
to produce the table
The gold standard for high quality tables is the booktabs
package, and xtable
provides a boolean to do exactly that.
Final R print command
print(xtable(summary(npk.aov)),floating=FALSE,latex.environments=NULL,booktabs=TRUE)
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 add latex.environments=NULL)
. In your LaTeX source, you will need to manually add \centering
right after \begin{table}
to center the table.
You can use \columncolor
macro:
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{|c|c|c|>{\columncolor{olive!50}}c|c|} %% Color 4th column
\hline
Username & Sample & Filename & Sequence & Comments \\ \hline
1& 2& 3& 4& 5\\
3& 5& 8& 9& 23\\
1& 2& 3& 4& 5\\
3& 5& 8& 9& 23\\
1& 2& 3& 4& 5\\
3& 5& 8& 9& 23\\
1& 2& 3& 4& 5\\
3& 5& 8& 9& 23\\\hline
\end{tabular}
\end{document}
Best Answer
Set by step: Suppose that you have a
file.xlsx
that look like:To convert to R, first export to
file.csv
:Then import as a data frame
mytable
in R:Or import directly from the
file.xlsx
(need Perl installed on your system):The data frame obtained must be the same in any case. Just in case that first column are really row names as above:
For convert the dataframe
mytable
as a xtable (that is, a chunk of LaTeX code):So you can copy & paste the ouput of R in your LaTeX file to obtain:
But the good thing of LaTeX+R is that you can make all the above automatically inserting a R chunk in your
file.tex
document (now is R noweb document, so save it asfile.Rnw
) and compile it usingSweave
orknitr
from the command line (see How to build Knitr document from the command line ) or simply making in Rstudio:File > New file > R Sweave > Write some like:
and finally, click on Compile PDF and you will obtain this
file.pdf
: