What is the difference between #()
and #{}
in Typst and when should each be used? The documentation uses both syntaxes next to each other:
To embed these into markup, you can use parentheses, as in #(1 + 2).
Code block: { let x = 1; x + 2 }
If I understand correctly, both can handle simple expressions:
One plus one is #(1+1).
One plus one is #{1+1}.
But only #{}
can handle code consisting of multiple expressions, such as
#{
let x = "hi"
x.len()
}
So is there any scenario where #()
is preferred or even the only option?
It may be easier to think of #(expr)
as a shorthand for #{(expr)}
. Your question can then be rephrased as, "when are ()
necessary in code mode?"
(1 + 2)
has the same value as 1 + 2
because both are evaluations of a single expression. However, syntax structures like arrays and dictionaries require parentheses:
These will not work with `{}` since the syntax is defined by `()`:
// Dictionary
#(a: 1, b: 2)
// Array
#(1, 2, 3)
// Array in code mode
But you can also do this: #{(1,2,3)}