Brett Klamer

Print R Matrices in LaTeX Math Environments

This is a quick and easy method to print R matrices in LaTeX math environments. It's based on the R function found here. (The newline at the end of the bmatrix should be removed)

Use this function

bmatrix = function(x, digits=NULL, ...) {
  default_args = list(include.colnames=FALSE, only.contents=TRUE,
                      include.rownames=FALSE, hline.after=NULL, comment=FALSE,
  passed_args = list(...)
  calling_args = c(list(x=xtable(x, digits=digits)),
                     default_args[setdiff(names(default_args), names(passed_args))]))
  return(cat("\\begin{bmatrix}\n",, calling_args),

And then insert the matrix as seen here

a =
<<results = 'asis', echo=FALSE>>=

Grab the .Rnw example file here. It should compile into something like this

R to LaTeX matrices

Last updated 2014-09-09 - Feel free to email me with any questions or comments.