Wave2LaTeX

This string function returns values from numeric waves into a simple LaTeX table format.


// Create a LaTeX table from waves
// Version 2
// Operates in current data folder and returns theTable as a string
// output is in column format, one wave per column
// waves can be numeric or string
// ==> waves: a semicolon list of wave names to tabulate
// ==> rowend: (optional) string to add to the end of each row
 
Function/S Wave2LaTeX(waves,[rowend])
	string waves, rowend
 
 	// strings and variables
 	
	string theTable = "", theText = ""	
	variable ic, nt, maxl, jc, theValue
 
 	// default rowend parameter
 	
	if (ParamIsDefault(rowend))
		rowend = ""
	endif
 
 	// how many columns to create
 	
	nt = ItemsInList(waves)
	
	// create free waves to translate inputs
 
	make/T/n=(nt)/FREE theWaves
	make/n=(nt)/FREE theLengths
	
	// put wave names in to text wave storage
	// put lengths of waves in to wave storage
 
	for(ic=0;ic<nt;ic+=1)
		theWaves[ic] = StringFromList(ic,waves)
		theLengths[ic] = numpnts($theWaves[ic])
	endfor
	
	// how many rows do we need?
 
	maxl = wavemax(theLengths)
 
 	// proccess each row
 	
	for(ic=0;ic<maxl;ic+=1)
		theText = ""
		
		// do up to the last column
		
		for(jc=0;jc<nt-1;jc+=1)
			switch(wavetype($theWaves[jc],1))
				case 1:	// numeric wave
					wave theOne = $theWaves[jc]
					if (numpnts(theOne)>maxl)
						sprintf theText, "%s\t&", theText
					else
						sprintf theText, "%s%g\t&", theText, theOne[ic]
					endif
					break
				case 2:	// text wave
					wave/T theTOne = $theWaves[jc]
					if (numpnts(theTOne)>maxl)
						sprintf theText, "%s\t&", theText
					else
						sprintf theText, "%s%s\t&", theText, theTOne[ic]
					endif
					break
			endswitch
			
		endfor
		
		// add the last column
		
		switch(wavetype($theWaves[nt],1))
			case 1:	// numeric wave
				wave theOne = $theWaves[nt]
				if (numpnts(theOne)>maxl)
					sprintf theText, "%s \\\\ %s\r", theText, rowend
				else
					sprintf theText, "%s%g \\\\ %s\r", theText, theOne[ic], rowend
				endif
				break
			case 2:	// text wave
				wave/T theTOne = $theWaves[nt]
				if (numpnts(theTOne)>maxl)
					sprintf theText, "%s \\\\ %s\r", theText, rowend
				else
					sprintf theText, "%s%s \\\\ %s\r", theText, theTOne[ic], rowend
				endif
				break
		endswitch
		
		theTable += theText
	endfor
	
	return theTable
end
Upgrade but Pb with wavelist

This is great, I modified to support replace NaN by " " and deal with text waves as well (see below). However, I ran into a problem with the macro using wavelist to find the waves in a given table. It seems that the list is not ordered according to the column order. How could I correct that to make this export to LaTex better ?


macro TabletoLatex()
	print WaveList("*",";","WIN:")
	print Wave2LaTeX(WaveList("*",";","WIN:"))
	PutScrapText Wave2LaTeX(WaveList("*",";","WIN:")) 
end


// Create a LaTeX table from waves
//This string function returns values from numeric waves into a simple LaTeX table format.
// Operates in current data folder and returns theTable as a string
// ==> waves: a semicolon list of wave names to tabulate
// ==> rowend: (optional) string to add to the end of each row
Function/S Wave2LaTeX(waves,[rowend]) //jjweimer on IgorForum
	string waves, rowend
	string theTable = "", theText = ""	
	variable ic, nt, maxl, jc, theValue,WT
 	if (ParamIsDefault(rowend))
		rowend = ""
	endif
 	nt = ItemsInList(waves)
 	make/T/n=(nt)/FREE theWaves
	make/n=(nt)/FREE theLengths
 	for(ic=0;icmaxl)
					sprintf theText, "%s\t&", theText
				else
					if((numtype( theOne[ic])==2))
						sprintf theText, "%s%s \t& ", theText," " 
					else
						sprintf theText, "%s%g\t& ", theText, theOne[ic]
					endif
				endif
			elseif(wavetype($theWaves[jc],1)==2)
		wave/t  theOneS=$theWaves[jc]
				if (numpnts(theOneS)>maxl)
					sprintf theText, "%s\t&", theText
				else
					sprintf theText, "%s %s \t & ", theText, theOneS[ic]
				endif
			endif
		endfor
		if(wavetype($theWaves[jc],1)==1)
			wave theOne =  $theWaves[jc]
			if (numpnts(theOne)>maxl)
				sprintf theText, "%s \\\\ %s\r", theText, rowend
			else
				if((numtype( theOne[ic])==2))
					sprintf theText, "%s%s\\\\ %s\r", theText," " , rowend
				else
	 				sprintf theText, "%s%g\\\\ %s\r", theText, theOne[ic], rowend
				endif
			endif
		elseif(wavetype($theWaves[jc],1)==2)
			 wave/t  theOneS=$theWaves[jc]
			if (numpnts(theOneS)>maxl)
				sprintf theText, "%s \\\\ %s\r", theText, rowend
			else
				sprintf theText, "%s%s \\\\ %s\r", theText, theOneS[ic], rowend
			endif
		endif
		theTable += theText
	endfor
	theTable+="\end{tabular}"
	return theTable
end

<\igor>
Thank you. The modified version supports string waves too.

I do not include the \begin{...} ... \end{..} sections marks. They are for individuals to make on their own.

I do not change NaN or INF designations. The %g format gives them directly in a way that can be altered later via a blanket find/replace on the text editor.

As to the other question about the order of waves from tables to string lists ... I do not have an immediate answer other than that WaveList has to be properly configured in how it reads the table. This is not a problem to handle inside this code segment.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville

Here is a slight tweak of Wave2LaTex!

This snippet takes a 2D wave with x and y dimension labels, i.e.

make/O/N=(3,3) data = abs(round(enoise(100)))
setdimlabel 1, 0, X, data
setdimlabel 1, 1, Y, data
setdimlabel 1, 2, Z, data
setdimlabel 0, 0, run1, data
setdimlabel 0, 1, run2, data
setdimlabel 0, 2, run3, data
edit data.ld

 

and then makes a table from it, including the text labels. The LaTex code is directly put to Clipboard from where it can be pasted in to a text editor. Not extensively tested!!

function/S Matrix2LaTeX(w2d)
	// w2d is expected to have x and y dimension labels
	wave w2d
	
	variable rows = DimSize(w2d,0) 
	variable cols = DimSize(w2d,1) 
	variable i, j
	
	// make a blank line
	print "\r"
	
	// initiate the table
	String theTable ="\begin{tabular}{"
	for(j=0; j < cols+1; j+=1)
       theTable += "c "
   endfor
	theTable+="}\r"
	// add some formatting
	theTable+="\hline\r"
	
	// the first "row" consists of column headers, y dim labels
	// the first column header remains empty
	theTable += " &"
	for(j=0; j < cols; j+=1)
		if (j < cols-1)
     		theTable += GetDimLabel(w2d, 1, j) +" &"
     	else
     		theTable += GetDimLabel(w2d, 1, j) +" \\\ \r"	
     	endif
   endfor
	
	// add some formatting
	theTable+="\hline\r"
	
	// now add the data
	for(i=0; i < rows; i+=1)
		// the first table column consists of x dimension labels
		theTable += GetDimLabel(w2d, 0, i) +" &"
   
   	// now data follows
		for(j=0; j < cols; j+=1)
			if (j < cols-1)
      		theTable += num2str(w2d[i][j]) +" &"
      	else
      		theTable += num2str(w2d[i][j]) +" \\\ \r"	
      	endif 		
   	endfor
	endfor
	
	// do some formatting and close table	
	theTable+="\hline\r"
	theTable+="\end{tabular}"
	
	// put table to clipboard
	PutScrapText theTable
	
	return theTable
end

 

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More