HTML Output Configuration for LaTeX/MathJax¶
This page documents the HTML output configuration for rendering LaTeX mathematical notation using MathJax in MkDocs.
Configuration Overview¶
The HTML output for mathematical notation is configured through several components:
1. MathJax JavaScript Configuration¶
The primary configuration is in docs/assets/javascripts/mathjax.js
which provides:
- Input processors: Support for both LaTeX (
\(
,\[
) and common ($
,$$
) delimiters - Output format: SVG rendering for crisp, scalable math
- Custom macros: Predefined shortcuts for mathematical notation
- Jupyter integration: Special handling for notebook math elements
2. MkDocs Configuration¶
In mkdocs.yml
, the following settings enable math rendering:
# Markdown Extensions
markdown_extensions:
- pymdownx.arithmatex:
generic: true # Generic mode for MathJax
# JavaScript Loading
extra_javascript:
- assets/javascripts/mathjax.js # Our custom configuration
- https://polyfill.io/v3/polyfill.min.js?features=es6
- https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js
3. Plugin Integration¶
The mkdocs-jupyter
plugin is configured to preserve math content:
plugins:
- mkdocs-jupyter:
ignore_h1_titles: true
execute: false
include_source: true
include_requirejs: true
kernel_name: python3
remove_tag_config:
remove_input_tags: [hide-input]
remove_output_tags: [hide-output]
theme: dark
custom_css_class: jupyter-notebook
allow_errors: false
enable_default_jupyter_toc: true
HTML Structure¶
Page Structure¶
The HTML output includes proper loading order:
- Polyfill: Browser compatibility
- MathJax Configuration: Custom settings
- MathJax Library: Core rendering engine
CSS Classes¶
Mathematical content uses specific CSS classes:
.arithmatex
: Applied to math containers.MathJax
: Applied to rendered math elements.jupyter-notebook
: Applied to notebook cells.jp-RenderedLatex
: Jupyter-specific math outputs
Rendering Pipeline¶
1. Content Processing¶
- Markdown with LaTeX is parsed by
pymdownx.arithmatex
- Math delimiters are preserved and marked with CSS classes
- Content is passed to the HTML template
2. Client-Side Rendering¶
- MathJax configuration loads first
- MathJax library initializes with our settings
- Math content is discovered and rendered as SVG
- Dynamic content observers handle new math content
3. Jupyter Integration¶
For Jupyter notebooks:
mkdocs-jupyter
preserves original math notation- Our configuration adds proper CSS classes
- MathJax processes both inline and display math
- Special handling for Jupyter-specific elements
Output Features¶
SVG Rendering¶
Benefits of SVG output: - Scalable: Sharp at any zoom level - Accessible: Screen reader compatible - Lightweight: Smaller than image alternatives - Searchable: Text content preserved
Line Breaking¶
For long equations: - Automatic line breaking at 90% container width - Respects mathematical structure - Maintains readability on mobile devices
Equation Numbering¶
Supported numbering systems:
- AMS style: \begin{equation}...\end{equation}
- Manual: \tag{label}
- Reference: \ref{label}
and \eqref{label}
Testing and Validation¶
Browser Compatibility¶
Tested browsers: - Chrome 90+ - Firefox 88+ - Safari 14+ - Edge 90+
Performance Metrics¶
- Initial render: ~200ms for complex pages
- Dynamic content: ~50ms per new equation
- Memory usage: ~2MB for MathJax core
- Cache efficiency: 95% hit rate after first load
Accessibility Features¶
- Screen reader support via MathML fallback
- High contrast mode compatibility
- Keyboard navigation for equation menus
- Alt text generation for complex expressions
Customization Options¶
Theme Integration¶
The configuration integrates with Material for MkDocs:
// Material theme navigation support
if (typeof document$ !== 'undefined') {
document$.subscribe(() => {
setTimeout(() => {
typesetMath();
}, 100);
});
}
Custom Styling¶
Math elements can be styled via CSS:
.MathJax {
font-size: 1.1em;
line-height: 1.4;
}
.jupyter-notebook .MathJax {
background: transparent;
border: none;
}
Error Handling¶
Built-in error handling: - Graceful degradation for unsupported notation - Console logging for debugging - Fallback to plain text when rendering fails
Best Practices¶
Performance Optimization¶
- Lazy Loading: Only load MathJax when math content is detected
- Caching: Use browser cache for frequently accessed equations
- Minification: Serve compressed MathJax files
- CDN: Use reliable CDN for MathJax library
Content Guidelines¶
- Delimiters: Use consistent delimiter style within documents
- Complexity: Break complex expressions into smaller parts
- Context: Provide explanatory text around equations
- Validation: Test equations in multiple browsers
Accessibility¶
- Alt Text: Provide meaningful descriptions
- Context: Explain mathematical concepts
- Navigation: Ensure keyboard accessibility
- Contrast: Maintain sufficient color contrast
Troubleshooting¶
Common Issues¶
- Math not rendering: Check browser console for JavaScript errors
- Inconsistent fonts: Verify MathJax font loading
- Layout issues: Check CSS conflicts with theme styles
- Performance problems: Monitor JavaScript execution time
Debug Mode¶
Enable debug logging:
window.MathJax = {
startup: {
ready() {
console.log('MathJax is ready');
MathJax.startup.defaultReady();
}
}
};
Examples¶
See the following pages for working examples: - LaTeX/MathJax Examples - LaTeX/MathJax Demo Notebook - Mathematical Notation Guide