How can I load configuration and defined in another .typ file?

So far I have only used one .typ file per project. Now I would like to save repetitive configurations and load them with one # so that I don’t have to type the same thing multiple times when I have more than one .typ file in a project.

All of my .typ files start with the following:

#set page(
  paper: "us-letter",
  margin: 0.5in,
)
#set text(
  font: "New Computer Modern",
  size: 12pt
)

#let title = [
  (Insert Title Here)
]

#let print_title = {
  align(center, text(12pt)[
  *#title*
])
}

Is it possible to have a config.typ file that contains the above, which are all loaded with a single # at the start of another .typ file in the same project? Also, can title be manually overridden in the new .typ file as needed?

What you’re looking for is called a template. Here is the official Typst guide on templates:

Your example would become:
Main Typst file:

#import "template.typ": *
#show: template

#print_title([Template Test])

= Foo

#lorem(10)

template.typ:

#let print_title(title) = {
  align(center, text(12pt)[
  *#title*
  ])
}

#let template(doc) = {
  set page(
    paper: "us-letter",
    margin: 0.5in,
  )
  set text(
    font: "New Computer Modern",
    size: 12pt
  )
  
  doc
}
2 Likes

Thank you so much! Can I import a template from another project? That is, can I create a “global” template that can be imported by any project?

I believe the way to do this is make a local package. Forum user Eric posted an answer about this here:

1 Like

Understood! So if I wanted to do this in the web app, would the only option be to publicly publish my template as part of a package?

That’s a good question that I don’t know the answer to.
Publishing a package would certainly work, but it seems overkill to me. Plus it could be an information security problem, depending on what info is included in your template.