ADR 0005: GitHub-native math syntax, rendered in the book by KaTeX
Context
Documentation pages are read in two places: on GitHub, and in the mdBook site. Writing the conventions page showed that GitHub’s renderer has four relevant quirks:
- It applies Markdown backslash escapes inside
$$blocks, so\;and\!appear as visible punctuation and mid-line\\row breaks are lost. - It rejects
\operatorname. \tagbreaks equation layout in Chromium.- In
$…$inline math, subscripts with underscores can be read as emphasis.
Decision
- Display equations are fenced code blocks with the language
math, and inline equations are written$`…`$, a code span wrapped in dollar signs. Markdown never processes code, so the TeX arrives intact on GitHub and in the book. - Equation IDs are written as a trailing
\qquad\text{(ID)}, not with\tag. - Use
\mathrm{…}instead of\operatorname{…}. - In the book,
docs/theme/math.jsrenders both forms with KaTeX 0.19.1. KaTeX is loaded from a CDN at a pinned version with subresource integrity. The script also gives each tagged display equation the anchoreq-ID.
Consequences
- A page renders correctly on GitHub and in the book from one source.
- Without JavaScript, the book shows readable TeX source in code blocks, so it degrades gracefully.
- Code comments can cite and link equations by stable ID.
cargo xtask equationschecks that IDs are unique and that every citation exists. - The book depends on the CDN at view time. If KaTeX fails to load, the TeX is shown as text.
Alternatives considered
$$and$delimiters with the mdbook-katex preprocessor. Server-side rendering is attractive, but pages written that way render incorrectly on GitHub.- mdBook’s built-in MathJax support. It needs
\\(and\\[delimiters, which GitHub doesn’t understand, and MathJax is heavier than KaTeX. - Vendoring KaTeX into the repository. It works offline, but adds fonts and minified JavaScript to the repository. A pinned CDN copy with integrity hashes gives the same guarantees about what code runs.