Math & diagrams

Typst equations and drawing packages (CeTZ, Fletcher, Lilaq) target a 2D coordinate canvas rather than flow-based HTML. Two choices determine how they behave on the web: whether to emit native MathML vs html.frame / standalone SVGs, and how to keep contrast clean in dark mode.

Math

Equations have two export paths in HTML:

ApproachStrengthsTrade-offs
Native MathML (default)Tiny output, selectable/copyable text, adapts automatically to font size and dark themeBrowser rendering varies for complex matrices, alignments, and math fonts
html.frame inline SVGIdentical to Typst PDF output; supports every math macro and styling ruleLarger HTML size; requires explicit dark-mode and inline baseline handling

To enforce uniform rendering across browsers, wrap equations in html.frame under target() == "html":

typst
#let math-svg(eq, alt: auto) = context {
  if target() != "html" { return eq }
  let is-block = eq.at("block", default: math.equation.block)
  let label-text = if alt == auto { eq.at("alt", default: math.equation.alt) } else { alt }
  let attrs = (
    class: if is-block { "math-block" } else { "math-inline" },
    role: "math",
  )
  if label-text != none { attrs.insert("aria-label", label-text) }

  if is-block {
    html.elem("div", attrs: attrs, html.frame(eq))
  } else {
    html.elem("span", attrs: attrs, box(html.frame(eq)))
  }
}

#show math.equation: math-svg

Wrap inline math in box(html.frame(eq)) so it flows inside paragraphs, and place display equations in an overflow-x: auto container to prevent horizontal overflow on small screens. Attach accessible text with math.equation(alt: "Euler's identity", $e^(i pi) + 1 = 0$). Because html.frame embeds font paths directly into SVG, include required math fonts (such as New Computer Modern Math) under [typst.fonts] for --pure builds.

CeTZ & Fletcher

Vector packages such as CeTZ and Fletcher cannot map to flow HTML elements and must be rasterized as inline SVGs via html.frame(...) when targeting HTML. Wrap them behind target() so the same source also builds PDFs cleanly:

typst
#import "@preview/cetz:0.3.4"
#import "@preview/fletcher:0.5.7" as fletcher

#let web-frame(body, class: "diagram-frame") = context {
  if target() == "html" {
    html.figure(class: class, html.frame(body))
  } else {
    body
  }
}

#let canvas(..args) = web-frame(cetz.canvas(..args))
#let diagram(..args) = web-frame(fletcher.diagram(..args))

Mermaid

To avoid shipping a large client-side Mermaid runtime, compile ```mermaid blocks at build time with @preview/merman and publish standalone SVGs via Bundle-level query aggregation:

typst
// 1. Intercept mermaid blocks, record metadata, and emit <img>
#let mermaid-block(source) = {
  let id = diagram-id(source.text)
  html.div(class: "mermaid-diagram")[
    #metadata((id: id, source: source.text)) <mermaid-diagram>
    #html.img(class: "diagram-light", src: output-url("diagrams/" + id + "-light.svg"), alt: "")
    #html.img(class: "diagram-dark", src: output-url("diagrams/" + id + "-dark.svg"), alt: "")
  ]
}

// 2. Deduplicate at Bundle top level and emit light/dark SVGs
#let publish-mermaid() = context {
  let seen = (:)
  for item in query(<mermaid-diagram>) {
    let v = item.value
    if v.id in seen { continue }
    seen.insert(v.id, true)
    import "@preview/merman:0.5.0": mermaid-svg
    for (mode, palette) in themes {
      asset(
        "diagrams/" + v.id + "-" + mode + ".svg",
        mermaid-svg(v.source, site-config: merman-config(palette), scoped-css: edge-label-css(palette)),
      )
    }
  }
}

Mermaid leaves edge-label backgrounds semi-transparent by default, letting arrow lines cut through text. Set themeVariables.edgeLabelBackground to your canvas background color and add .edgeLabel rect{opacity:1!important} in scoped-css so arrows break cleanly around labels. Adding accTitle: and accDescr: in the Mermaid source lets templates populate alt automatically.

Dark mode

Choose dark-mode styling by diagram type rather than applying blanket filter: invert(1) to colored diagrams:

css
/* Monochrome html.frame equations */
:root[data-theme="dark"] .math-block svg,
:root[data-theme="dark"] .math-inline svg {
  filter: invert(0.92) hue-rotate(180deg);
}
css
/* Separate light/dark SVGs keep borders, fills, and text legible */
.diagram-dark { display: none; }

:root[data-theme="dark"] .diagram-light { display: none; }
:root[data-theme="dark"] .diagram-dark { display: block; }

For colored CeTZ or Fletcher diagrams rendered via html.frame, pass light/dark palette arguments and toggle two frames; monochrome academic figures can use a light CSS inversion. See Dark mode for theme-toggle wiring, WCAG balanced contrast calculations for single-SVG artwork, and light-dark() trade-offs.

Search this site

Use the arrow keys to move through results.

Type to search.