This function provides a soft-wrapper to xtable::xtableMatharray() with support for fractions output and square brackets.

matrix2latex(x, fractions = FALSE, brackets = TRUE, ...)

Arguments

x

a matrix

fractions

logical; if TRUE, try to express non-integers as rational numbers, using the fractions function; if you require greater accuracy, you can set the cycles (default 10) and/or max.denominator (default 2000) arguments to fractions as a global option, e.g., options(fractions=list(cycles=100, max.denominator=10^4)).

brackets

logical; include square brackets around the matrices?

...

additional arguments passed to xtable::xtableMatharray()

Author

Phil Chalmers

Examples

A <- matrix(c(2, 1, -1,
             -3, -1, 2,
             -2,  1, 2), 3, 3, byrow=TRUE)
b <- c(8, -11, -3)

matrix2latex(cbind(A,b))
#> \left[
#>  \begin{array}{rrrr}
#>   2.00 & 1.00 & -1.00 & 8.00 \\ 
#>   -3.00 & -1.00 & 2.00 & -11.00 \\ 
#>   -2.00 & 1.00 & 2.00 & -3.00 \\ 
#>   \end{array}
#> \right]
matrix2latex(cbind(A,b), digits = 0)
#> \left[
#>  \begin{array}{rrrr}
#>   2 & 1 & -1 & 8 \\ 
#>   -3 & -1 & 2 & -11 \\ 
#>   -2 & 1 & 2 & -3 \\ 
#>   \end{array}
#> \right]
matrix2latex(cbind(A/2,b), fractions = TRUE)
#> \left[
#>  \begin{array}{llll}
#>   1 & 1/2 & -1/2 & 8 \\ 
#>   -3/2 & -1/2 & 1 & -11 \\ 
#>   -1 & 1/2 & 1 & -3 \\ 
#>   \end{array}
#> \right]