Home

Awesome

Tools for Typst (v0.4.1)

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.4.1": *

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.4.1"
// Import all modules
#import "@preview/t4t:0.4.1": *
// Import specific modules
#import "@preview/t4t:0.4.1": 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.4.1": *
#import "@preview/t4t:0.4.1": test

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(not-none)

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

Some of the more frequently used tests are available as top-level imports from t4t. The rest can be found in the test module.

#import "@preview/t4t:0.4.1"

#if t4t.is-none(none) [
	it is none
]

The following tests are available in the base t4t module:

Default values

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

These functions perform a test to decide if a given value is invalid. If the test passes, the default def is returned, 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(
	passed_date, 		// passed-in argument
	def: datetime.today(), 	// default
	do: (d) >= {		// post-processor
		d = d.split("-")
		datetime(year=d[0], month=d[1], day=d[2])
	}
)

Note that previous versions of these functions got the default passed in as the first positional argument. Since Version 0.4.0 the default is now a named argument in favor of more readable code. To restore the old behaviour, you can import the def.compat module as def:

#import "@preview/t4t:0.4.1": def
#import def.compat as def

Assertions

#import "@preview/t4t:0.4.1": 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.4.1": 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.4.1": math

Some functions to complement the native calc module.

Changelog

Version 0.4.1

Version 0.4.0

Version 0.3.3

Version 0.3.2

Version 0.3.1

Version 0.3.0

Version 0.2.0

Version 0.1.0