Marius Hofert
2010-Dec-30 16:50 UTC
[R] latex() etc.: How to nicely format a matrix for a LaTeX document?
Dear (T)eXpeRts, I try to create a LaTeX table from an R matrix for the first time. I am not sure what the "best" approach is, I just read about latex() from Hmisc (toLatex() didn't work). Consider the following minimal example: library(Hmisc) mat <- matrix(c(1,NA,3,100,10000,4), ncol = 3, byrow = TRUE) latex(mat, file = "", booktabs = TRUE, numeric.dollar = FALSE, table.env = FALSE) I am only interested in the part between \midrule and \bottomrule [I couldn't figure out how to remove the tabular environment]. It looks like this: 1&&3\tabularnewline 100&10000&4\tabularnewline My questions/problems are: (1) if there are NA's, you can see that the output is not aligned according to the &-symbols. That makes it hard to read in a LaTeX *source* file. How can I get something like: 1& &3\tabularnewline 100&10000&4\tabularnewline (2) it would even be nicer to read if the output was like this: 1 & & 3 \tabularnewline 100 & 10000 & 4 \tabularnewline How can I achieve this? (3) is there another package/function to get output like this more easily? I wrote a one-liner which formats the lines separately, but it would be nice to have the columns aligned as given in (2). I believe it is most helpful to have the output in a form which is readable in a *source* file (i.e., .tex), since the headers/footers from tabular [or tabularx etc.] are often easy to put in the document. Cheers, Marius
John Kane
2010-Dec-30 20:29 UTC
[R] latex() etc.: How to nicely format a matrix for a LaTeX document?
Have a look at xtable. --- On Thu, 12/30/10, Marius Hofert <m_hofert at web.de> wrote:> From: Marius Hofert <m_hofert at web.de> > Subject: [R] latex() etc.: How to nicely format a matrix for a LaTeX document? > To: "Help R" <r-help at stat.math.ethz.ch> > Received: Thursday, December 30, 2010, 11:50 AM > Dear (T)eXpeRts, > > I try to create a LaTeX table from an R matrix for the > first time. I am not sure what the "best" approach is, I > just read about latex() from Hmisc (toLatex() didn't work). > > Consider the following minimal example: > > library(Hmisc) > mat <- matrix(c(1,NA,3,100,10000,4), ncol = 3, byrow > TRUE) > latex(mat, file = "", booktabs = TRUE, numeric.dollar > FALSE, table.env = FALSE) > > I am only interested in the part between \midrule and > \bottomrule [I couldn't figure out how to remove the tabular > environment]. It looks like this: > > ? 1&&3\tabularnewline > 100&10000&4\tabularnewline > > My questions/problems are: > > (1) if there are NA's, you can see that the output is not > aligned according to the &-symbols. That makes it hard > to read in a LaTeX *source* file. How can I get something > like: > ? 1&? > ???&3\tabularnewline > 100&10000&4\tabularnewline > > (2) it would even be nicer to read if the output was like > this: > ? 1 &? ? ???& 3 > \tabularnewline > 100 & 10000 & 4 \tabularnewline > How can I achieve this? > > (3) is there another package/function to get output like > this more easily? I wrote a one-liner which formats the > lines separately, but it would be nice to have the columns > aligned as given in (2). I believe it is most helpful to > have the output in a form which is readable in a *source* > file (i.e., .tex), since the headers/footers from tabular > [or tabularx etc.] are often easy to put in the document. > > Cheers, > > Marius > > ______________________________________________ > R-help at r-project.org > mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >