Last active
March 10, 2016 14:39
-
-
Save aabiddanda/2b67282aa15c06fe31e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to convert R matrices to LaTeX | |
mat2tex <- function(mat, mat.name=NA){ | |
mat.string <- "\\setcounter{MaxMatrixCols}{15}$$\n" | |
if (!is.na(mat.name)) { | |
mat.string <- paste(mat.string, mat.name, "=") | |
} | |
mat.string <- paste(mat.string, "\\begin{bmatrix} \n") | |
n <- dim(mat)[1] | |
m <- dim(mat)[2] | |
for (i in 1:n){ | |
mat.string <- paste(mat.string, mat[i,1]) | |
for (j in 2:m){ | |
mat.string <- paste(mat.string, mat[i,j], sep=" & ") | |
} | |
mat.string <- paste(mat.string, '\\\\ \n') | |
} | |
mat.string <- paste(mat.string, "\\end{bmatrix}$$\n") | |
return(mat.string) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "r2mat_test" | |
author: "Arjun Biddanda" | |
date: "February 5, 2016" | |
output: pdf_document | |
--- | |
## 1. A crude example of just printing out the matrix | |
```{r} | |
x <- diag(1,nrow=3,ncol=3) | |
x | |
``` | |
## 2. Calling from within a chunk | |
```{r, results='asis'} | |
source("../../src/mat2tex.R") | |
cat(mat2tex(diag(1,nrow=10,ncol=10), mat.name = "\\hat{X}")) | |
``` | |
\textbf{Note :} The maximum size that a matrix can be viably rendered within \LaTeX is $10 \times 10$. | |
I am currently working to make larger matrices have dots in them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment