[Tex/LaTex] Sweave and Matrix

knitrstatisticssweave

I am using xtable to generate Matrices

x1mean <- matrix(c(204.5, 556.5), 2)
x2mean <- matrix(c(130.0, 355.0), 2)
x1mmat <- xtable(x1mean, align = rep( "", ncol(x1mean) + 1) )
x2mmat <- xtable(x2mean, align = rep( "", ncol(x2mean) + 1) )
print(x1mmat, floating=FALSE, tabular.environment="bmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE)
print(x2mmat, floating=FALSE, tabular.environment="bmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE)

And it does generate beautiful matrix but I am unable to find a way to place the Matrix variable beside it and that too for more than one matrices. Something like:

    X = []  Y = []  
    Z = []

where X, Y and Z are matrix variables. They may have bar (denoting mean) over them, could be bold and the matrices themselves might be nXm dimension. In addition when I print, I have to place the print statement in equation/math environment otherwise I get errors from Sweave. This also has a limitation that the following sequence would not work:

  1. Write X in latex, open math environment, print matrix in R environment, close math environment
  2. Write Y in latex, open math environment, print matrix in R environment, close math environment

I want to avoid hard-coding the matrix and I would like to use the same matrix variables that I use in computing other results such as covariance, correlation etc. Essentially I am trying to describe my input which are matrix variables and then describe my calculation and finally place the result. I am trying to do all of this with variables.

Is this possible? If not Sweave, does knitr have these features? I have heard about knitr but have not used it but if knitr has a solution for this I am happy to learn it.

FYI, my environment are the very latest release versions of RStudio and R.

EDIT

Thanks Aaron for your help. Here is function to generate latex code for print matrices:

<<echo=False>>=
x1mean <- matrix(c(204.5, 556.5), 2)
@

Some blah.. blah...

<<echo=False, results=tex>>=
  matgen <- function(mat) {
  mattable <- table(mat, align = rep("", ncol(mat) + 1) )
  mattex <- print(x1mmat, floating = FALSE, tabular.environment = "bmatrix", print.results=FALSE, hline.after = NULL, include.rownames = FALSE, include.colnames = FALSE)
  mattex <- gsub("\\", "\\\\", mattex, fixed=TRUE)
  return (mattex)
}
@

$\Sexpr{matgen(x1mean)}$

$% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Tue Sep 18 11:41:40 2012
\begin{bmatrix}{}
  204.50 \\
  556.50 \\
  \end{bmatrix}
$

One thing that it is still holding this is the comments generated by xtable. I wonder why the author insisted on keeping comments.

Best Answer

Your suggestions are very close; you just want to open the math environment before writing the X or the Y. The thing to remember is that Sweave will simply replace the code chunk with the results of that code chunk, so you just need to decide how you want your LaTeX code to look and then replace the appropriate part with the Sweave chunk. It's also sometimes easier to output LaTeX code directly from the Sweave chunk by using cat (If you need a backslash you do need to double it though.) The below example shows both methods; the result is two matrices next to each other.

\[A=
<<results=tex, echo=FALSE>>=
print(x1mmat, floating=FALSE, tabular.environment="bmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE)
@ 
<<results=tex, echo=FALSE>>=
cat("\\hat B=")
print(x2mmat, floating=FALSE, tabular.environment="bmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE)
@ 
\]