Provide a lightweight MathML-only build
KaTeX already supports generating MathML via output: "mathml", but the distributed library still contains infrastructure required for HTML rendering.
It would be useful to provide an official MathML-only build/package containing only the functionality required for:
LaTeX → parse tree → MathML
without shipping the HTML renderer and other HTML-specific code.
Motivation
There are use cases where KaTeX is used exclusively as a server-side/build-time LaTeX-to-MathML compiler.
For example, a static-site generator can convert:
katex.renderToString(source, {
output: "mathml",
});into static <math> elements during the build. In this scenario, the HTML renderer, DOM-related infrastructure, font metrics, and other HTML-specific functionality are unnecessary.
A dedicated build could therefore provide:
- lower package/bundle size;
- fewer dependencies at runtime;
- simpler deployment for server-side/static generation;
- no KaTeX JavaScript required in the browser;
- a clearer separation between TeX parsing and presentation backends.
Proposed API
Ideally, the lightweight package would expose an API such as:
import { renderToMathML } from "@katex/mathml";
const mathml = renderToMathML(String.raw`
\mathrm{ROIC} =
\frac{\mathrm{NOPAT}}{\mathrm{Invested\ Capital}}
`);with options equivalent to the MathML-relevant subset of the existing KaTeX API.
Alternatively, this could be exposed as a separate entry point rather than a separate npm package:
import { renderToMathML } from "katex/mathml";Possible architecture
The existing architecture already separates much of the rendering process conceptually:
LaTeX
│
▼
Parser / macro expansion
│
▼
Parse tree
│
├── HTML builder
│
└── MathML builderA MathML-only distribution could retain:
Lexer
Parser
Macro expansion
Symbols
Function definitions
Parse tree
MathML builders
MathML serialization
Error handling
MathML-specific optionswhile excluding functionality exclusively required for HTML rendering.
The implementation would ideally preserve the existing parser and MathML builders rather than introducing a separate LaTeX parser.
Goals
- Provide an officially supported MathML-only distribution or entry point.
- Preserve KaTeX's existing LaTeX and macro compatibility as much as practical.
- Reuse the existing parser and MathML rendering implementation.
- Remove HTML-only rendering infrastructure from the resulting bundle.
- Make the resulting package suitable for server-side and static-site generation.
- Keep the public API small and stable.
- Measure and document the resulting bundle-size reduction.
Non-goals
This proposal is not asking for:
- changes to KaTeX's existing HTML rendering;
- removal of MathML support from the main package;
- a new LaTeX parser;
- client-side MathML rendering;
- a replacement for the normal KaTeX distribution.
The existing full KaTeX package should remain available for applications requiring HTML rendering.
Example use case
A static-site generator may process Markdown/MDX containing:
\[
ROIC = \frac{NOPAT}{Invested\ Capital}
\]at build time and emit:
<math display="block">
...
</math>In this workflow, shipping the HTML renderer and its associated infrastructure provides little value.
A dedicated MathML-only build would make KaTeX a more natural fit for this use case.
Open questions
- Should this be a separate npm package or an additional package export?
- How much of the current bundle can be eliminated through module separation/tree-shaking?
- Which existing APIs and options are genuinely required by the MathML renderer?
- Can the MathML-only build retain the same function/macro definitions as the full package?
- Should the MathML-only build have an independent size budget tested in CI?
Success criteria
A successful implementation would provide an officially supported API for LaTeX → MathML while producing a substantially smaller distribution than the full KaTeX package, without materially reducing KaTeX's existing MathML/LaTeX compatibility.
Source: KaTeX/KaTeX