Overview
The library supports both inline and display math expressions using standard LaTeX syntax:- Inline math:
$...$for math within text - Display math:
$$...$$for block-level equations
MathRenderer class, which integrates with SwiftMath’s MTMathImage to convert LaTeX expressions into images.
SwiftMath Integration
MarkdownView uses SwiftMath as its LaTeX rendering engine. The integration happens inMathRenderer.swift:101-106:
SwiftMath converts LaTeX expressions into native iOS/macOS/visionOS images using CoreText and CoreGraphics.
LaTeX Preprocessing
Before rendering, LaTeX expressions are preprocessed to ensure compatibility with SwiftMath. The following transformations are automatically applied:| Original Command | Replacement | Reason |
|---|---|---|
\\dots | \\ldots | Standard ellipsis |
\\implies | \\Rightarrow | Implication arrow |
\\begin{align} | \\begin{aligned} | Alignment environment |
\\begin{cases} | \\left\\{\\begin{matrix} | Cases environment |
\\dfrac | \\frac | Display fraction |
\\boxed{...} | content only | Removes boxing |
MathRenderer.swift:65-77 to handle common LaTeX patterns that may not be fully supported by SwiftMath.
Render Cache
Math expressions are expensive to render, so MarkdownView implements an intelligent caching system. Each rendered image is cached based on:- LaTeX content
- Font size
- Text color (resolved RGBA values)
MathRenderer.swift:59-63:
The cache stores up to 256 rendered math expressions. Cache keys account for color appearance (light/dark mode) to ensure correct rendering in different themes.
Color and Appearance
Math images automatically adapt to the current appearance mode:- On iOS/visionOS: Images use
.alwaysTemplaterendering mode with.labeltint color - On macOS: Images are marked as template images for automatic color adaptation
Fallback Behavior
When LaTeX rendering fails (invalid syntax, unsupported commands, etc.), MarkdownView displays the original LaTeX source as inline code with a monospace font. This ensures content remains readable even when rendering fails. FromInlineNode+Render.swift:198-208:
Inline Rendering Details
Math expressions are rendered asLTXAttachment objects that integrate seamlessly with text layout. The rendering process:
- Parse markdown to identify math expressions
- Render to image on background queue (for raw markdown) or main queue (for preprocessed content)
- Create attachment with the rendered image
- Draw inline using custom
LTXLineDrawingActioncallback
InlineNode+Render.swift:149-152).
Performance Considerations
- Async rendering: When using
setMarkdown(string:), math rendering happens on a background queue with UI-dependent steps completed on the main thread - Incremental parsing: Unchanged math expressions are reused when only part of the document changes
- Memory limits: The cache is limited to 256 expressions to prevent unbounded memory growth
Font Size
The default font size for math rendering is 16pt, matching typical body text. Math rendering uses the theme’s body font size to ensure consistent appearance:Customize font sizes through your
MarkdownTheme configuration. Math expressions will scale proportionally with your theme’s body font size.