Create LaTex tables from R output

Creating reasonable layouted LaTeX tables from R output was easier then expected. I should have googled it long ago…


Lets say you created a tabular output in R called “tab1”, e.g by doing:
tab1 <- with(CO2, table(Treatment , Type))

your text output in R would look like

Treatment Quebec Mississippi
nonchilled 21 21
chilled 21 21

Now you would like this or whatever table or data.frame as a nice LaTeX-table, the only thing to do is:

and the output will be:

% latex table generated in R 2.9.0 by xtable 1.5-5 package
% Wed Jul 08 16:20:54 2009
& Quebec & Mississippi \\
nonchilled & 21 & 21 \\
chilled & 21 & 21 \\

If you are using Sweave the usage becomes
<< echo = FALSE , results = tex >>
with(CO2, xtable(table(Treatment , Type)))

and a the result of the R run is a LaTeX document. Another post will give a hint how to paste R graphics into the same Sweave or LaTeX document… later…

The full usage of xtable() is
xtable(x, caption=NULL, label=NULL, align=NULL, digits=NULL, display=NULL)
.. add table captions
xtable(table , caption = "My table caption")
and labels
xtable(table , label = " MyLaTeXlable")
to the LaTeX tables.


One thought on “Create LaTex tables from R output

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s