SwiftMath
provides a full Swift implementation of iosMath
for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written
using LaTeX in a UILabel
equivalent class. It uses the same typesetting rules as LaTeX and
so the equations are rendered exactly as LaTeX would render them.
Please also check out SwiftMathDemo for examples of how to use SwiftMath
from SwiftUI.
SwiftMath
is similar to MathJax or
KaTeX for the web but for native iOS or MacOS
applications without having to use a UIWebView
and Javascript. More
importantly, it is significantly faster than using a UIWebView
.
SwiftMath
is a Swift translation of the latest iosMath
v0.9.5 release but includes bug fixes
and enhancements like a new \lbar (lambda bar) character and cyrillic alphabet support.
The original iosMath
test suites have also been translated to Swift and run without errors.
Note: Error test conditions are ignored to avoid tagging everything with silly throw
s.
Please let me know of any bugs or bug fixes that you find.
SwiftMath
prepackages everything needed for direct access via the Swift Package Manager.
Here are screenshots of some formulae that were rendered with this library:
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
f(x) = \int\limits_{-\infty}^\infty\!\hat f(\xi)\,e^{2 \pi i \xi x}\,\mathrm{d}\xi
\frac{1}{n}\sum_{i=1}^{n}x_i \geq \sqrt[n]{\prod_{i=1}^{n}x_i}
\frac{1}{\left(\sqrt{\phi \sqrt{5}}-\phi\\right) e^{\frac25 \pi}}
= 1+\frac{e^{-2\pi}} {1 +\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\cdots} } } }
More examples are included in EXAMPLES
Here are previews of the included fonts:
SwiftMath
works on iOS 11+ or MacOS 12+. It depends
on the following Apple frameworks:
- Foundation.framework
- CoreGraphics.framework
- QuartzCore.framework
- CoreText.framework
Additionally for iOS it requires:
- UIKit.framework
Additionally for MacOS it requires:
- AppKit.framework
SwiftMath
is available from SwiftMath.
To use it in your code, just add the https://github.com/mgriebling/SwiftMath.git path to
XCode's package manager.
The library provides a class MTMathUILabel
which is a UIView
that
supports rendering math equations. To display an equation simply create
an MTMathUILabel
as follows:
import SwiftMath
let label = MTMathUILabel()
label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"