Drawing formulas with Quartz 2d

As the developer of an iPhone application which does just that, trust me when I say typesetting equations is not a trivial undertaking. In my case, I used Core Animation layers to construct the sub-elements of a parsed equation. The equations are constructed hierarchically, and the operations that compose them are laid out as such. Each operation is contained within its parent operation’s layer, and laid out following the rules of that particular operation.

For rendering the visual elements of the operations within an equation, I used Quartz to draw lines, symbols, etc., but most of the drawing was simply text drawn within a CALayer using the NSString text drawing extensions.

I did override the standard CALayer rendering architecture for the generation of PDFs from these equations, because CALayers don’t render as vector elements by default. For an example of how this rendering works in my application, see the open source Core Plot project, which does the same thing at its base level.

I do output to LaTeX from the equations, which is pretty simple once you’ve parsed them into a hierarchical data structure, but parsing them from LaTeX into that structure is proving a little trickier.

For simple text equation input and evaluation, you might find Graham Cox’s GCMathParser to be of use.

Leave a Comment