Notes

Rendering Mathematics at Build Time

CodeMathematics

Typesetting mathematics in the browser costs about a megabyte of JavaScript and shows the reader raw TeX until it finishes. Doing it at build time costs nothing at all.

The catch is that equation numbering is document-scoped, so the whole article has to be rendered in one pass.

Why one pass

An equation counter that restarts at every expression is useless, and a forward reference cannot resolve if the target has not been parsed yet. Rendering the assembled document once solves both.

math.ts
const doc = mathjax.document(html, { InputJax: tex, OutputJax: chtml });
doc.render();

The trap is that the TeX input jax carries counter state between documents. Without a reset, every post after the first is silently misnumbered.

See the fundamental theorem for a worked example.