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

后端 未结 3 799
轻奢々
轻奢々 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: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.

提交回复
热议问题