to.latex <- function(x){ gsub("\\\\","\\\\\\\\",latexSN(x)) } cleanup.tolatex <- function(output) { gsub("\\\\textrm\\{e\\}(-?)0*(\\d+)","$\\\\times 10^{\\1\\2}$",output); } table.to.latex <- function(table, digits=NULL, format=NULL, scientific=NULL, nsmall=0, colspec="r", useBooktabs=TRUE, toprule=if(useBooktabs) "\\toprule" else "\\hline\\hline", midrule=if(useBooktabs) "\\midrule" else "\\hline", bottomrule=if(useBooktabs) "\\bottomrule" else "\\hline\\hline",...) { ans <- "" header <- c("",colnames(table)); res <- rbind(header, cbind(rownames(table), apply(table,c(1,2),function(x){format(x,digits=digits,nsmall=nsmall,scientific=scientific,...)})#format(table,digits=digits,scientific=scientific) )) coefrows <- 2:NROW(res) res[coefrows,-1] <- sub("(\\*+)","^{\\1}",res[coefrows,-1]) res[coefrows,-1] <- sub("([eE])\\+?(-?)0*([0-9]+)","$\\\\times 10^{\\2\\3}$", res[coefrows,-1]) tabspec <- rep("l",ncol(res)) tabspec[-1] <- colspec tabbegin <- paste("\\begin{tabular}{",paste(tabspec,collapse=""),"}",sep="") tabend <- "\\end{tabular}" ans <- c(ans,tabbegin) if(length(toprule)) ans <- c(ans,toprule) for(j in 2:ncol(res)) ans <- c(ans,paste(" & \\multicolumn{1}{c}{",trimws(res[1,j]),"}",sep="")) ans <- c(ans,"\\\\") if(length(midrule)) ans <- c(ans,midrule) for(i in 2:NROW(res)) { ans <- c(ans, paste(paste(res[i,],collapse=" & "),"\\\\")) } if(length(bottomrule)) ans <- c(ans,bottomrule) ans <- c(ans,tabend) structure(ans,class="Latex") }