TypstGenerator.jl

Generate Typst documents with julia
Author onecalfman
Popularity
9 Stars
Updated Last
3 Months Ago
Started In
July 2023

TypstGenerator

Build Status

This package can be used to generate Typst through julia code.

Basic usage

The function naming follows the typst function names.

using TypstGenerator
t1 = text("some text")
t2 = text("some text", size = 20pt)
t3 = text("some text", size = 20pt, fill = :red)

A difference from typst however is, that the content always comes first. An arbitrary number of named arguments can be supplied. These will just be converted into typst syntax. This package currently doesn't check the validity of the supplied option in julia itself. Refer to the typst documentation eg. text for valid options.

The elements are then rendered to String using the typst function.

typst([t1,t2,t3])

Listlike

Elements like grid, table or page accept lists of typst arguments.

block(
    grid(map(x -> text("field $(x)"), 1:3),
        columns = [fr(1), fr(2), fr(5)],
        row_gutter = 3mm,
    ),
    line(length = 0.5),
    grid(map(x -> text("field $(x)"), 4:12),
        columns = [fr(1), fr(2), fr(5)],
        row_gutter = 3mm,
    )
)

Column or row width can be specified using the fr function, following the css flex logic. Multiple columns with the same fraction can also be constructed with fr(1,n). Note that arguments, that are written with a - in typst are written with _ in julia. Floats are interpretted as percent when given as an named argument.

set

block(
    set(text, size = 10pt),
    "10pt text",
    "10pt text",
    set(text, size = 15pt, fill = cmyk(0.9,0.2,0.4,0.2)),
    "50pt text",
    literal("#overline[literal]"),
    set(text, size = 10pt),
    "colored text",
)

Set syntax works pretty similar to typst itself. For arbitrary typst commands use the literal function. Strings in typst functions will be interpreted as text.

figures

page(
    figure(
        grid(map(x -> square(
            align(
                scale(
                    math(" A = pi r^2 "), 
                x = x/20, y = x/20), 
            :bottom), 
            size = x * mm), 5:5:30),
        columns = fr.(1:length(5:5:30)),
        ),
        label = :label1,
        caption = text("a figure")
    ),
    lorem(20),
    lorem(20),
    ref(:label1)
)

Figures can be referenced, using the label option with some symbol.

Example

Check the example folder for a more complex and comprehensive example.

For a full list of functions check the TypstGenerator.jl file.

Things that don't yet work.

  • Text styles besides text. As of today only text is implemented. Empy, overline etc are not yet supported.
  • References when a figure is in an align.
  • repeat
  • measure
  • counter
  • layout
  • locate
  • query
  • selector
  • state
  • style

Required Packages

Used By Packages

No packages found.