How to access or set PWD / path / project root?

I run an archive (like UN Secretariat) with hundreds-to-thousands of Typst documents. I use a build system to functionally generate PDF artifacts.

These Typst documents use a shared library to style stuff, like adding headers and footers, but are not guaranteed to stay in the same depth level. For example, “filing/hq/sig123/iod.123.456.typ” (depth 4) and “orders/2025/OD-20250101-000001.typ” (depth 3) may share the same header file (corporate letterhead). When I use LaTeX, I use \include{latexlib/styles.H.tex} to load the customizations I need. But Typst makes this inconvenient by disallowing PWD-based relative paths, i.e. requiring all relative paths to be target-based.

  • I could soft-code a relative path, but I cannot guarantee depth uniformity, so the import clauses are fragmented and path-sensitive.
  • I could hard-code an absolute path, but this couples too much on USER and HOME, making the archive unportable, and sticks to a certain OS.
  • I could use my build script to rsync my archive-specific typstlib to a fixed, promised-to-be-writable-on-the-fly, target path /tmp/6c991fad92fb43688d5e9ecf83e2540f to ensure import clause uniformity, but this makes the archive unbuildable on Windows (unless using WSL), making it unfriendly for non-UNIX, non-tech collaborators.

Before I convince maintainers that PWD-based relative path is a good feature to have, is there any quick remedy ready to work?

Hi, welcome! There are two methods.

Use absolute paths

Paths start with a leading / will be relative to the project root.

#import "/path/to/style.typ": …
typst compile --root . orders/2025/OD-20250101-000001.typ

Create a local package

As Scripting – Typst Documentation says:

If you are using Typst locally, you can also create your own system-local packages. For more details on this, see the package repository.

In short, you can create a local package, and replace #import "../../style.typ": … with #import "@local/archive-style:1.0.0": ….

8 Likes

Thanks. The --root param works for me.

1 Like