Templates

Templates

If you want to reuse styling in other files, you can use the template idiom. Because set and show rules are only active in their current scope, they will not affect content in a file you imported your file into. But functions can circumvent this in a predictable way:

// define a function that:
// - takes content
// - applies styling to it
// - returns the styled content
#let apply-template(body) = [
  #show heading.where(level: 1): emph
  #set heading(numbering: "1.1")
  // ...
  #body
]

This is equivalent to:

// we can reduce the number of hashes needed here by using scripting mode
// same as above but we exchanged `[...]` for `{...}` to switch from markup
// into scripting mode
#let apply-template(body) = {
  show heading.where(level: 1): emph
  set heading(numbering: "1.1")
  // ...
  body
}

Then in your main file:

#import "template.typ": apply-template
#show: apply-template

This will apply a "template" function to the rest of your document!

Passing arguments

// add optional named arguments
#let apply-template(body, name: "My document") = {
  show heading.where(level: 1): emph
  set heading(numbering: "1.1")

  align(center, text(name, size: 2em))

  body
}

Then, in template file:

#import "template.typ": apply-template

// `func.with(..)` applies the arguments to the function and returns the new
// function with those defaults applied
#show: apply-template.with(name: "Report")

// it is functionally the same as this
#let new-template(..args) = apply-template(name: "Report", ..args)
#show: new-template

Writing templates is fairly easy if you understand scripting.

See more information about writing templates in Official Tutorial.

There is no official repository for templates yet, but there are a plenty community ones in awesome-typst.