Typst Notes


Overview

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
    • Typst recognizes many different data types in argument lists
    • has named arguments
      • specified as name: value pairs
      • e.g., #function(name: argument)
    • 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]
    • 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>
    • reference the element with @ and name
      • e.g., @image1

Formatting

Set Rules

  • use set rules to apply style properties to all occurrences of some kind of content
    • use set keyword followed by name of function whose properties you want to set
      • e.g., #set text(font: Arial)
    • 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
    • only functions that tell how to do something can be set
      • not what to do

Show Rules

  • show rules redefine how Typst displays certain elements
    • specify:
      1. which element to show differently
      2. how to it should look
  • can be applied to:
    • instances of text
    • functions
    • whole document
  • syntax:
    • show keyword
    • 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 show keyword 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(*/...*/)
  • e.g.,
    • #show title: set text(size: 17pt)

Context

  • context allows retrieving the content on values set on elements before
  • uses context keyword
  • 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]
  • call variable with # and its name
    • e.g,. #var1

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

Embed Set and Show Rules

  • to apply set and show rules to a template
    • use set and show within a content block in a function
    • then insert the document into that content block
#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 import keyword
    • e.g., #import "template.typ": template

Conditionals

  • Typst supports if, else if, and else statements
  • 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: if and while
  • 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)
  • for loops can iterate over a variety of collections:
    • for value in array {..}
    • for pair in dict {..}
    • for letter in "abc" {..}
    • for byte in bytes("😃") {..}
  • supports break and continue statements
  • body of a loop can be:
    • code block :
      • for .. in collection {..}
      • while condition {..}
    • content block:
      • for .. in collection [..]
      • while condition [..]