typst

Typst reference elements from another file


I am writing a document that have each chapter as a separate file. The structure is like

main.typ  
ch_1.typ  
|-- fig_1  
ch_2.typ

Within ch_1.typ I have a figure with a label, say <fig_1>. In ch_2.typ I would like to reference it using @fig_1. However, when compiling ch_2.typ, the compiler raises the error label does not exists in the document.

The reference works when I compile main.typ, even though there are some warnings. However, I still cannot compile ch_2.typ on its own, which defeats the meaning of a separate file structure.

Any help is appreciated.


Solution

  • Typst supports cross-referencing another file, as long as they are both included by a common ancestor. This project demonstrates this with the setup from your question.

    The problem could be in your local setup. For example, the Visual Studio Code extension "Typst LSP" is known to treat every file independently, producing these sorts of errors.

    You can try to compile the project by running typst compile main.typ in your terminal instead, which should work. You will need to obtain a Typst compiler from a package manager or GitHub if you do not yet have a standalone installation for your shell. If you want Typst to keep running and recompile when files change, use typst watch main.typ instead.