Math support was added in v10.9.0. Ensure you’re using a compatible version.
Syntax
Surround mathematical expressions with$$ delimiters:
Supported diagram types
Math expressions are currently supported in:- Flowcharts - In node labels and edge labels
- Sequence diagrams - In participant names, messages, and notes
Flowchart examples
Basic expressions
Complex formulas
In node labels
In edge labels
Sequence diagram examples
Participant names
Messages with formulas
Common LaTeX expressions
Greek letters
Fractions and roots
Superscripts and subscripts
Operators and symbols
Matrices
Equations
Rendering modes
Mermaid supports two rendering modes for math:MathML (default)
By default, Mermaid uses MathML for rendering mathematical expressions. MathML is natively supported by modern browsers. Advantages:- No additional CSS required
- Native browser support
- Better performance
CSS rendering (legacy mode)
For browsers without MathML support, you can enable CSS-based rendering using KaTeX’s stylesheets.Include KaTeX stylesheet
Ensure the KaTeX version in your stylesheet matches the version in your package-lock.json.
Force legacy rendering
For consistent rendering across all browsers, you can force CSS rendering even when MathML is supported:Only
forceLegacyMathML needs to be set; it automatically includes legacyMathML behavior.When to use force legacy
- Cross-browser consistency - MathML rendering varies between browsers
- Production apps - When you need identical output everywhere
- Custom styling - When you want to apply custom styles to math
Rendering differences
Different browsers render MathML differently due to font and implementation variations:- Chrome/Edge - Uses system fonts, may vary by OS
- Firefox - Better MathML support, more consistent
- Safari - Native MathML support with macOS fonts
forceLegacyMathML ensures KaTeX’s CSS rendering is used everywhere, providing consistent results.
Best practices
Escaping special characters
- Use double backslashes for LaTeX commands:
$$\\alpha$$ - Escape curly braces in nested structures
- Test expressions in KaTeX demo first
Performance
- Use MathML when possible - Faster rendering, no extra CSS
- Limit complex expressions - Very complex formulas can slow rendering
- Cache rendered diagrams - For static content, render once and cache
Accessibility
- Add accessible descriptions explaining mathematical content
- Use semantic LaTeX commands (e.g.,
\fracinstead of manual formatting) - Test with screen readers to ensure formulas are announced correctly
Styling
Complete example
Troubleshooting
Formulas not rendering
- Check Mermaid version - Math support requires v10.9.0+
- Verify delimiters - Must use
$$around expressions - Check LaTeX syntax - Test expression at katex.org
- Include KaTeX CSS - Required when using
legacyMathML
Rendering differences
- Enable forceLegacyMathML - For consistent cross-browser rendering
- Check font availability - MathML relies on system fonts
- Validate browser support - Ensure browser supports MathML or CSS rendering
Special characters not working
- Use double backslashes -
\\alphainstead of\alpha - Escape in JSON - When in configuration, escape backslashes again
- Check encoding - Ensure UTF-8 encoding for your files
KaTeX resources
- KaTeX documentation - Supported functions and symbols
- KaTeX demo - Test expressions interactively
- LaTeX math symbols - Comprehensive symbol reference
Next steps
Setup and configuration
Configure global and diagram-specific settings
Accessibility
Add accessibility features to diagrams