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:
#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@linkswhen#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,letbindings emit nothing, and remaining expressions concatenate. Arrays must be joined explicitly withforor.join().
Here is a callout component and a link list helper:
#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:
#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)
]
]#import "/templates/page.typ": page-layout
#document("index.html", format: "html", title: "Home")[
#show: page-layout.with("Home")
#include "/content/index.typ"
]importbrings exports into scope without inheriting the caller’s variables;includeinserts 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. setchanges default parameters for subsequent elements, whileshowintercepts matching elements; an un-targeted#show: fnpasses the rest of the document body intofn.
context
Queries depending on document location or whole-site state (current-document(), headings(), references(), counters) must run inside context:
#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:
#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.scriptandhtml.styletake string bodies; pass an empty string for external scripts:html.script("", src: url, defer: true).- Use
html.a(...)when you need raw HTML attributes liketargetorrel. - 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’sheadings()only sees nativeheadingelements andreferences()only sees nativelinkandrefelements—writinghtml.h2orhtml.ain 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 writehtml.header,html.nav,html.main,html.aside, andhtml.elemdirectly for exact DOM structure, classes,aria-*, anddata-*attributes without accidental<p>wrappers. - Place custom components in between: expose a semantic function (
#callout(kind: "warning")[...]) whose implementation branches ontarget()to emithtml.*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:
#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.