Home

Awesome

Tools for Typst (v0.3.2)

A utility package for typst package authors.

Tools for Typst (t4t in short) is a utility package for Typst package and template authors. It provides solutions to some recurring tasks in package development.

The package can be imported or any useful parts of it copied into a project. It is perfectly fine to treat t4t as a snippet collection and to pick and choose only some useful functions. For this reason, most functions are implemented without further dependencies.

Hopefully, this collection will grow over time with Typst to provide solutions for common problems.

Usage

Either import the package from the Typst preview repository:

#import "@preview/t4t:0.3.2": *

If only a few functions from t4t are needed, simply copy the necessary code to the beginning of the document.

Reference


Note: This reference might be out of date. Please refer to the manual for a complete overview of all functions.


The functions are categorized into different submodules that can be imported separately.

The modules are:

Any or all modules can be imported the usual way:

// Import as "t4t"
#import "@preview/t4t:0.3.2"
// Import all modules
#import "@preview/t4t:0.3.2": *
// Import specific modules
#import "@preview/t4t:0.3.2": is, def

In general, the main value is passed last to the utility functions. #def.if-none(), for example, takes the default value first and the value to test second. This is somewhat counterintuitive at first, but allows the use of .with() to generate derivative functions:

#let is-foo = eq.with("foo")

Test functions

#import "@preview/t4t:0.3.2": is

These functions provide shortcuts to common tests like #is.eq(). Some of these are not shorter as writing pure Typst code (e.g. a == b), but can easily be used in .any() or .find() calls:

// check all values for none
if some-array.any(is-none) {
	...
}

// find first not none value
let x = (none, none, 5, none).find(is.not-none)

// find position of a value
let pos-bar = args.pos().position(is.eq.with("|"))

There are two exceptions: is-none and is-auto. Since keywords can't be used as function names, the is module can't define a function to do is.none(). Therefore the methods is-none and is-auto are provided in the base module of t4t:

#import "@preview/t4t:0.3.2": is-none, is-auto

The is submodule still has these tests, but under different names (is.n and is.non for none and is.a and is.aut for auto).

Default values

#import "@preview/t4t:0.3.2": def

These functions perform a test to decide if a given value is invalid. If the test passes, the default is returned, the value otherwise.

Almost all functions support an optional do argument, to be set to a function of one argument, that will be applied to the value if the test fails. For example:

// Sets date to a datetime from an optional
// string argument in the format "YYYY-MM-DD"
#let date = def.if-none(
	datetime.today(), 	// default
	passed_date, 		// passed-in argument
	do: (d) >= {		// post-processor
		d = d.split("-")
		datetime(year=d[0], month=d[1], day=d[2])
	}
)

Assertions

#import "@preview/t4t:0.3.2": assert

This submodule overloads the default assert function and provides more asserts to quickly check if given values are valid. All functions use assert in the background.

Since a module in Typst is not callable, the assert function is now available as assert.that(). assert.eq and assert.ne work as expected.

All assert functions take an optional argument message to set the error message shown if the assert fails.

Element helpers

#import "@preview/t4t:0.3.2": get

This submodule is a collection of functions, that mostly deal with content elements and get some information from them. Though some handle other types like dictionaries.

Math functions

#import "@preview/t4t:0.3.2": math

Some functions to complement the native calc module.

Alias functions

#import "@preview/t4t:0.3.2": alias

Some of the native Typst function as aliases, to prevent collisions with some common argument names.

For example using numbering as an argument is not possible if the value is supposed to be passed to the numbering() function. To still allow argument names that are in line with the common Typst names (like numbering, align ...), these alias functions can be used:

#let excercise( no, numbering: "1)" ) = [
	Exercise #alias.numbering(numbering, no)
]

The following functions have aliases right now:

Changelog

Version 0.3.1

Version 0.3.0

Version 0.2.0

Version 0.1.0