Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
just a minor comment: the answer below should also work with knitr, but you'd better use results='asis'. –  Yihui Sep 17 '12 at 18:37

1 Answer 1

up vote 1 down vote accepted

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)
@ 
\]
share|improve this answer
    
Thanks Aaron. It worked for me. Would you know how i can place the matrix with an equation this time. Calc = \frac{x1}{x2}\Sexpr{x1mmat} When this is rendered, is it possible to get the matrix here? Is that possible? –  R2D2 Sep 17 '12 at 14:50
    
I'm pretty sure Sexpr works by using cat, so you couldn't use Sexpr, but it should work fine to use the chunk method, as shown above. –  Aaron Sep 17 '12 at 15:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.