[R] latex() etc.: How to nicely format a matrix for a LaTeX document?

Liviu Andronic landronimirc at gmail.com
Thu Dec 30 21:54:33 CET 2010


On Thu, Dec 30, 2010 at 9:48 PM, Marius Hofert <m_hofert at web.de> wrote:
> Thanks, John, I did.
>
> Here is an update [not all problems are solved; can they be?]:
>
> Consider:
>
> library(xtable)
> mat <- matrix(c(1,NA,3,100,10012.23423,4), ncol = 3, byrow = TRUE)
> print(xtable(mat, digits = 1), floating = FALSE, only.contents = TRUE,
>      include.rownames = FALSE, include.colnames = FALSE, hline.after = NULL)
>
> With this one obtains a clean output of the rows (maybe someone knows how to suppress the "% latex table generated..." that is displayed).
> However, as you can see from the output, the "&" symbols are not vertically aligned (that would be quite helpful in reading the table in a source file). This is even worse if you have row names of different lengths...
>
If you used Sweave, would you still need to read the  table code in a
source file? Regards
Liviu


> So my questions (2) and (3) are solved, but (1) remains. Is there any way to (maybe) first format the matrix entries to get the right alignment in xtable()?
>
> Cheers,
>
> Marius
>
> On 2010-12-30, at 21:29 , John Kane wrote:
>
>> 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.
>>>
>>
>>
>
> ______________________________________________
> 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.
>



-- 
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail



More information about the R-help mailing list