Braces, brackets and default
Square brackets
You may remember that square brackets convert everything inside to content.
#let v = [Some text, _markup_ and other #strong[functions]]
#v
We may use same for functions bodies:
#let f(name) = [Hello, #name]
#f[World] // also don't forget we can use it to pass content!
Important: It is very hard to convert content to plain text, as content may contain anything! Sp be careful when passing and storing content in variables.
Braces
However, we often want to use code inside functions.
That's when we use {}
:
#let f(name) = {
// this is code mode
// First part of our output
"Hello, "
// we check if name is empty, and if it is,
// insert placeholder
if name == "" {
"anonym"
} else {
name
}
// finish sentence
"!"
}
#f("")
#f("Joe")
#f("world")
Scopes
This is a very important thing to remember.
You can't use variables outside of scopes they are defined (unless it is file root, then you can import them). Set and show rules affect things in their scope only.
#{
let a = 3;
}
// can't use "a" there.
#[
#show "true": "false"
This is true.
]
This is true.
Return
Important: by default braces return anything that "returns" into them. For example,
#let change_world() = {
// some code there changing everything in the world
str(4e7)
// another code changing the world
}
#let g() = {
"Hahaha, I will change the world now! "
change_world()
" So here is my long evil monologue..."
}
#g()
To avoid returning everything, return only what you want explicitly, otherwise everything will be joined:
#let f() = {
"Some long text"
// Crazy numbers
"2e7"
return none
}
// Returns nothing
#f()
Default values
What we made just now was inventing "default values".
They are very common in styling, so there is a special syntax for them:
#let f(name: "anonym") = [Hello, #name!]
#f()
#f(name: "Joe")
#f(name: "world")
You may have noticed that the argument became named now. In Typst, named argument is an argument that has default value.