#Unified imports/preamble?

1 messages · Page 1 of 1 (latest)

formal oak
#

I'm working on a template file, and am wondering whether it's possible to import all the packages I'll want to use while writing the actual document, directly from the template.
I've tried simply importing my packages in the template, e.g.

template.typ:
#let conf(doc) = {
 import "@preview/example:0.1.0": *
 text(doc)
}

main.typ:
#import "template.typ": conf
#show: doc => conf(doc: doc)

#add(1, 2)

But so far no luck. I've also attempted moving the imports to a seperate file and includeing it or wildcard importing everything, but this doesn't seem to have worked either.
Is there currently any supported way of doing this?

ivory sail
#

Template is just the same as any other import, and when you import something, you import all the things from the main scope the file you import from to file where you are importing.

So you can do this like this:

template.typ:

// All functions from example will be imported when you import that template
#import "@preview/example:0.1.0": *
#let conf(doc) = {
 text(doc)
}

main.typ:

#import "template.typ": *
#show: doc => conf(doc: doc)
#add(1, 2)

However, importing with * may be dangerous, as functions from your template and packages will be mixed. And importing each one by hand is tiresome, so I suggest that solution:

template.typ:

// Example module will be imported and available as a whole 
#import "@preview/example:0.1.0"
#let conf(doc) = {
 text(doc)
}

main.typ:

#import "template.typ": *
#show: doc => conf(doc: doc)
// specify which package to use
#example.add(1, 2)

It is quite a flexible system, so it is possible to get almost anything you want once you understand its principles.

formal oak
#

Hmm. That doesn't seem to be working for me, but I assume this just means I'm doing something wrong on my end. Thanks!

agile mist
#

What I have done is, I created a third file util.typ and imported everything I want to access in all other files there

#

So every typst file contains #import "util.typ": *

#

Could also be named prelude.typ but I also placed some little functions inside

formal oak