latexMatrix()
# return value
mat <- latexMatrix()
str(mat)
cat(getLatex(mat))
# copy to clipboard (can't be done in non-interactive mode)
if (FALSE) {
clipr::write_clip(mat)
}
# can use a complex symbol
latexMatrix("\\widehat{\\beta}", 2, 4)
# numeric rows/cols
latexMatrix(ncol=3)
latexMatrix(nrow=4)
latexMatrix(nrow=4, ncol=4)
# diagonal matrices
latexMatrix(nrow=3, ncol=3, diag=TRUE)
latexMatrix(nrow="n", ncol="n", diag=TRUE)
latexMatrix(nrow="n", ncol="n", diag=TRUE, sparse=TRUE)
# commas, exponents, transpose
latexMatrix("\\beta", comma=TRUE, exponent="-1")
latexMatrix("\\beta", comma=TRUE, transpose=TRUE)
latexMatrix("\\beta", comma=TRUE, exponent="-1", transpose=TRUE)
# for a row/column vector, wrap in matrix()
latexMatrix(matrix(LETTERS[1:4], nrow=1))
latexMatrix(matrix(LETTERS[1:4], ncol=1))
# represent the SVD, X = U D V' symbolically
X <- latexMatrix("x", "n", "p")
U <- latexMatrix("u", "n", "k")
D <- latexMatrix("\\lambda", "k", "k", diag=TRUE)
V <- latexMatrix("v", "k", "p", transpose = TRUE)
cat("\\mathrm{SVD:}\n", getLatex(X), "=\n", getLatex(U),
getLatex(D), getLatex(V))
# supply a matrix for 'symbol'
m <- matrix(c(
"\\alpha", "\\beta",
"\\gamma", "\\delta",
"\\epsilon", "\\pi",
0 , 0), 4, 2, byrow=TRUE)
latexMatrix(m)
# Identity matrix
latexMatrix(diag(3))
latexMatrix(diag(3), sparse=TRUE)
# prefix / suffix
latexMatrix(prefix="\\sqrt{", suffix="}")
latexMatrix(suffix="^{1/2}")
# show size (order) of a matrix
latexMatrix(show.size=TRUE)
latexMatrix(nrow=3, ncol=4, show.size=TRUE)
# handling fractions
m <- matrix(3/(1:9), 3, 3)
latexMatrix(m)
latexMatrix(m, digits=2)
latexMatrix(m, fractions=TRUE)
# zero-based indexing
latexMatrix(zero.based=c(TRUE, TRUE))
# partitioned matrix
X <- latexMatrix(nrow=5, ncol=6)
partition(X, rows=c(2, 4), columns=c(3, 5))
# binding rows and columns; indexing
X <- latexMatrix("x", nrow=4, ncol=2)
Y <- latexMatrix("y", nrow=4, ncol=1)
Z <- latexMatrix(matrix(1:8, 4, 2))
cbind(X, Y, Z)
rbind(X, Z)
X[1:2, ]
X[-(1:2), ]
X[1:2, 2]
# defining row and column names
W <- latexMatrix(rownames=c("\\alpha_1", "\\alpha_2", "\\alpha_m"),
colnames=c("\\beta_1", "\\beta_2", "\\beta_n"))
W
Rownames(W) <- c("\\mathrm{Abe}", "\\mathrm{Barry}", "\\mathrm{Zelda}")
Colnames(W) <- c("\\mathrm{Age}", "\\mathrm{BMI}", "\\mathrm{Waist}")
W
Run the code above in your browser using DataLab