How these pages fake having math
The HTML you are reading contains the LaTeX, not the pictures. A script swaps one for the other.
Each page is a plain text file. Where a formula belongs, the source file has the
LaTeX between $…$ or $$…$$ — the same delimiters the
MathJax-style setups use. At load, math.js walks the text nodes of the
body, finds those delimiters (and \(…\) and \[…\]), and
replaces each with the SVG that the worker renders for it. Nothing is pre-built,
nothing is on disk but the script, the stylesheet and the HTML.
The endpoint
formel is a Rust math-typesetting engine compiled to WebAssembly. The Cloudflare Worker keeps one renderer per isolate, parses a bundled New Computer Modern Math font once, and answers with a standalone SVG:
GET /?tex=<LaTeX>—image/svg+xml, or a plain-text error- missing
tex—400; over 2000 characters or over 512 KiB of SVG —413 - unparsable LaTeX —
422, with the parser's message
Display style has to be asked for in the LaTeX itself, with \displaystyle;
the worker always typesets as inline math, so the script prepends it for a display
block. The inline=1 flag asks for the box-sized SVG: no margin,
currentColor for the fill, and its own vertical-align, which
is what lets the result drop into a line of text at the right height and follow the
page's light or dark ink without a second render.
What the script does, and what it costs
- It skips
script,style,codeandpre, so snippets in the text are left alone. - Until a picture arrives, the span holds the TeX source in a monospace, dimmed font: the reader sees roughly the right amount of text instead of a hole.
- Fetches run four at a time. Each formula is one request the first time and a
cache hit afterwards — the worker stores responses in the Cache API and marks them
immutable, and the browser keeps them too. - If a formula fails to parse, the span keeps the source and gets a red tint with the parser's message as its tooltip. Nothing disappears silently.
The cost of doing it this way rather than at build time is that the first reader of a
page waits for a handful of round trips and the formulas arrive as pictures of text —
selectable only through the SVG's own title. The benefit is that the site
is three static files with no pipeline, and a changed formula is a text edit.