Mathematics in Markdown
Heine can render TeX math while building a site. Enable it in heine.toml:
[markdown]
math = "katex"Write inline math between single dollar signs and display math between double dollar signs:
The derivative is $f'(x) = 2x$.
$$
\int_0^1 x^2\,dx = \frac{1}{3}
$$Heine generates visual HTML and MathML at build time. Readers do not need JavaScript, but the site must supply the matching KaTeX stylesheet and fonts. Without the stylesheet, formulas and their accessible representations may both be visible. Generated math attributes use a stable order across builds.
For native browser rendering, choose math = "mathml" instead. This produces
MathML without the visual KaTeX HTML and does not require the KaTeX stylesheet
or fonts. An OpenType math font such as STIX Two Math can improve layout; a
site can supply one as a WOFF2 asset or rely on the reader's available fonts.
Both modes use the same TeX parser, so they accept the same expressions and
share the limitations below.
Browser MathML support can change or omit notation. For example, Chromium can
render \mathbb{R} as an ordinary R and omit the line from \cancel{x}.
The renderer also reverses horizontal and vertical outer array lines in MathML.
Check your formulas in the browsers your readers use.
Choose a mode for an input
A page can override the site default for each declared Markdown input:
[content]
main = "article.md"
example = "example.md"
[markdown.math]
example = "tex"main inherits the site default. Both inline and display math in example
use tex. Every override must name a declared Markdown input and use one of
off, katex, mathml, or tex. Render an unsegmented input with
markdown(content="example") to apply its effective mode automatically.
In heine.toml, markdown.math is a single site-wide default. In a .page
file, [markdown.math] maps declared input names to overrides. Use
main = "off" there to leave dollar signs in that input as ordinary Markdown.
The markdown filter accepts arbitrary text, so it uses the site default unless
you pass math. For a segment, pass its declared input's effective mode:
{{ page.content.example.lead | markdown(math=page.markdown.example.math.mode) }}The filter cannot recover a source identity from the text, including when that
text comes from another page. Source-aware features retain their existing
requirement to use markdown(content="…").
Heine checks declared Markdown before rendering templates, using the input's
effective mode. A filter argument changes only that rendering call. For
example, passing math="tex" to the filter does not prevent an input declared
as katex from failing its earlier TeX check. Set the input's .page override
to tex when it needs to preserve expressions that KaTeX cannot parse.
For example, to render another page's declared input with that page's mode:
{% set other = page(id="guide/example") %}
{{ other.content.main | markdown(math=other.markdown.main.math.mode) }}A quick build may leave a page that reads another page's facts unchanged after those facts change. Use a full build when cross-page content or facts change.
Load resources for declared math
Each declared Markdown input exposes its effective mode and whether it
contains recognized math as page.markdown.<name>.math.mode and
page.markdown.<name>.math.present. The facts describe the input, not all
content the template may render. In off mode, math parsing is disabled and
present is false. For segmented inputs, present covers all segments.
It records parsed math, not TeX validity: tex preserves recognized expressions
without validating them, while invalid TeX fails a katex or mathml build.
Math in image alternative text stays as TeX with its delimiters, because an
HTML alt attribute cannot contain rendered math. Write a plain-language
alternative when readers need the mathematical meaning described.
The fact can therefore be true when math occurs only in an image alternative or
an unrendered segment, even though neither needs KaTeX CSS on the page.
A template that renders main can use its facts to link KaTeX CSS:
{% if page.markdown.main.math.mode == "katex" and page.markdown.main.math.present %}
{% set math_css = asset(id="assets/katex/katex.min.css") %}
<link rel="stylesheet" href="{{ math_css.url }}"{% if math_css.integrity %} integrity="{{ math_css.integrity }}" crossorigin="anonymous"{% endif %}>
{% endif %}When a template renders other inputs, uses a different mode in the Markdown
filter, or adds math through raw HTML or an external renderer, include the
resources those uses need as well.
Heine does not infer a complete resource list for the rendered page.
For example, a page combining katex and tex inputs needs the KaTeX
stylesheet for the former and any resources required by the site's chosen TeX
renderer for the latter.
Install the stylesheet and fonts
The current renderer tracks KaTeX 0.19.1. Download its distribution, then copy the stylesheet and WOFF2 fonts into the site. For example, on a POSIX shell:
curl -fL https://registry.npmjs.org/katex/-/katex-0.19.1.tgz -o /tmp/katex-0.19.1.tgz
mkdir -p /tmp/heine-katex content/assets/katex/fonts
tar -xzf /tmp/katex-0.19.1.tgz -C /tmp/heine-katex
cp /tmp/heine-katex/package/dist/katex.min.css content/assets/katex/
cp /tmp/heine-katex/package/dist/fonts/*.woff2 content/assets/katex/fonts/The stylesheet also names WOFF and TTF fallbacks. This setup supports browsers
that use WOFF2. Keep the fonts/ directory next to the stylesheet so its
relative font URLs work. Update these assets when Heine changes the KaTeX
version it uses. The stylesheet and fonts have their own licenses; include the
applicable notices when distributing them.
In the base template, link the stylesheet using Heine's checked asset lookup:
{% set math_css = asset(id="assets/katex/katex.min.css") %}
<link rel="stylesheet" href="{{ math_css.url }}"{% if math_css.integrity %} integrity="{{ math_css.integrity }}" crossorigin="anonymous"{% endif %}>For long display formulas, a site may add horizontal scrolling:
.katex-display {
max-inline-size: 100%;
overflow-x: auto;
overflow-y: hidden;
}The starter kit includes the KaTeX assets and STIX Two Math. The starter site
uses KaTeX by default; changing only math = "katex" to math = "mathml" in
its heine.toml switches to native MathML with STIX. The kit links its KaTeX
stylesheet when a page's main Markdown input contains KaTeX math in its
declared mode. Templates that render other inputs, use a different filter mode,
or add math in another way must link the resources those uses need. A site
created with heine new leaves math off and does not include these assets.
Supported input and diagnostics
Heine checks expressions and reports invalid or unsupported TeX at the source.
The KaTeX commands \href, \url, \includegraphics, \htmlClass,
\htmlId, \htmlStyle, and \htmlData are unavailable. Use Markdown links
and images outside a formula instead. Definitions in one expression do not
affect another expression. Each expression is limited to 100 macro expansions;
exceeding that limit produces a diagnostic.
The renderer limits parsing and tree building to 32 active expression, function, or group calls. A command with arguments can use more than one call. It also limits markup serialization to 192 nested containers. When an expression exceeds either limit, Heine reports a diagnostic. Long, flat expressions do not use up the nesting limit.
The supported TeX subset depends on the bundled renderer. For a description of
KaTeX notation, see KaTeX's supported functions.
The Rust renderer may differ from the JavaScript implementation. For
\reflectbox and \mathreflectbox, reflection applies to visual HTML only;
the MathML representation retains the unreflected content. In math = "mathml"
mode, these commands therefore do not visually reflect their contents.
To preserve TeX delimiters for a renderer supplied by the site, use
math = "tex" instead. The reference describes all
four settings.