Typst - Reusability

04/20/2024, Sat
Categories: #shell #typst

Sharing String Contents

Typst offers the capability of content reuse by allowing for file imports. It makes extensive use of functions to perform special manipulation of text.

Shared Function - Load Function from File

Reusable content can be defined in another file within a function. This function when called will yield the shared content.

// shared-function.typ

#let content() = {
[ 
  = Content from another file
 Shared appears here.
 ]
}

The sample.typ will load the content from shared-function.typ.

// sample.typ

#import("shared-function.typ"): *
#content()

Define Variables within File

If there is only a need to localize shared content inside one file, a variable definition will be sufficient.

#let coolStuff = "beyond cool"
#let amazing = "This is so " + coolStuff + "!"

This is the usual text, but this content below is the dynamic portion:

#amazing

Importing External File Types

Typst has the built-in ability to read data type files such as json.

The below person.json information can be defined as

{
  "name": "Xavier"
}

which then can then be loaded into the sample.typ file

#let personData = json("person.json")
#personData.name