[R] FW: Matrix to Latex

Winfried Theis theis at statistik.uni-dortmund.de
Thu Sep 7 14:43:58 CEST 2000


Hello Bendix!

The attached function should do what you want. The function was written by
Torsten Hothorn (torsten.hothorn at rzmail.uni-erlangen.de). It does write a
matrix into a table (options for labels and caption included). 
-- Torsten: Did you write a newer version? Just asking... ;-)

Another possibility to do it is setting sep="&" and eol="\\\\" in write.table().
In this case you will have to set up all the latex surrounding yourself...

Cheers, Winfried 

----------------------------------
E-Mail: Winfried Theis <theis at statistik.uni-dortmund.de>
Date: 07-Sep-00
Tel.: +49-231-755-5903                  Room: Mathematikgebäude, M832
Dipl.-Math. Winfried Theis, Graduiertenkolleg "Angewandte Statistik",
Universität Dortmund, 44221 Dortmund
----------------------------------
-------------- next part --------------
m2l <- function(X, fname = "m2l.out", rowlabel = 0, collabel = 0, caption = " ")
{
	# Prints a matrix as latex table into a file
	
	# Input:	
	#		X:		a matrix
	#		fname:		a filename as String
	#		rowlabel:	a string array of labels, dim = nrow
	#		collabel:	a string array of labels, dim = ncol
	#		caption:	a string for caption field
				

	if (!(is.matrix(X)))
	{
		error("X is not a matrix");
		stop;
	}

	row <- nrow(X);
	col <- ncol(X);

	if (length(rowlabel) < 2)
	{
		rowlabel <- c(1:nrow);
	}
	if (length(collabel) < 2)
	{
		collabel <- c(1:ncol);
	}

	if ((length(rowlabel) != row) || (length(collabel) != col))
	{	
		error("wrong label dimensions");
		stop;
	}

	cat("\\begin{table}\n", file=fname);
	cat("\\begin{center}\n", file=fname, append=TRUE);
	cat("\\begin{tabular}{", file=fname, append=TRUE);
	

	for (j in c(1:(col+1)))
	{
		cat("|c", file=fname, append=TRUE);
	}
	cat("|} \\hline \n", file=fname, append=TRUE);
	
	for (j in c(1:col))
	{
		cat("&", file=fname, append=TRUE);
		cat(collabel[j], file=fname, append=TRUE);
	}
	cat("\\\\ \\hline \n", file=fname, append=TRUE);

	for (i in c(1:row))
	{
		cat(rowlabel[i], file=fname, append=TRUE);
		for (j in c(1:col))
		{
			cat("&", file=fname, append=TRUE);
			cat(X[i,j], file=fname,append=TRUE);
		}
		if (i < row)
		{
			cat("\\\\ \n", file=fname, append=TRUE);
		} else {
			cat("\\\\ \\hline \n", file=fname, append=TRUE);
		}
	}
	
	cat("\\end{tabular} \n", file=fname, append=TRUE);
	cat("\\caption{", file=fname, append=TRUE);
	cat(caption, file=fname, append=TRUE);
	cat("} \n", file=fname, append=TRUE);
	cat("\\end{center} \n", file=fname, append=TRUE);
	cat("\\end{table} \n", file=fname, append=TRUE);
}


	

	
			
	


More information about the R-help mailing list