Skip to contents

(This function has been deprecated; see latexMatrix instead). This function provides a soft-wrapper to xtable::xtableMatharray() with additional support for fractions output and brackets.

Usage

matrix2latex(
  x,
  fractions = FALSE,
  brackets = TRUE,
  show.size = FALSE,
  digits = NULL,
  print = TRUE,
  ...
)

Arguments

x

a numeric or character matrix. If the latter a numeric-based arguments will be ignored

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 or a character in "p", "b", "B", "V". If TRUE, uses square brackets around the matrix, FALSE produces no brackets. Otherwise "p") uses parentheses, ( ); "b") uses square brackets [ ], "B") uses braces { }, "V") uses vertical bars | |.

show.size

logical; if TRUE shows the size of the matrix as an appended subscript.

digits

Number of digits to display. If digits == NULL (the default), the function sets digits = 0 if the elements of x are all integers

print

logical; print the LaTeX code for the matrix on the console?; default: TRUE

...

additional arguments passed to xtable::xtableMatharray()

Details

The code for brackets matches some of the options from the AMS matrix LaTeX package: \pmatrix{}, \bmatrix{}, \Bmatrix{}, ... .

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))
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \left[
#>  \begin{array}{rrrr}
#>   2 & 1 & -1 & 8 \\ 
#>   -3 & -1 & 2 & -11 \\ 
#>   -2 & 1 & 2 & -3 \\ 
#>   \end{array} \right]
matrix2latex(cbind(A,b), digits = 0)
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \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)
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \left[
#>  \begin{array}{llll}
#>   1 & 1/2 & -1/2 & 8 \\ 
#>   -3/2 & -1/2 & 1 & -11 \\ 
#>   -1 & 1/2 & 1 & -3 \\ 
#>   \end{array} \right]

matrix2latex(A, digits=0, brackets="p", show.size = TRUE)
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \left(
#>  \begin{array}{rrr}
#>   2 & 1 & -1 \\ 
#>   -3 & -1 & 2 \\ 
#>   -2 & 1 & 2 \\ 
#>   \end{array} \right)
#>  _{3 \times 3}

# character matrices
A <- matrix(paste0('a_', 1:9), 3, 3)
matrix2latex(cbind(A,b))
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \left[
#>  \begin{array}{llll}
#>   a_1 & a_4 & a_7 & 8 \\ 
#>   a_2 & a_5 & a_8 & -11 \\ 
#>   a_3 & a_6 & a_9 & -3 \\ 
#>   \end{array} \right]
b <- paste0("\\beta_", 1:3)
matrix2latex(cbind(A,b))
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> \left[
#>  \begin{array}{llll}
#>   a_1 & a_4 & a_7 & \beta_1 \\ 
#>   a_2 & a_5 & a_8 & \beta_2 \\ 
#>   a_3 & a_6 & a_9 & \beta_3 \\ 
#>   \end{array} \right]