Skip to content

Instantly share code, notes, and snippets.

@ckunte
Created October 18, 2024 05:42
Show Gist options
  • Save ckunte/6f7a1e8d3a3053d92c70c6b9ea5138ef to your computer and use it in GitHub Desktop.
Save ckunte/6f7a1e8d3a3053d92c70c6b9ea5138ef to your computer and use it in GitHub Desktop.
Build Typst

Typst to PDF on MacOS and Windows

Typst can be installed on MacOS with brew. Typst package for Sublime Text provides the build setup needed to compile typ file to a pdf. For older MacOS, or Windows, see below.

Using Sublime Text on older MacOS

My 12+y old laptop, long out-of-date and warranty, is better than a Raspberry Pi for desktop computing in every practicable way. For writing, editing, scripting, typesetting, and for terminal applications, it works just fine.

Last October, I replaced its cable-frayed, obsolete power adapter with a USB-C charger, thus making it safe to use.

MacBook Air (mid-2011) with its (old and new) chargers MacBook Air (mid-2011) with its (old and new) chargers

I was looking to get Typst working on this Mac recently. Instead of the conventional route (via either Rust toolchain or Homebrew — both large and unnecessary on old Macs facing dependency issues and obsolescence), I discovered a simpler way. That Typst can also be installed as a py package. With this, pdf documents can be produced like so:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import typst

# compile `document.typ` and save as `document.pdf`
typst.compile("document.typ", output="document.pdf")

Using docopt, this can be rewritten to accept a filename as an argument at command line:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Compile Typst document
ctd.py 2024 ckunte

Usage: ctd.py (-f <file>)
       ctd.py --help
       ctd.py --version

Options:
  -h, --help  Show this help
  -f, --file   Specify Typst input file to compile (required)

"""
import typst
from docopt import docopt

def main(typstfile):
    pdffile = f"{typstfile.rsplit('.', 1)[0]}.pdf"
    print(f"Compiling {typstfile} to {pdffile}...", end="")
    typst.compile(typstfile, output=pdffile)
    print("done.")

if __name__ == '__main__':
    args = docopt(__doc__, 
        version="Compile Typst document, 0.1")
    main(args["<file>"])

Pre-install dependencies like so first:

$ python3 -m pip install --user --upgrade docopt typst

and the above script (ctd.py) can now be run as:

$ python3 ctd.py -f document.typ
Compiling document.typ to document.pdf...done.

Custom Typst-py.sublime-build with typst-py and ctd.py:

{   
  "shell_cmd" : "python3 \\${HOME}/ctd.py -f \"$file_name\"",
  "selector" : "source.typ",
  "working_dir" : "$file_path"
}

This above allows one to type-set with Cmd+B.

Using Sublime Text on Windows (Cygwin)

Download and place typst.exe under a path accessible from Cygwin first. To be able to build via Sublime Text, see below.

Custom Typst-Cyg.sublime-build via Cygwin:

{   
  "shell_cmd" : "typst compile \"$file_name\"",
  "selector" : "source.typ",
  "path" : "C:\\Cygwin\\bin;$path",
  "working_dir" : "$file_path"
}

The approach should be similar for WSL, i.e., park typst for linux in an accessible path, e.g. /usr/local/bin, then write a build file with appropriate path.

Using GitHub Actions

A workflow is a set of instructions to be carried out as actions in a container provided by GitHub. These instructions are to be based inside a subfolder of the repository like so .github/workflows/release.yml, whose contents are as below:

name: Build Typst document
on: 
  push:
    tags:
      - "v*.*.*"

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: prep typst
        uses: typst-community/setup-typst@v3
      - run: typst compile m1.typ

      - name: release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            m1.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment