Converting R matrix into LaTeX matrix in the math or equation environment

Viewed 6836

Let's say there is an R matrix x:

x <- structure(c(2, 3, 5, 7, 9, 12, 17, 10, 18, 13), .Dim = c(5L,2L), .Dimnames = list(NULL, c("X1", "X2")))

I am writing a report featuring LaTeX equations, using the Markdown-pandoc-LaTeX workflow. x is one of the matrices that need to appear in these equations. Is it possible to programmatically render the LaTeX respresentation of the matrix as follows?:

\begin{bmatrix}
2 & 12\\ 
3 & 17\\ 
5 & 10\\ 
7 & 18\\ 
9 & 13
\end{bmatrix}

Ideally the report code would be something in the lines of:

\begin{displaymath}
\mathbf{X} = `r whatever-R-code-to-render-X`
\end{displaymath}

but this is probably cumbersome, so I will surely settle for the simple transformation.

3 Answers
Related