Documentation

Syntax

Typst is a markup language. This means that you can use simple syntax to accomplish common layout tasks. The lightweight markup syntax is complemented by set and show rules, which let you style your document easily and automatically. All this is backed by a tightly integrated scripting language with built-in and user-defined functions.

Modes

Typst has three syntactical modes: Markup, math, and code. Markup mode is the default in a Typst document, math mode lets you write mathematical formulas, and code mode lets you use Typst's scripting features.

You can switch to a specific mode at any point by referring to the following table:

New modeSyntaxExample
CodePrefix the code with #Number: #(1 + 2)
MathSurround equation with $..$$-x$ is the opposite of $x$
MarkupSurround markup with [..]let name = [*Typst!*]

Once you have entered code mode with #, you don't need to use further hashes unless you switched back to markup or math mode in between.

Markup

Typst provides built-in markup for the most common document elements. Most of the syntax elements are just shortcuts for a corresponding function. The table below lists all markup that is available and links to the best place to learn more about their syntax and usage.

NameExampleSee
Paragraph breakBlank lineparbreak
Strong emphasis*strong*strong
Emphasis_emphasis_emph
Raw text`print(1)`raw
Linkhttps://typst.app/link
Label<intro>label
Reference@introref
Heading= Headingheading
Bullet list- itemlist
Numbered list+ itemenum
Term list/ Term: descriptionterms
Math$x^2$Math
Line break\linebreak
Smart quote'single' or "double"smartquote
Symbol shorthand~, ---Symbols
Code expression#rect(width: 1cm)Scripting
Character escapeTweet at us \#adBelow
Comment/* block */, // lineBelow

Math mode

Math mode is a special markup mode that is used to typeset mathematical formulas. It is entered by wrapping an equation in $ characters. This works both in markup and code. The equation will be typeset into its own block if it starts and ends with at least one space (e.g. $ x^2 $). Inline math can be produced by omitting the whitespace (e.g. $x^2$). An overview over the syntax specific to math mode follows:

NameExampleSee
Inline math$x^2$Math
Block-level math$ x^2 $Math
Bottom attachment$x_1$attach
Top attachment$x^2$attach
Fraction$1 + (a+b)/5$frac
Line break$x \ y$linebreak
Alignment point$x &= 2 \ &= 3$Math
Variable access$#x$, $pi$Math
Field access$arrow.r.long$Scripting
Implied multiplication$x y$Math
Symbol shorthand$->$, $!=$Symbols
Text/string in math$a "is natural"$Math
Math function call$floor(x)$Math
Code expression$#rect(width: 1cm)$Scripting
Character escape$x\^2$Below
Comment$/* comment */$Below

Code mode

Within code blocks and expressions, new expressions can start without a leading # character. Many syntactic elements are specific to expressions. Below is a table listing all syntax that is available in code mode:

NameExampleSee
Nonenonenone
Autoautoauto
Booleanfalse, truebool
Integer10, 0xffint
Floating-point number3.14, 1e5float
Length2pt, 3mm, 1em, ..length
Angle90deg, 1radangle
Fraction2frfraction
Ratio50%ratio
String"hello"str
Label<intro>label
Math$x^2$Math
Raw text`print(1)`raw
Variable accessxScripting
Code block{ let x = 1; x + 2 }Scripting
Content block[*Hello*]Scripting
Parenthesized expression(1 + 2)Scripting
Array(1, 2, 3)Array
Dictionary(a: "hi", b: 2)Dictionary
Unary operator-xScripting
Binary operatorx + yScripting
Assignmentx = 1Scripting
Field accessx.yScripting
Method callx.flatten()Scripting
Function callmin(x, y)Function
Argument spreadingmin(..nums)Arguments
Unnamed function(x, y) => x + yFunction
Let bindinglet x = 1Scripting
Named functionlet f(x) = 2 * xFunction
Set ruleset text(14pt)Styling
Set-if ruleset text(..) if .. Styling
Show-set ruleshow heading: set block(..)Styling
Show rule with functionshow raw: it => {..}Styling
Show-everything ruleshow: templateStyling
Context expressioncontext text.langContext
Conditionalif x == 1 {..} else {..}Scripting
For loopfor x in (1, 2, 3) {..}Scripting
While loopwhile x < 10 {..}Scripting
Loop control flowbreak, continueScripting
Return from functionreturn xFunction
Include moduleinclude "bar.typ"Scripting
Import moduleimport "bar.typ"Scripting
Import items from moduleimport "bar.typ": a, b, cScripting
Comment/* block */, // lineBelow

Comments

Comments are ignored by Typst and will not be included in the output. This is useful to exclude old versions or to add annotations. To comment out a single line, start it with //:

// our data barely supports
// this claim

We show with $p < 0.05$
that the difference is
significant.
Preview

Comments can also be wrapped between /* and */. In this case, the comment can span over multiple lines:

Our study design is as follows:
/* Somebody write this up:
   - 1000 participants.
   - 2x2 data design. */
Preview

Escape sequences

Escape sequences are used to insert special characters that are hard to type or otherwise have special meaning in Typst. To escape a character, precede it with a backslash. To insert any Unicode codepoint, you can write a hexadecimal escape sequence: \u{1f600}. The same kind of escape sequences also work in strings.

I got an ice cream for
\$1.50! \u{1f600}
Preview

Paths

Typst has various features that require a file path to reference external resources such as images, Typst files, or data files. Paths are represented as strings. There are two kinds of paths: Relative and absolute.

Project root

By default, the project root is the parent directory of the main Typst file. For security reasons, you cannot read any files outside of the root directory.

If you want to set a specific folder as the root of your project, you can use the CLI's --root flag. Make sure that the main file is contained in the folder's subtree!

typst compile --root .. file.typ

In the web app, the project itself is the root directory. You can always read all files within it, no matter which one is previewed (via the eye toggle next to each Typst file in the file panel).

Paths and packages

A package can only load files from its own directory. Within it, absolute paths point to the package root, rather than the project root. For this reason, it cannot directly load files from the project directory. If a package needs resources from the project (such as a logo image), you must pass the already loaded image, e.g. as a named parameter logo: image("mylogo.svg"). Note that you can then still customize the image's appearance with a set rule within the package.

In the future, paths might become a distinct type from strings, so that they can retain knowledge of where they were constructed. This way, resources could be loaded from a different root.