Date: 2025-04-16
Status
Accepted
Context
Our document processing pipeline relies heavily on LaTeX for high-quality typesetting, especially for academic and technical documents containing complex elements like:
- Mathematical equations and formulas
- Custom font selection and Unicode support
- Cross-platform compatibility for processing
- Web-based math display for HTML outputs
The standard pdfLaTeX engine has limitations with:
- Unicode character handling
- Font management (limited to TeX fonts)
- Memory constraints for complex documents
- Extensibility and programmability
Additionally, our HTML output needs to display mathematical content correctly across browsers and devices without requiring LaTeX installation.
These challenges affect several critical areas in our platform:
- Document processing reliability
- Output quality and consistency
- Cross-platform support
- Mathematical content rendering
Decision
We will implement the following LaTeX-related requirements:
1. LuaLaTeX as Primary Engine
We will standardize on LuaLaTeX as our primary LaTeX engine for the following reasons:
- Built-in Unicode support
- Lua scripting capabilities for custom processing
- Better memory management
- Modern font rendering
- Support for system fonts
Implementation:
defmodule Codexis.Builders.LatexBuilder do
def default_engine, do: "lualatex"
def build_pdf(manuscript_path, options \\ []) do
engine = Keyword.get(options, :engine, default_engine())
# Engine-specific implementation
end
end
2. Fontspec Integration
We will implement Fontspec integration to provide:
- System font access
- Unicode-aware font configuration
- Consistent font rendering across operating systems
- Support for OpenType and TrueType fonts
Standard configuration:
\\usepackage{fontspec}
\\setmainfont{DejaVu Serif}
\\setsansfont{DejaVu Sans}
\\setmonofont{DejaVu Sans Mono}
The system will:
- Include Fontspec by default in our LaTeX templates
- Ship with fallback open-source fonts
- Allow user-configurable font selection
- Implement intelligent font substitution
3. MathJax for HTML Output
We will use MathJax for rendering math in HTML outputs:
- Cross-browser compatibility
- Accessibility features
- Support for LaTeX, MathML, and AsciiMath
- Responsive design support
- Configurable rendering options
Implementation approach:
- Maintain compatibility between LaTeX math and MathJax
- Use CDN-hosted MathJax with fallback to local version
- Configure MathJax for optimal rendering
- Implement automatic detection of math content
Example configuration:
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
processEscapes: true,
packages: ['base', 'ams', 'noerrors', 'noundefined']
},
options: {
renderActions: {
findScript: [10, function (doc) {
for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
const display = node.type.includes('mode=display');
const math = new doc.options.MathItem(
node.textContent,
doc.inputJax[0],
display
);
const text = document.createTextNode('');
node.parentNode.replaceChild(text, node);
math.start = {node: text, delim: '', n: 0};
math.end = {node: text, delim: '', n: 0};
doc.math.push(math);
}
}, '']
}
}
};
4. LaTeX Package Requirements
Our system will require and manage the following core LaTeX packages:
fontspecfor font managementunicode-mathfor Unicode math supportmicrotypefor typography enhancementsbabel/polyglossiafor multilingual supportmathtoolsandamsmathfor mathematical content
Consequences
Positive
- Improved unicode handling: LuaLaTeX provides native Unicode support, eliminating character encoding issues.
- Better font management: Fontspec allows using system fonts and provides more typography options.
- Enhanced mathematical typesetting: Combination of LuaLaTeX and MathJax ensures consistent math rendering across formats.
- Improved extensibility: Lua scripting allows custom document processing beyond LaTeX capabilities.
- Cross-platform consistency: Font rendering will be more consistent across operating systems.
- Web-friendly math output: MathJax ensures mathematical content displays correctly in browsers without requiring LaTeX.
Negative
- Increased dependencies: LuaLaTeX requires a more comprehensive TeX distribution.
- Performance overhead: LuaLaTeX can be slower than pdfLaTeX for certain documents.
- Migration costs: Existing pdfLaTeX-specific code will need updates.
- Testing complexity: Need to verify outputs across multiple engines and formats.
- MathJax loading time: Can impact page load performance for math-heavy content.
Mitigations
- Implement caching mechanisms for repeated LaTeX compilations
- Provide fallback options to pdfLaTeX when appropriate
- Create compatibility layer for existing templates
- Use MathJax configuration optimized for performance
- Implement comprehensive testing across formats