[R] Writing a tabel in LaTeX-format

Joerg Kindermann Joerg.Kindermann at gmd.de
Thu Sep 7 15:48:24 CEST 2000


>>>>> " " == BXC (Bendix Carstensen) <BXC at novo.dk> writes:

     > I have a vague recollection of seeing a R-function that will output
     > a table or matix in a format suitable for the tabular enviroment in
     > LaTeX.

     > But I cannot find it. Any hints?

This is my 2 cent's worth - we hacked it here in the dept. some time ago.

--  Dr. Joerg Kindermann                        Knowledge Discovery Team
    GMD   - German National Research Center for Information Technology -
    	       phone: +49 02241 142437 fax: +49 02241 142342
			   http://ais.gmd.de/KD/
----------------------------------------------------------------------


mat2TeX <- function(mat, digits=4, rowNameTitle="", file="",
                    roundNum=NULL,
                    rowNameAlign="l",matAlign="r", prtHead=T, prtEnd=T,
                    extraTitle=NULL, rowNameCols=1, append=F) {
  nr <- nrow(mat);      nc <- ncol(mat)
  rn <- rownames(mat);  cn <- colnames(mat)
  cmat_array("",dim=dim(mat))
  if (!is.null(roundNum)) {
    if (length(roundNum)==nc){
      for(i in 1:nc) mat[,i]_round(mat[,i],roundNum[i])
    }  else {
      errif(length(roundNum!=1))
      mat_round(mat,roundNum)
    }
  }
  for(i in 1:nc){
    cmat[,i]_format(mat[,i], digits=digits)
  }

  if(is.null(cn))   cn <- 1:nc
  if(is.null(rn))   rn <- 1:nr
  if(rowNameTitle == "") 
    rowNameTitle <- paste(rep(" ",rowNameCols), collapse="|")

  cat("\n ", file=file, append=append)  # write new if necessary 

  if (length(rowNameAlign)==1) rowNameAlign_rep(rowNameAlign,rowNameCols)
  if (length(matAlign)==1) matAlign_rep(matAlign,nc)

  if (prtHead){
    cat("\\begin{tabular}[c]{",
        paste(c("",rowNameAlign, matAlign,""), collapse="|"),
        "}\\hline\n", sep="",
        file=file, append=T)
    if (!is.null(extraTitle))
      cat(extraTitle,"\\\\ \n",file=file, append=T)
    cat(  paste(c(rowNameTitle,cn), collapse=" & "),"\\\\ \\hline\n",
        file=file, append=T)
  }
  for(j in 1:nr){
    cat(paste(c(rn[j], cmat[j,]), collapse=" & "),
        "\\\\\n", file=file, append=T)
  }
  cat("\\hline\n", file=file, append=T)
  if (prtEnd) cat("\\end{tabular}\n", file=file, append=T)
}

-------------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list