#4296·KaTeX

Provide a lightweight MathML-only build

Author: EldarAgalarovCreated Sep 28, 2026Updated Sep 29, 2026
Labelsenhancement

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:

typescript
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:

typescript
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:

typescript
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 builder

A MathML-only distribution could retain:

Lexer
Parser
Macro expansion
Symbols
Function definitions
Parse tree
MathML builders
MathML serialization
Error handling
MathML-specific options

while 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

  1. Provide an officially supported MathML-only distribution or entry point.
  2. Preserve KaTeX's existing LaTeX and macro compatibility as much as practical.
  3. Reuse the existing parser and MathML rendering implementation.
  4. Remove HTML-only rendering infrastructure from the resulting bundle.
  5. Make the resulting package suitable for server-side and static-site generation.
  6. Keep the public API small and stable.
  7. 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:

latex
\[
  ROIC = \frac{NOPAT}{Invested\ Capital}
\]

at build time and emit:

xml
<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.