Typst basics

When compiling for HTML, write prose, headings, lists, equations, and links in native Typst markup while controlling layout and presentation with html.* elements and CSS (paged rules such as set page(...) do not govern browser layout). For complete language documentation, see the Typst documentation.

Prose & headings

.typ files start in markup mode, where standard Typst syntax maps cleanly to semantic HTML:

typst
#title[Experiment log]

Put the *summary* first and _open questions_ at the end. Inline math $x^2 + y^2$,
or spaces inside dollar signs for block equations:

$ sum_(i=1)^n i = frac(n(n + 1), 2) $

= References <links>

- #link("https://typst.app/")[Typst docs]
- #link("https://tola.build/")[Tola site]

By default, #title[...] (or parameterless #title(), reading document(..., title: ...)) exports as <h1>, = as <h2>, and == as <h3>. Adjust sizes with CSS rather than skipping heading levels.

  • Attaching a label (= References <links>) lets you jump with #link(<links>)[References] or cite its number with @links when #set heading(numbering: "1.1") is active.
  • When embedding a subfile inside a section via include, use #set heading(offset: heading.offset + 1) to shift its heading levels.
  • All documents in the Bundle share heading and equation counters; reset them with #counter(heading).update(0) and #counter(math.equation).update(0) at the start of each document when numbering is enabled.

Component functions

Template code uses three primary syntactic forms:

  • "text" is a plain string (str) for paths, URLs, and HTML attributes; HTML characters in strings are escaped rather than parsed as tags.
  • [text] is content (content) that can nest markup, equations, links, and components; switching back to code inside [...] requires #.
  • { ... } is a code block: omit # inside, let bindings emit nothing, and remaining expressions concatenate. Arrays must be joined explicitly with for or .join().

Here is a callout component and a link list helper:

typst
#let callout(body, title: none, kind: "note") = html.aside(class: "callout " + kind)[
  #if title != none { html.strong(title) }
  #body
]

#let link-list(items) = html.ul(
  items.map(item => html.li(link(item.href, item.label))).join(),
)

#callout(title: "Tip", kind: "tip")[
  Pass the body as a trailing `[...]` block and options as named arguments.
]

#{
  let links = (
    (href: "/docs/start/overview/", label: "Overview"),
    (href: "/docs/start/quick-start/", label: "Quick start"),
  )
  link-list(links)
}

A trailing [...] after a function call binds to its final positional parameter body. Wrap multi-line array expressions or chained .filter(...) / .map(...) calls in #{ ... } or #( ... ) so trailing lines in markup mode are not parsed as prose.

Modules & templates

Keep shared components and layouts outside content/, import them, and apply a wrapper template with #show:

typst
#let page-layout(title-text, body) = html.html(lang: "en")[
  #html.head[
    #html.meta(charset: "utf-8")
    #html.meta(name: "viewport", content: "width=device-width, initial-scale=1")
    #html.title(title-text)
  ]
  #html.body[
    #html.main(class: "reading", body)
  ]
]
typst
#import "/templates/page.typ": page-layout

#document("index.html", format: "html", title: "Home")[
  #show: page-layout.with("Home")
  #include "/content/index.typ"
]
  • import brings exports into scope without inheriting the caller’s variables; include inserts the target file’s rendered content at the call site.
  • A leading / resolves against the site root in site files and against the module root inside modules. Reading or including a file never publishes it as an output.
  • set changes default parameters for subsequent elements, while show intercepts matching elements; an un-targeted #show: fn passes the rest of the document body into fn.

context

Queries depending on document location or whole-site state (current-document(), headings(), references(), counters) must run inside context:

typst
#import "@tola/document:0.0.0": current-document

#context {
  let doc = current-document()
  [Output path: #raw(doc.output)]
}

HTML elements

Typed html.* helpers (html.nav, html.aside, html.main) accept standard HTML attributes and a body (void elements take no body); use html.elem for data-* or custom attributes:

typst
#html.aside(class: "note")[Semantic sidebar.]
#html.elem("aside", attrs: ("data-kind": "tip"))[Container with custom attributes.]

When building your own #html.html[...] shell:

  • Keep an explicit #html.head[...] so Typst can inject math styles, and add <meta charset>, viewport, <title>, stylesheets, and SEO tags explicitly.
  • html.script and html.style take string bodies; pass an empty string for external scripts: html.script("", src: url, defer: true).
  • Use html.a(...) when you need raw HTML attributes like target or rel.
  • Custom shells do not place footnotes automatically; render or style them explicitly if used.

Markup vs HTML

Separate markup from raw HTML by layer:

  • In content files (content/), stick to Typst markup and semantic component functions (= Heading, #link(...), lists, tables, math, #callout(...)). @tola/document’s headings() only sees native heading elements and references() only sees native link and ref elements—writing html.h2 or html.a in prose omits those items from tables of contents, backlinks, and PDF exports.
  • In layout templates and shells (site.typ, templates/, components/), treat Typst as an HTML generator and write html.header, html.nav, html.main, html.aside, and html.elem directly for exact DOM structure, classes, aria-*, and data-* attributes without accidental <p> wrappers.
  • Place custom components in between: expose a semantic function (#callout(kind: "warning")[...]) whose implementation branches on target() to emit html.* or paged blocks.

Math

Equations export to native <math> (MathML) by default: text remains selectable and no client script is needed. MathML uses browser fonts rather than [typst.fonts]; publish web math fonts via [assets] with @font-face if you need consistent rendering across platforms.

When pixel-identical Typst layout is required for specific equations, wrap them in inline SVGs via html.frame:

typst
#show math.equation: eq => if eq.at("block", default: false) {
  block(html.frame(eq))
} else {
  box(html.frame(eq))
}

html.frame(...) produces an SVG image using [typst.fonts] (paged layout rules like set page cannot appear inside) and drops selectable text and native MathML semantics.

Search this site

Use the arrow keys to move through results.

Type to search.