How to use package functions in included Typst files

This question might be a repeat, sorry…

I am dividing a document into many subfiles and merging them using #include "subfile.typ" in a main file that imports several packages, for example:

#import "@preview/ctheorems:1.1.3": *

A description in a subfile using a function defined in the package:

#theorem("Euclid")[ 
There are infinitely many primes.
]

results in “error: unknown variable”, while the same description written in the main file compiles successfully.

How can I successfully use package functions in the subfiles?

Hi there,

I’m afraid you will need to add the import in every file where it is needed.

1 Like

Note that include returns the content of the included file, not any symbols, which is why you can’t use theorem.

As said above, you will have to import the symbols you need.

If you have a main file that includes many subfiles, you would probably be better off with a template-style document, e.g.,

// template.typ
#import "@preview/ctheorems:1.1.3": *
#let theorem = thmbox("theorem", "Theorem", fill: rgb("#eeffee"))
// main.typ
#import "template.typ": * // import symbols
#include "subfile.typ" //content
// subfile.typ
#import "template.typ": * // import symbols
#theorem[] ...
1 Like

Thank vmartel08 for immediate response,

This reaffirms that importing packages in each subfile is tedious, but necessary.

1 Like

Thank quachpas for the idea to reduce the import work.

I use a lot of packages, so it’s very helpful to only have to write one line in the subfiles.