Basics

Variables I

Let's start with variables.

The concept is very simple, just some value you can reuse:

#let author = "John Doe"

This is a book by #author. #author is a great guy.

#quote(block: true, attribution: author)[
  \<Some quote\>
]
Rendered image

Variables II

You can store any Typst value in variable:

#let block_text = block(stroke: red, inset: 1em)[Text]

#block_text

#figure(caption: "The block", block_text)
Rendered image

Functions

We have already seen some "custom" functions in Advanced Styling chapter.

Functions are values that take some values and output some values:

// This is a syntax that we have seen earlier
#let f = (name) => "Hello, " + name

#f("world!")
Rendered image

Alternative syntax

You can write the same shorter:

// The following syntaxes are equivalent
#let f = (name) => "Hello, " + name
#let f(name) = "Hello, " + name

#f("world!")
Rendered image