Typst Notes
Overview
- Typst documentation
- trigger autocomplete with
Ctrl+Space
Writing
- Simply write content directly
- Format content/text with markup
- Typst uses its own markup syntax
- i.e., specific characters (symbols) have specific meaning
- used only for the most common and simple formatting tasks
- Can add:
- headings
- text
- links
- code
- math
- lists
- etc.
- uses own math format syntax
- Everything else other than markup is inserted with functions
- functions produce output for a set of arguments
- call a function with
#character- i.e.,
#function(argument, arg2) - only needed in direct markup
- i.e.,
- Typst recognizes many different data types in argument lists
- has named arguments
- specified as
name: valuepairs - e.g.,
#function(name: argument)
- specified as
- within the argument list of a function, Typst is already in code mode
- means don’t need to use a hash for additional function calls
- functions may take arbitrary markup as an argument
- to pass markup to a function, enclose in square brackets
[] - e.g.,
#figure(image("img.jpg", width: 70%), caption: [arbitrary markup _text_.]) - this is called a content block
- alt syntax is to add the content block after the argument list
- e.g.,
#function(argument)[content]
- e.g.,
- to pass markup to a function, enclose in square brackets
- title function sets the title of the document
- titles only appear once vs headings multiple times
#title[content here]- typically store in document metadata
#set document(title: [content here])- retrieve with
#title() - use parentheses when no argument is passed
- can reference elements in a document by adding a label to it and calling that label
- add a label after an element enclosed in angle brackets
<>- e.g.,
<image1>
- e.g.,
- reference the element with
@and name- e.g.,
@image1
- e.g.,
- add a label after an element enclosed in angle brackets
Formatting
Set Rules
- use set rules to apply style properties to all occurrences of some kind of content
- use
setkeyword followed by name of function whose properties you want to set- e.g.,
#set text(font: Arial)
- e.g.,
- Common functions for set rules:
text- font, size, color, etc.
page- page size, margins, headers, columns, footers
par- justify paragraph, set line spacing, etc.
heading- set appearance of headings, add numbering
document- set metadata contained in PDF
- title, author
- set metadata contained in PDF
- only functions that tell how to do something can be set
- not what to do
- use
Show Rules
- show rules redefine how Typst displays certain elements
- specify:
- which element to show differently
- how to it should look
- specify:
- can be applied to:
- instances of text
- functions
- whole document
- syntax:
showkeyword- element to show differently + colon
:- can be text, function, etc.
- e.g.,
#show "string of text": function and content
- a function that takes the content that should be shown
Show-Set Rules
- show-set rules are used to customize properties of an element inside of another element
- use
showkeyword to select which element to customize- called a selector
- colon
: - set rule that should apply to element matching the selector
- e.g.,
#show your-selector: set some-element(*/...*/)
- use
- e.g.,
#show title: set text(size: 17pt)
Context
- context allows retrieving the content on values set on elements before
- uses
contextkeyword - allows access any property of any element
- including document
- e.g.,
#set document(title: [title here])... context document.title
Templates
Variables
- can use variables to store data for reuse later
- define variable with
let- e.g.,
#let var1 = [some content]
- e.g.,
- call variable with
#and its name- e.g,.
#var1
- e.g,.
Custom Functions
- create custom function with
let - can set optional named parameters as default
- custom function:
#let amazed(term, color: blue) = {
text(color, [#term])
}
-
the above outputs a colored term passed into it and defaults to blue
- within the function, the term argument is called to output within the content block
-
call the custom function:
#amazed[beautiful]without optional argument#amazed(color: purple)[beautiful]with custom color argument
-
templates work by wrapping the whole document in a custom function
- uses an “everything” show rule
- same as wrapping entire document in the function
- syntax:
#show: function
- uses an “everything” show rule
Embed Set and Show Rules
- to apply set and show rules to a template
- use
setandshowwithin a content block in a function - then insert the document into that content block
- use
#let template(doc) = [
#set text(font: "Inria Serif")
#show "something cool": [Typst]
#doc
]
#show: template
I am learning something cool today.
It's going great so far!
Separate Template File
- best to create a template as a separate file from the content of its instance
- import into the instance document
- use
importkeyword- e.g.,
#import "template.typ": template
- e.g.,
Conditionals
- Typst supports
if,else if, andelsestatements - syntax:
#if 1 < 2 [
This is shown if true
] else [
This is shown if false
]Loops
- used to repeat content or computer something iteratively
- two loops:
ifandwhile - syntax:
// For loop
#for c in "ABC" [
#c is a character.
]
// Outputs: "A is a character. B is a character. C is a character"
// While loop
#let n = 2
#while n < 10 {
n = (n * 2) - 1
(n,)
}
// Outputs: (3, 5, 9, 17)forloops can iterate over a variety of collections:for value in array {..}for pair in dict {..}for letter in "abc" {..}for byte in bytes("😃") {..}
- supports
breakandcontinuestatements - body of a loop can be:
- code block :
for .. in collection {..}while condition {..}
- content block:
for .. in collection [..]while condition [..]
- code block :