When working with Typst templates in Quarto, you’ll inevitably encounter special characters that have syntactic meaning in Typst. The dollar sign ($
) is a perfect example—essential for monetary values but problematic in templates.
If you’re new to creating Typst templates, the official Typst tutorial provides an excellent introduction, and the Quarto documentation covers how to integrate custom Typst templates with your Quarto workflow.
Special Characters in Typst Templates
While creating Typst templates with $
for monetary values, you might run into issueswhen trying to include the dollar sign directly in your template. Instinctively, you might aim to try:
$50.00" // Direct usage
let price = "let price = "\$50.00" // Backslash escape
Both approaches can trigger compilation errors like:
Error compiling template "/var/folders/b0/vt_1hj2d6yd8myx9lwh81pww0000gn/T/quarto-session5507649961f270aa/3d2f7f01b738eb46/6c7df7952a282c34/typst-template.typ" (line 64, column 24):
unexpected "\"" expecting letter or digit or "()"
Escape Sequences with Hexadecimal Values to the Rescue
The reliable workaround is to use hexadecimal escape sequences. For the dollar sign ($
), you can use:
\u{24}
This tells Typst to render the Unicode character with hexadecimal code 24
, which corresponds to the dollar sign ($
). This is one of two types of escape sequences available in Typst. The other type is single-character escapes, which is used for single characters like \n
for newline or \t
for tab. You can find the reference page for both escape sequence types in the official Typst syntax documentation.
With this in mind, we can define a variable in a Typst template like this:
\u{24}50.00" // Hexadecimal escape let price = "
Finding Hexadecimal Codes
But, how do you find a hexadecimal code for a character like the dollar sign? Use Typst’s handy ASCII table tool to look up characters and their hexadecimal codes:
https://typst.app/tools/ascii-table/
Let’s walk through how to find the hexadecimal code for the dollar sign ($
):
- Open a web browser and navigate to Typst’s ASCII table tool linked above
- Type into the search bar or scan for your desired character (in our case,
$
) - Look at the hexadecimal value shown (you’ll see something like
0x24
) - Take the part after
0x
(so24
from0x24
) - Use it in the escape sequence:
\u{24}
Alternatively, we can click on the character in the ASCII table, which will open a dialog showing the character’s details, including its Typst escape sequence.
Including the Hexadecimal Codes in Typst
With the hexadecimal escape sequence, you can now safely include the dollar sign in your Typst templates without worrying about compilation errors. Some practical examples of when escape characters come in handy to use in Typst are when defining variables, using them in functions, or including them in text.
For financial content like budgets or prices, you can define variables with the dollar sign like this:
\u{24}1,500 - \u{24}2,000" let budget = "Budget:
For dynamic content like user inputs or calculations, you can use it in functions to format monetary values:
#let currency(amount) = box[\u{24}#amount]
#currency("99.99") // Renders: $99.99
For everyday usage in text, you can include it directly in your content:
\u{40}company.com for pricing starting at \u{24}50. Contact us at info
Other Useful Escape Sequences
For my own work, I often need to include other special characters in Typst templates. Here are some common characters and their hexadecimal escape sequences that I find useful:
Character | Hex Code | Escape Sequence |
---|---|---|
# (hash) |
23 |
\u{23} |
$ (dollar) |
24 |
\u{24} |
@ (at sign) |
40 |
\u{40} |
% (percent) |
25 |
\u{25} |
& (ampersand) |
26 |
\u{26} |
Fin
Hexadecimal escape sequences (\u{XX}
) are your reliable solution when standard escaping fails in Typst templates. They work consistently across different template contexts and eliminate parsing ambiguities. Bookmark Typst’s ASCII table for quick hex code lookups, and your template compilation errors will become a thing of the past.