Should I `include` or `import` a function?

I am having issues with the #include function.

Even a minimal example doesn’t work for me.
File a: main.typ

#include "style.typ"
anything

File b: style.typ

#set page(fill: red)

having any other content in the main file overwrites all set and show rules included from style.typ.

how can i use include in order to have a default set of stylings and functions to use in all my documents

Are you trying to build a template? The problem here is that whatever you #include is only scoped to their own container, not the whole document (in your MWE, inserting a #pagebreak() before “anything” would include a red page, as that page is scoped within the included style file) .

See here for a more in-depth explanation: How can I create a set of shared `set` and `show` rules which can be imported into a theme? - #3 by ParaN3xus

3 Likes

Hi, welcome to the Typst forum! When you do #set page(fill: red) in a file, it affects only the document content that is produced further down in that file. Your example is a bit of an edge case: when you compile style.typ there is no content so Typst produces a blank page with the chosen fill color.

When you include the file in another one, where you add some real content, the blank page is produced in the context of the file with the real content, where the #set page(fill: red) is no longer active.

To solve your problem you should define a template function in style.typ, then import (not include) style.typ in your main file and use the template function with something like show: template.

Here are some other posts that might help you understand how templates work in Typst:

(Too slow :-) but I will leave it here in case it helps…)

2 Likes
  • no effect? Typst Documentation is the MUST read series during composing with typstlang
  • overlap? multiple set/show rules would compose into one, and new cover old

Including: include "bar.typ"
Evaluates the file at the path bar.typ and returns the resulting content.

  • include “xxx.file” // in yyy.file
    • many other langs, e.g. python: copy and paste xxx to yyy
    • typstlang: run xxx and return the result PDF piece to yyy

Welcome @TWG

In short: you generally use #import when you want to use a function, a module and/or a template in a separate document. Remember what keyword you use when loading packages from Typst Universe ;)

Long Version

#import inserts modules, functions, variables, everything that is non-content into your document. Content are things such as equations, lists, paragraphs, etc.

#include inserts the contents, but leaves out any non-content. So you get the paragraphs, equations, images, tables, etc.

Here’s an example on how you can see that effect (and how to fill the page red). You have two files main.typ and style.typ

Contents of style.typ

#let make-it-red(body) = {
  set page(fill: red)

  body
}

Hello, it's me `style.typ`!

Now let’s see how the end results differ when using either #import or #include

Importing style and then applying everything after the #show statement with the function make-it-red

#import "style.typ": *

#set page(width: 5cm, height: 4cm, margin: 5mm)

#show: make-it-red

Hello World

yields

This is essentially what creating a document template is. When you state #show: function, it means take everything below this statement and apply function to it. function would have at least one parameter which represents the taken content. If you want to have a parameter for filling the page with a different color, you have function(content, fill: red). Since fill has a default value, the above #show statement sill works. If you want to change the background to blue, you then have to use function.with(fill: blue), because #show: always wants a function. Read more on Making a Template – Typst Documentation.

When we include style.typ like following

#set page(width: 5cm, height: 4cm, margin: 5mm)

#include "style.typ"

Hello World

we get

You can imagine the rendering process as compiling everything inside the document style.typ and then inserting it into main.typ. (I think) This unfortunately makes it impossible to have something like subfiles from LaTeX.

4 Likes