Overview
MathRenderer is an enum that provides static methods for rendering LaTeX mathematical expressions into platform-native images. It uses the SwiftMath library for LaTeX parsing and rendering, with built-in caching for performance.
Definition
This is an enum with static methods only (no cases). It cannot be instantiated.
Methods
Renders a LaTeX string to a platform-native image.Parameters:
latex: The LaTeX mathematical expression to renderfontSize: The font size in points (default: 16)textColor: The text color for the rendered math
nil if rendering failsPlatform Types:- iOS/visionOS: Returns
UIImage? - macOS: Returns
NSImage?
Type Aliases
LaTeX Preprocessing
Before rendering, LaTeX expressions are automatically preprocessed to improve compatibility with SwiftMath:Command Replacements
| Original | Replacement | Reason |
|---|---|---|
\dots | \ldots | Standardize ellipsis |
\implies | \Rightarrow | Arrow compatibility |
\begin{align} | \begin{aligned} | SwiftMath support |
\end{align} | \end{aligned} | SwiftMath support |
\begin{align*} | \begin{aligned} | SwiftMath support |
\end{align*} | \end{aligned} | SwiftMath support |
\begin{cases} | \left\{\begin{matrix} | Cases environment |
\end{cases} | \end{matrix}\right. | Cases environment |
\dfrac | \frac | Display fraction |
\boxed{...} | ... (content only) | Remove boxing |
Caching
The renderer maintains an internal cache for performance optimization:Cache Configuration
- Type:
NSCache - Count Limit: 256 images
- Key Components: LaTeX string, font size, and resolved color (RGBA)
Cache Key
The cache key considers:- The exact LaTeX string
- Font size in points
- Text color components (R, G, B, A)
SwiftMath Integration
The renderer uses SwiftMath’sMTMathImage for LaTeX rendering:
Label Mode
The renderer uses.text label mode, which renders the expression in text style (inline math) rather than display style.
Image Template Mode
Rendered images are configured as templates to support dynamic colors:Error Handling
If rendering fails, the method:- Prints an error message to the console
- Returns
nil
Example Usage
Basic Usage
Custom Styling
Inline Math
Display Math with Alignment
Common LaTeX Patterns
Fractions
Square Roots
Summations and Integrals
Matrices
Piecewise Functions
Note:\begin{cases} is automatically converted to matrix notation.
Performance Considerations
Cache Hits
Repeated renders of the same expression are served from cache, providing near-instant results.Color Resolution
Dynamic colors are resolved to their effective RGBA values for caching:- UIKit: Uses
UITraitCollection.currentfor resolution - AppKit: Uses
NSApp.effectiveAppearancefor resolution
Memory Management
TheNSCache automatically evicts entries under memory pressure. The 256-image limit balances memory usage with cache effectiveness.
Limitations
- SwiftMath Compatibility: Not all LaTeX commands are supported. The preprocessor handles common incompatibilities.
- Rendering Mode: Only text (inline) mode is used. Display mode with larger operators is not available.
- Advanced Features: Complex LaTeX packages (tikz, pgfplots, etc.) are not supported.
- Error Recovery: Failed renders return
nilwith no fallback.
Related Components
- MarkdownTextView - Main view that uses MathRenderer for inline and display math
- MarkdownTheme - Theme configuration for font sizes and colors
Dependencies
- SwiftMath: LaTeX parsing and rendering engine
- Litext: Text layout framework