[R] latex() etc.: How to nicely format a matrix for a LaTeX document?
m_hofert at web.de
Thu Dec 30 17:50:47 CET 2010
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:
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:
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:
(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.
More information about the R-help