Types, part I

Each value in Typst has a type. You don't have to specify it, but it is important.

Content (content)

Link to Reference.

We have already seen it. A type that represents what is displayed in document.

#let c = [It is _content_!]

// Check type of c
#(type(c) == content)

#c

// repr gives an "inner representation" of value
#repr(c)
Rendered image

Important: It is very hard to convert content to plain text, as content may contain anything! So be careful when passing and storing content in variables.

None (none)

Nothing. Also known as null in other languages. It isn't displayed, converts to empty content.

#none
#repr(none)
Rendered image

String (str)

Link to Reference.

String contains only plain text and no formatting. Just some chars. That allows us to work with chars:

#let s = "Some large string. There could be escape sentences: \n,
 line breaks, and even unicode codes: \u{1251}"
#s \
#type(s) \
`repr`: #repr(s)

#let s = "another small string"
#s.replace("a", sym.alpha) \
#s.split(" ") // split by space
Rendered image

You can convert other types to their string representation using this type's constructor (e.g. convert number to string):

#str(5) // string, can be worked with as string
Rendered image

Boolean (bool)

Link to Reference.

true/false. Used in if and many others

#let b = false
#b \
#repr(b) \
#(true and not true or true) = #((true and (not true)) or true) \
#if (4 > 3) {
  "4 is more than 3"
}
Rendered image

Integer (int)

Link to Reference.

A whole number.

The number can also be specified as hexadecimal, octal, or binary by starting it with a zero followed by either x, o, or b.

#let n = 5
#n \
#(n += 1) \
#n \
#calc.pow(2, n) \
#type(n) \
#repr(n)
Rendered image
#(1 + 2) \
#(2 - 5) \
#(3 + 4 < 8)
Rendered image
#0xff \
#0o10 \
#0b1001
Rendered image

You can convert a value to an integer with this type's constructor (e.g. convert string to int).

#int(false) \
#int(true) \
#int(2.7) \
#(int("27") + int("4"))
Rendered image

Float (float)

Link to Reference.

Works the same way as integer, but can store floating point numbers. However, precision may be lost.

#let n = 5.0

// You can mix floats and integers, 
// they will be implicitly converted
#(n += 1) \
#calc.pow(2, n) \
#(0.2 + 0.1) \
#type(n) 
Rendered image
#3.14 \
#1e4 \
#(10 / 4)
Rendered image

You can convert a value to a float with this type's constructor (e.g. convert string to float).

#float(40%) \
#float("2.7") \
#float("1e5")
Rendered image