What would be the best approach to implement math formula rendering in a Cocoa application?
There is a utility app included with MacOS X called Grapher that do
If you only need to typeset equations and not build an interactive equation editor, you might want to look at using troff
. It's got a reasonably simple syntax for equations, outputs postscript, and is part of OS X. The easiest way to use it would also require ghostscript.
As an example (based on the man page):
Create the file file fib.tr
:
.EQ
x sub i = x sub {i-1} + x sub {i-2}
.EN
Convert to postscript with eqn
and groff
:
$ eqn fib.tr | groff -P-b16 > fib.ps
Calculate and extract the bounding box with ghostscript:
$ gs -dNOPAUSE -sDEVICE=bbox -- fib.ps 2> fib.bbox
Add the bounding box information to the ps file to generate an eps file:
$ cat fib.ps | sed -e '/%%Orientation/rfib.bbox' > fib.eps
Convert the eps file to a pdf using a perl script included with most TeX distributions (and available at http://tug.org/epstopdf/)
$ epstopdf fib.eps
Now you have a pdf image that contains just the rendered equation with minimal padding.
Alternatively, if your users have TeX installed (or you want to go through the hassle of bundling a minimal TeX distribution with your app), then LaTeXiT provides a service to typeset equations and return them as PDF images. You could call it using NSPerformService
.