Typst basics
Typst Set and Show Rules, Explained with Examples
Almost all styling in Typst comes down to two rules. A set rule changes the default arguments of an element, and a show rule replaces how an element looks. Once both make sense, templates stop being magic: a template is just a function full of set and show rules.
Set rules change defaults
Every Typst element is a function with arguments, and #set gives those arguments new defaults from that point on. Instead of passing a size to every piece of text, you set it once:
#set page(paper: "a4", margin: 2.5cm, numbering: "1")
#set text(font: "New Computer Modern", size: 11pt, lang: "en")
#set par(justify: true, leading: 0.65em)
#set heading(numbering: "1.1")Anything you can pass to page, text, par or heading can be set this way. A set rule only affects content that comes after it, so the usual place for them is the top of the main file.
Set rules are scoped
A set rule inside a content block or a code block lasts until the end of that block. That makes local changes easy, with nothing to reset afterwards:
Regular paragraph.
#[
#set text(fill: gray, size: 9pt)
This aside is small and grey.
]
Back to normal here.This is the Typst answer to LaTeX's {\small ...} groups and \begingroup. Scoping follows the brackets you can see, so there is no state to leak.
Show rules replace how something looks
A show rule targets a selector (an element, some text or a label) and transforms it. The simplest form applies a set rule only to the selected element, which Typst calls show-set:
#show heading: set text(fill: rgb("#0b8796"))
#show link: underline
#show raw: set text(font: "JetBrains Mono")For full control, give the show rule a function. It receives the element as it, and whatever the function returns is shown instead. Fields such as it.body give you the original content:
#show heading.where(level: 1): it => {
pagebreak(weak: true)
block(below: 1.2em)[
#text(size: 22pt, weight: "bold", it.body)
]
}where narrows the selector by an element's fields, here only level-one headings. pagebreak(weak: true) starts each chapter on a new page without leaving a blank page at the very start of the document.
Other selectors: text, regex and labels
- A string replaces exact text:
#show "Kern": smallcapssets every occurrence in small caps. - A regex matches patterns:
#show regex("\d+%"): strongmakes percentages bold. - A label selects one element you tagged: write
= Results <results>, then#show <results>: set text(red).
Text and regex show rules are handy, but broad ones can slow compiles on long documents. Prefer element selectors where you can.
The everything show rule, or how templates work
A show rule with no selector receives the rest of the document as its argument. That is how a template wraps a whole file: define a function that takes the document, apply your rules inside it, and return the content.
// template.typ
#let report(title: none, author: none, body) = {
set document(title: title, author: author)
set page(numbering: "1")
set heading(numbering: "1.1")
align(center, text(20pt, weight: "bold", title))
body
}
// main.typ
#import "template.typ": report
#show: report.with(title: [Field notes], author: "A. Student")
= Introduction
...report.with(...) fills in the named arguments and leaves body for the show rule to pass. Every template on Typst Universe, and every template in Kern's gallery, follows this pattern.
Order matters
- Rules apply to content after them, so put them before the text they should affect.
- A later set rule for the same argument wins over an earlier one.
- Set rules inside a template apply before the rules you write after
#show: template, so your own rules can still override the template's.
Trying rules on your phone
Styling is easiest to learn by changing a value and watching the result. Kern runs the Typst compiler on the device and the preview follows every keystroke, so you can adjust a show rule and see it at once, offline. Explain at cursor opens Typst's documentation for the function under the cursor, which helps when you're unsure which arguments text or page take.
New to Typst? Start with What Is Typst?, then keep the math cheat sheet at hand.