Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Text, math and code

Text is typeset with Typst, using bundled fonts; no LaTeX install is needed. Each text is laid out into per-glyph shapes, split into tokens that diff transforms can match up.

FunctionTypesets
text(s)Plain text
math_tex(s)Math, in Typst math syntax
latex(s)Math, in LaTeX syntax (converted to Typst by MiTeX)
code(src, lang)Syntax-highlighted source code, e.g. code(src, "rust")
list([v1, v2, ...])A row of boxed values, centered on the origin
let a = math_tex("a = sqrt(c^2 - b^2)");
scene.add(a);
scene.add(latex(`\frac{a}{b} + \sqrt{c}`).next_to(a, DOWN));
scene.add(list([3, 1, 2]).to_edge(DOWN));

Rhai backtick strings span lines, which suits code:

const SRC = `fn main() {
    println!("hi");
}`;
let c = scene.add(code(SRC, "rust").scale(0.8));

Fonts

text, math_tex, latex and code take an options map as their last argument. Its one option so far is font, a font family name: one of the bundled fonts (Libertinus Serif, New Computer Modern, New Computer Modern Math, DejaVu Sans Mono), or one loaded from a file with load_font(path). load_font takes a TTF, OTF, TTC or OTC file, relative to the script’s directory, and returns the families it holds; loading the same file again does nothing.

let families = load_font("fonts/Inter-Regular.ttf");   // ["Inter"]
let title = scene.add(text("Hello", #{font: "Inter"}).to_edge(UP));
scene.add(code("let x = 1;", "rust", #{font: "DejaVu Sans Mono"}));

An unknown family is an error rather than a silent fallback. Math in another font falls back to New Computer Modern Math for the symbols that font lacks. Fonts can’t be loaded in the browser playground yet.

Methods

Texts take the positioning methods, plus:

MethodEffect
.fill(color)Color every glyph
.rotate(angle)Rotate about the center
.glyphsAn array of the glyph shapes

Animating text

Most animations apply to every glyph at once. write and unwrite draw glyph by glyph; move_to, scale and rotate move the text as a whole.

A text cannot be read back with scene.get, used with scene.always, or passed to transform or replacement_transform; those need shapes. Use transform_diff to change a text into another.