Converting R matrix into LaTeX matrix in the math or equation environment

后端 未结 3 784
轻奢々
轻奢々 2020-12-15 12:38

Let\'s say there is an R matrix x:

x <- structure(c(2, 3, 5, 7, 9, 12, 17, 10, 18, 13), .Dim = c(5L,2L), .Dimnames = list(NULL, c(\"X1\", \"X         


        
相关标签:
3条回答
  • 2020-12-15 13:04

    if anyone needs rounded matrices with borders via \bordermatrix, I appended @Maxim.K's function to that end.

    m2l <- function(matr) {
        matr <- round(x = matr, digits = 2)  # sadly this is necessary because given this function, the options(digits = 2) does not work
        matr2 <- data.frame(c("~",rownames(matr)))  # add rownames
        for (r in colnames(matr)) {  # add col contents and colnames
          matr2 <- cbind(matr2, c(r, matr[,r]))
        }
        printmrow <- function(x) {
            ret <- paste(paste(x, collapse = " & "), "\\cr")
            sprintf(ret)
        }
        out <- apply(matr2, 1, printmrow)
        out2 <- paste("\\bordermatrix{", paste(out, collapse = ' '),"}")
        return(out2)
    }
    

    Pretty hideous code, I know, but get's the job done.

    Maybe this'll be useful for someone out there.

    Can look nice, especially for correlation matrices and such stuff:

    tex

    0 讨论(0)
  • 2020-12-15 13:09

    For a future reference, here is the function that I wrote myself later:

    matrix2latex <- function(matr) {
    
    printmrow <- function(x) {
    
        cat(cat(x,sep=" & "),"\\\\ \n")
    }
    
    cat("\\begin{bmatrix}","\n")
    body <- apply(matr,1,printmrow)
    cat("\\end{bmatrix}")
    }
    

    It doesn't require an external package. For some reason the apply produced NULL at the end of the output (the actual return?). This was solved by assigning the return to the body variable, which is otherwise of no use. The next task is to render the output of that function in LaTeX within knitr.

    0 讨论(0)
  • 2020-12-15 13:15

    You can use the xtable packages print.xtable method with a simple wrapper script to set some default args.

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

    Seems to do what you are looking for

    x <- structure(c(2, 3, 5, 7, 9, 12, 17, 10, 18, 13), .Dim = c(5L,2L), .Dimnames = list(NULL, c("X1", "X2")))
    
    bmatrix(x)
    ## \begin{bmatrix}
    ##   2.00 & 12.00 \\ 
    ##   3.00 & 17.00 \\ 
    ##   5.00 & 10.00 \\ 
    ##   7.00 & 18.00 \\ 
    ##   9.00 & 13.00 \\ 
    ##    \end{bmatrix}
    

    And to use no decimal places like your example.

    bmatrix(x, digits=0)
    ## \begin{bmatrix}
    ##   2 & 12 \\ 
    ##   3 & 17 \\ 
    ##   5 & 10 \\ 
    ##   7 & 18 \\ 
    ##   9 & 13 \\ 
    ##    \end{bmatrix}
    
    0 讨论(0)
提交回复
热议问题