Wave2LaTeX

Average rating
(1 vote)

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

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;ic<nt;ic+=1)
		theWaves[ic] = StringFromList(ic,waves)
		theLengths[ic] = numpnts($theWaves[ic])
	endfor
 
	maxl = wavemax(theLengths)
	//Add the header, include centering option c, l or r and insert vertical lines with | as needed 
	theTable ="\begin{tabular}{"
	for(ic=0;ic<nt;ic+=1)
		theTable+="c "
	endfor
	theTable+="}"
	sprintf theTable, "%s\r", theTable
 	for(ic=0;ic<maxl;ic+=1)
		theText = ""
		for(jc=0;jc<nt-1;jc+=1)
			if(wavetype($theWaves[jc],1)==1)
				wave theOne = $theWaves[jc]
				if (numpnts(theOne)>maxl)
					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

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

Back to top