\hat{x}

Hi,

I try to use a symbols in igot graphs. I want to have a circumflex over some characters, similar what $\hat{x}$ would do in latex. I tried the unicode symbol for hat "-x̂ . It works well inside igor, as you can see in the attached first image, but the letter is corrupted when exporting it to EPS. Please look at the second image.

I am indeed very excited about the Igor Tex support. I tried "\\$WMTEX$\hat{x}\\$/WMTEX$". Unfortunately it does not work either.

Any idea?

Best regards, Joerg.

 

The unicode hat symbol works inside igor The unicode hat symbol is corrupted in the EPS file

This may depend on the font you use, your operating system and your Igor version, but, in general...

The combining circumflex character is U+0302 in Unicode and "\xCC\x82" in UTF-8.

The garbling you see occurs when you use a character that is not available in the system text encoding (e.g., MacRoman on Macintosh, Windows-1252 on Windows) and you the EPS does not have an embedded outline for the character. This will happen if:

1. The font you use does not support the character

2. The font you use supports the character but it is a "Standard" font and you have not specified to embed it.

So, in this case, I think it should work if you uncheck the "Don't embed standard fonts" checkbox in the Save Graphics dialog.

This works for me in Igor Pro 8 on Macintosh using Helvetica font:

Display /W=(54,70,389,345)
TextBox/C/N=text0/A=MC "\\F'Helvetica'\\Z72x̂"
SavePICT/O/E=-3 as "EPS with Standard Fonts Embedded.eps"

Viewing in Preview, it appears correct, though the circumflex is shifted slightly to the left.

If I execute the same code on Windows and then view it in Preview on Macintosh, I get the right characters but the circumflex is shifted considerably to the right. I got the same shifted result with Helvetica, Arial, Tahoma, and Times New Roman. I also got similar results using Igor PDF. I will bring this up with our graphics expert.

I got good results on Windows using Microsoft Print to PDF, except for Times New Roman where the circumflex was shifted and cut off.

 

 

 

 

One option is to create the LaTeX representation of the equation externally, save it as an image, and embed it where needed. The good news is that one _can_ include PICTURES in labels and annotations. As my time permits, I might generate an example on macOS using LaTeXiT.

Here is the example. I did this ...

  • Open LaTeXiT
  • Generate the desired equation for the label ... x - \hat{x}\ (\mathrm{Pix})
  • Save the equation image as PDF (xaxis.pdf)
  • Load the xaxis.pdf file as a picture in Igor Pro
  • Go to the axis label dialog
  • Select picture and choose the xaxis.pdf picture

I note, the size of the label picture stays fixed as the graph window size is changed.

Advanced Users: Suppose that you are including the graphic from a PDF into a LaTeX-generated document (i.e. using `\includegraphics{...}`). I might wonder whether the fonts for labels and annotations are accessible (by tikz or whatever). I wonder this because, I have seen a posting on StackExchange that asks specifically how to change fonts on PDF files that are brought into the document.

Hi,

I am on a Win 10 machine. Just for you to know.

Howard's solution worked as soon, as I usde the /EF=2  option in SavePICT/O/E=-3. As Howard wrote, the circumflex is not quite on top of the "x".

I very much like Jeffrey's approch, because once we can include Latex, everything should be possible and appear great. I created the following Latex document

\documentclass[12pt]{minimal}
\begin{document}
	$x - \hat{x}\ (\mathrm{Pix})$
\end{document}

I used latex.exe -synctex=1 %.tex to create DVI and dvips.exe -E -o %.eps %.dvi to create EPS. The EPS file has been checked with Inkscape and appears to be ok. Sorry, I cannot provide this EPS file here, because there is no upload for EPS. The EPS has then been loaded to Igor using

LoadPict/O Formula

The load appears to have been successful. Igor responds:

NAME:Formula;SOURCE:data fork;RESOURCENAME:;RESOURCEID:0;TYPE:Encapsulated PostScript;BYTES:52906;WIDTH:68;HEIGHT:16;PHYSWIDTH:51;PHYSHEIGHT:12;

But in the dialog window, there is only a gray rectangle with the green text "EPSF". I now use this Picture as bottom label.

Label bottom "\\$PICT$name=Formula$/PICT$"

The Graph now also shows the green "EPSF" text on gray background as you can see in the attached image.

Again, I am very keen on getting Igor importing Latex pictures. They will resolve a lot. So I really hope we get this running.

 

Many thanks and best regards,

Joerg.

MyGraph.png (4.12 KB)

Ok. It now seems to work. Thank you for your suggestions!

The imported EPS pict is not visible inside Igor. Once I export the Igor Graph as EPS, it appears correctly inside the EPS file.

Good news: It works. Bad news: It is a nasty workaround, because I cannot see what I do inside of Igor. I always have to export and look up the result in an EPS viewer.

I think, the new Igor inline Latex is absolutely the best possible answer to all similar symbol and formula questions. Please carry on! I really wonder, why we can't have a real latex interpreter inside Igor, which knows all Latex math symbols.

LaTeXiT is yet another reason to use macOS. :-)

Have you considered generating the image as PDF instead of EPS (use pdflatex)?

I had suggested that Igor should call an external LaTeX engine rather than doing all the work itself. One answer in the meantime might be to create a function call such as ...

Function convertLaTeX2PDF(<LaTeX string>, [figurename])

The function would take a LaTeX string, use ExecuteCommand to call pdflatex with that string, capture the PDF image, and store it in the Igor pictures (optional name [figurename] or default LaTeXImageNNN). The next step would be to manually embed the picture in an annotation or label. Really cool would be when annotations or labels could accept dynamic functions such as DFT (text) or DFP (picture). The annotation or label would be generated by

\DFP=convertLaTeX2Picture(...)

All that is needed then is for the function convertLaTeX2PDF(...) to return the picture (or a pointer to it as appropriate).

Here is proof of concept on macOS that a LaTeX equation can be compiled using a local latex install.

Function LaTeX2PDF()

	string theFileName = "LaTeX2PDF.tex"
	string thePDFCmd = "/Library/TeX/texbin/pdflatex -output-directory"
	
	string theEQN = "$x - \frac{\hat{x}}{2}$\r"
	string thePreamble = "\documentclass[12pt]{standalone}\r"
	string theLaTeX = thePreamble + "\begin{document}\r" + theEQN + "\end{document}"

	string theTFldr = SpecialDirPath("Desktop",0,0,0)
	string theTFldrU =  SpecialDirPath("Desktop",0,1,0)
	string theTF, theCmd, theUCmd
	
	variable refNum
	
	NewPath/O LaTeX2PDF theTFldr
	open/Z/P=LaTeX2PDF refNum as theFileName	
	fprintf refNum, "%s", theLaTeX
	close refNum
	
	theTF = theTFldrU + theFileName
	theTFldrU = RemoveEnding(theTFldrU)
	sprintf theCmd, "%s='%s' '%s'", thePDFCmd, theTFldrU, theTF
	sprintf theUCmd, "do shell script \"%s\"", theCmd

	ExecuteScriptText/UNQ theUCmd
	return 0

end

I propose this as a basis for a package with a function that will run LaTeX locally to generate a PDF image of an equation.

Hi,

here comes my solution. It seems to work quite well. Unfortunately it is a littel complicated, because it contains several workarounds. It takes a latex code argument and converts it to DVI and further on to PNG and EPS. Inside Igor, the PNG is loaded as a picture and enables WYSIWYG operation. The picture call is returned from the function

If the graph is to be saved as EPS, then all previous Latex PNG pictures are reloaded as EPS. This allows proper EPS export. After export, the pictures are again reloaded as PNG to switch back to a WYSIWYG display inside Igor.

Apart from this I like better graphical resolution than 92 dpi. This is why I ask dvipng to do 300 dpi. Unfortunaltely dvipng saves the 300 dpi images as 92 dpi PNG. So I have to load them into Igor, change the resolution, save them back to disk, and then load them as pictures. It would be helpful to have an Igor function, that can turn images to pictures directly.

InitGraphicsTools()
Make/O/N=2 A = x
Display A vs A as "MyGraph"
DoWindow/C MyGraph
Label bottom, LatexMath("\\hat{x}")
SaveEps("MyGraph")

 

function InitGraphicsTools([fsize])
	variable fsize
	
	if (ParamIsDefault(fsize))
		fsize = 12
	endif
	
	NewDataFolder/O root:Sys
	variable/G root:Sys:LatexMathCnt = 0
	variable/G root:Sys:LatexMathSize = fsize
	string/G root:Sys:LatexMathPath = "C:Temp:Igor:"
	svar LatexMathPath = root:Sys:LatexMathPath
	CreateDiskPath(LatexMathPath)
	string winpath = IgorFileName2WinFileName(LatexMathPath)
	ExecuteScriptText/B/W=0.5 "cmd.exe /C del /Q " + winpath + "*.*"
end
 
function SetLatexMathSize(size)
	variable size
	 
	NewDataFolder/O root:Sys
	variable/G root:Sys:LatexMathSize =  size
end 
 
function/S LatexMath(txt)
	string txt
	
	string latex = ""
	string bat = ""
	string cmd = ""
	string pict = ""
	
	variable dpi = 300
	string crlf = "\r\n"
	NewDataFolder/O root:Sys
	variable/G root:Sys:LatexMathCnt 
	nvar LatexMathCnt = root:Sys:LatexMathCnt
	variable/G root:Sys:LatexMathSize
	nvar LatexMathSize = root:Sys:LatexMathSize

	string/G root:Sys:LatexMathPath
	svar LatexMathPath = root:Sys:LatexMathPath
	if (strlen(LatexMathPath) ==0)
		ErrorMessage("GraphicsTools not initialized!")
		return "???"	
	endif
	string winpath = IgorFileName2WinFileName(LatexMathPath)
	
	string name = "LatexMath" + num2istr(LatexMathCnt) 
	LatexMathCnt += 1 
	
	CreateDiskPath(LatexMathPath)
	
	// create Latex document
	latex += "\\documentclass{article}" + crlf
	latex += "\\begin{document}" + crlf
	latex += "\\thispagestyle{empty}" + crlf
	latex += "\\fontsize{" + num2istr(LatexMathSize) + "}{" + num2istr(LatexMathSize) + "}" + crlf
	latex += "\\selectfont" + crlf
	latex += "$" + txt + "$" + crlf
	latex += "\\end{document}" + crlf
	SaveToFile(latex, LatexMathPath + "Math.tex")
	
	// create windows batch file, which translates the latex document into PNG and EPS
	bat += "C:" + crlf
	bat += "cd " + winpath + crlf
	bat += "latex.exe Math.tex >Latex.log 2>Latex.err" + crlf
	bat += "dvips.exe -E -o " + name + ".eps Math.dvi" + crlf
	bat += "dvipng.exe -D " + num2str(dpi) + " -T tight --truecolor -o " + name + ".png Math.dvi" + crlf
	bat += "del Math.aux" + crlf
	bat += "del Math.log" + crlf
	bat += "del Math.synctex.gz" + crlf
	bat += "del Math.dvi" + crlf
	bat += "del Math.tex" + crlf
	SaveToFile(bat, LatexMathPath + "LatexBat.bat")
	
	// execute windows batch file
	cmd = "cmd.exe /C " + winpath + "LatexBat.bat"
	ExecuteScriptText/B/W=3 cmd

	// change PNG resolution to correct value (fix Latex dvipng bug)
	ImageLoad/O/N=LatexMathPng LatexMathPath + name + ".png"
	SetScale/P x 0, 25.4E-3/dpi, LatexMathPng
	SetScale/P y 0, 25.4E-3/dpi, LatexMathPng
	ImageSave/O/T="rPNG" LatexMathPng as LatexMathPath + name + ".png"
	
	// load PNG pict and return string foc access
	LoadPict/O/Q LatexMathPath + name + ".png", $name
	pict = "\$PICT$name=" + name + "$/PICT$"
	return pict
end
	
function SaveEps(eps)
	string eps
	
	variable/G root:Sys:LatexMathCnt
	nvar LatexMathCnt = root:Sys:LatexMathCnt
	string path = "C:Temp:Igor:"
	
	string name
	variable i

	//Reload all pics as eps
	for (i=0; i<LatexMathCnt; i+=1)
		name = "LatexMath" + num2istr(i)
		LoadPict/O/Q path + name + ".eps", $name
	endfor
	MoveWindow 0,0,0,0
	MoveWindow 1,1,1,1 // forces Igor to redraw the window

	// save 
	SavePict/O/EF=1/E=-3/S as eps
	
	//Restore all pics as png
	for (i=0; i<LatexMathCnt; i+=1)
		name = "LatexMath" + num2istr(i)
		LoadPict/O/Q path + name + ".png", $name
	endfor
	MoveWindow 0,0,0,0
	MoveWindow 1,1,1,1 // forces Igor to redraw the window
end

 

Nice! I had not built a UI and the save operations around my code. That might come later.

You might try this idea as a substitution ...

variable fsize
string dclass

fsize = 12

sprintf dclass, "\\documentclass[%dpt]{standalone}" fsize

This will allow that you can remove the pagestyle and fontsize calls. This will also allow fontsize to be an input variable (i.e. a setvariable on a panel). I would head this way in my next iteration.

Finally, I still am curious why you go through EPS? Why not convert directly to PDF using pdflatex (avoiding the tex -> dvi -> ps -> picture chain)?

Thanks! My code is just a proof of concept. It still needs some error handling in addition.

The "standalone" document class did not work for me in the first place. I have done a Miktex update and then it started working. Now it runs indeed much faster than my "article" document class before. Very good suggestion! Thank you.

I have chosen EPS rather than PDF, because the Igor manual states about importing pictures:

"PDF (Portable Document Format)    Supported in Macintosh native graphics only."

I still have to live on Windows.

Do you know, which ams font package to choose inside Latex in order to best meet the Igor standard font? I have tried

\usepackage{arevmath}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}

The sans serif fonts looks already a bit better than the Igor standard. But it somehow appears a little too large, too wide and too bold. -?

I have not yet considered how to match fonts. My gut feeling is, this will not be an easy issue to address. Indeed, I wonder if the inverse approach will be easier: set the font in the LaTeX document, then match the font in Igor Pro to it.

I appreciate the clarification on EPS. I had lost track of whether Igor Pro by this time supports PDF across all platforms or not.

I have chosen EPS rather than PDF, because the Igor manual states about importing pictures:

"PDF (Portable Document Format)    Supported in Macintosh native graphics only."

I'm not sure why that is relevant since the original post is about exporting, not importing.

 

We are now working on the import of a picture of an equation. To retain vector graphics, does that not restrict the picture to EPS on Windows?

In reply to by jjweimer

We are now working on the import of a picture of an equation. To retain vector graphics, does that not restrict the picture to EPS on Windows?

Igor (all versions) can not render PostScript, whether in the form of EPS or PDF, on Windows. This is because the Windows OS did not provide support for PDF in the past. (There is support now but I am not sure if it is practical for use by Igor.)

Igor6 on Macintosh and Windows can import an EPS but displays the screen preview, if the EPS has one, not the PostScript.

Based on my testing, Igor7 and later on Macintosh displays the EPS as a gray box even if it has a screen preview, but Igor7 and later on Windows displays the (EMF) screen preview. (The EPS screen preview on Macintosh was PICT and Macintosh no longer supports PICT.)

I don't think there is any vector graphics format that Igor can import that displays correctly in all versions on Macintosh and Windows. The only reliable way to import graphics in all versions on Macintosh and Windows is to use PNG which, of course, is a bitmap format, not a vector format.

For Igor7 or later, SVG is supported on both Macintosh and Windows. Based on brief testing, it seems to work, but I have not tested all combinations of operating system and Igor version.

 

 

 

In reply to by hrodstein

hrodstein wrote:

(There is support now but I am not sure if it is practical for use by Igor.)

The PDF "support" on Windows is to display the PDF content as either a bitmap or a DirectXImage. Ugh! Ugh! UGH!

(And developers on macOS think that they have difficulties when they have to deal with differences between using native PDFKit on macOS versus what is implemented in Adobe's version of PDF).

I think this will throw a small proverbial monkey wrench in discussions about using LaTeX in Igor. The caution on Windows will have to be that any LaTeX-generated image posted anywhere in Igor Pro is likely either not going to show at its true resolution on a screen (but it should print at its true resolution) and/or is likely not going to be vector-scalable.

I do not know anything about LaTeX->SVG image to say whether it is doable (or easily doable).

And be aware that Igor's SVG support is limited to the SVG Tiny spec, so not all SVG can be rendered. There is at least one popular drawing program that produces SVG that is not limited to the Tiny spec.

The PDF "support" on Windows is to display the PDF content as either a bitmap or a DirectXImage. Ugh! Ugh! UGH!

Since the screen is a bitmap device, in order to render any graphics on it, the graphics must be converted to a bitmap.

I just wanted to share my final solution. In most cases it works indeed very fine on Win 10. In rare cases and with errors in the latex formula, the latex background process can suspend and block further batch script executions. Then the process has to be shut down manually in the Windows Task Manager.

I hope, the code is well understandable. I use some of my own library...

 

function InitGraphicsTools([fsize])
	variable fsize
	
	if (ParamIsDefault(fsize))
		fsize = 12
	endif
	
	SetQuickSaveCrop(1)
	
	NewDataFolder/O root:Sys
	Make/O/T/N=0 root:Sys:LatexMathPict
	variable/G root:Sys:LatexMathSize = fsize
	string winpath = Igor2Win(TempPath())
	ExecuteScriptText/B/W=0.5 "cmd.exe /C del /Q " + winpath + "*.*"
end
 
function SetLatexMathSize(size)
	variable size
	 
	NewDataFolder/O root:Sys
	variable/G root:Sys:LatexMathSize =  size
end 
 
function/S LatexMathId(txt)
	string txt
	
	NewDataFolder/O root:Sys
	nvar LatexMathSize = root:Sys:LatexMathSize
	
	return "LatexMath_" + FixLengthNum2HexDigits(StringCrc(Hex2Num("0x01234567"),Hash(txt + "_" + num2istr(LatexMathSize),7)),8)
end	
	
function/S LatexMath(txt)
	string txt
	
	string latex = ""
	string bat = ""
	string cmd = ""
	
	variable dpi = 96
	string crlf = "\r\n"
	NewDataFolder/O root:Sys
	variable/G root:Sys:LatexMathSize
	nvar LatexMathSize = root:Sys:LatexMathSize
	if (LatexMathSize ==0)
		print "Auto-intializing GraphicsTools!"
		InitGraphicsTools()	
	endif
	string LatexMathPath = TempPath()
	string winpath = Igor2Win(LatexMathPath)
	
	wave/T LatexMathPict = root:Sys:LatexMathPict
	string name = LatexMathId(txt)
	string png = QuickPath() + name + ".png"
	string eps = QuickPath() + name + ".eps"
	string quickwinpath = Igor2Win(ExperimentPath())
	string winpng = quickwinpath + name + ".png"
	string wineps = quickwinpath + name + ".eps"

	string pict = "\$PICT$name=" + name + "$/PICT$"	
	
	if ((FileExists(png)==0) || (FileExists(eps)==0))

		CreateDiskPath(LatexMathPath)
	
		// create Latex document
		latex += "\\documentclass{standalone}" + crlf
		latex += "\\usepackage{helvet}" + crlf
		latex += "\\usepackage[cm]{sfmath}" + crlf
		latex += "\\renewcommand{\familydefault}{\sfdefault}" + crlf
		latex += "\\begin{document}" + crlf
		latex += "\\fontsize{" + num2istr(LatexMathSize) + "}{" + num2istr(LatexMathSize) + "}" + crlf
		latex += "\\selectfont" + crlf
		latex += "$\\mathrm{" + txt + "}$" + crlf
		latex += "\\end{document}" + crlf
		SaveToFile(latex, LatexMathPath + "Math.tex")
		
		// create windows batch file, which translates the latex document into PNG and EPS
		bat += "C:" + crlf
		bat += "cd " + winpath + crlf
		bat += "latex.exe Math.tex >Latex.log" + crlf
		bat += "dvips.exe -E -o Math.eps Math.dvi" + crlf
		bat += "dvipng.exe -D " + num2str(dpi) + " -T tight --truecolor -o Math.png Math.dvi" + crlf
		bat += "copy Math.png " + winpng + crlf
		bat += "copy Math.eps " + wineps + crlf
		bat += "del Math.png" + crlf
		bat += "del Math.eps" + crlf
		bat += "del Math.aux" + crlf
		bat += "del Math.log" + crlf
		bat += "del Math.synctex.gz" + crlf
		bat += "del Math.dvi" + crlf
		bat += "del Math.tex" + crlf
		SaveToFile(bat, LatexMathPath + "LatexBat.bat")
		
		// execute windows batch file
		cmd = "cmd.exe /C " + winpath + "LatexBat.bat"
		ExecuteScriptText/B/W=5 cmd

		// change PNG resolution to correct value (fix Latex dvipng bug)
		if (FileExists(png))
			if (dpi != 96)
				ImageLoad/O/Q/N=LatexMathPng png
				SetScale/P x 0, 25.4E-3/dpi, LatexMathPng
				SetScale/P y 0, 25.4E-3/dpi, LatexMathPng
				ImageSave/O/T="rPNG" LatexMathPng as png
				KillWaves/Z LatexMathPng
			endif
		else // substitute missing PNG and EPS files
			name = "LatexMathError"
			png = QuickPath() + name + ".png"
			eps = QuickPath() + name + ".eps"			
			DoWindow/K $name
			Display/K=1/W=(100,0,124,20) as name
			DoWindow/C $name
			ModifyGraph gbRGB=(0,0,0)	
			TextBox/C/N=Txt/F=0/B=1/A=MC/X=0/Y=0 "\\Z16\\K(65535,65535,0)\\f01???"
			SavePict/E=-5/B=72 as png
			SavePict/E=-3 as eps
			DoWindow/K $name
		endif
	endif	

	// load PNG pict and return string foc access

	if (StrWaveIndex(LatexMathPict,name)<0) 
		InsertPoints 0,1, LatexMathPict
		LatexMathPict[0] = QuickPath() + name
	endif
	LoadPict/O/Q png, $name
	return pict
end
	
function SaveEps(eps)
	string eps
	
	if (exists("root:Sys:LatexMathPict"))
	
		wave/T LatexMathPict = root:Sys:LatexMathPict
		
		string name
		variable i
	
		//Reload all pics as eps
		for (i=0; i<numpnts(LatexMathPict); i+=1)
			name = (LatexMathPict[i])[strsearchback(LatexMathPict[i], ":")+1, strlen(LatexMathPict[i])]
			LoadPict/O/Q LatexMathPict[i] + ".eps", $name
		endfor
		MoveWindow 0,0,0,0
		MoveWindow 1,1,1,1 // forces Igor to redraw the window
	
		// save 
		SavePict/O/EF=1/E=-3/S as eps
		
		//Restore all pics as png
		for (i=0; i<numpnts(LatexMathPict); i+=1)
			name = (LatexMathPict[i])[strsearchback(LatexMathPict[i], ":")+1, strlen(LatexMathPict[i])]
			LoadPict/O/Q LatexMathPict[i] + ".png", $name
		endfor
		MoveWindow 0,0,0,0
		MoveWindow 1,1,1,1 // forces Igor to redraw the window
	else
		SavePict/O/EF=1/E=-3/S as eps
	endif
end