How to troubleshoot header with locate from an imported file?

Since a Typst update (about a year ago, I’d say), I can no longer use my file to format my other files.
The issue seems to stem from the header: locate...

In cours.typ, I have:

#let Cours(
  titre: none,
  chapitre: none,
  classe: none,
  doc)={

  // parametre entre autre l'en-tete
  set page(
    paper: "a4",
    margin: (x: 0.5cm, y: 1.2cm),
    numbering: "1/1",
    header: locate(loc => {
              if counter(page).at(loc).first() < 2 [
                  #set text(12pt)
                  #(chapitre)
                  #h(1fr)
                  #(classe)
                ]})
  )
}

and at the beginning of each of my files:


#import "cours.typ": *
#show: doc => Cours(
  titre: [my_title],
  chapitre: [my_number],
  classe: [my_classe],
  doc)

I can’t seem to fix the problem, any ideas?

Thank’s

Hi! Using locate like this was indeed how it worked a few years ago: 0.11.0 - Typst Documentation

Nowadays we would write context { counter(page).get() /* do the rest of your work in here; just as with locate, you can no longer do logic on it or read properties outside of this bracket → */ } to retrieve the current page counter value, or here() within a context block to get the current location as a value like loc in your snippet: Context - Typst Documentation.

Thank you.
I understand the problem, but I’m struggling to format it properly.
For now, I’ve written this:

header: if context[#here().page()] < 2 [
             #set text(12pt)
             #(chapitre)
             #h(1fr)
             #(classe)]

Everything has to be worked out within the statement (here code block delimited by curly brackets) after context:

header: context {
          if counter(page).get().first() < 2 [
            #set text(12pt)
            #(chapitre)
            #h(1fr)
            #(classe)
          ]
        }

We could also write it a bit more compact; here the statement after context is the if expression itself not a code-block containing one. It is equivalent to the above syntactically:

header: context if counter(page).get().first() < 2 {
  set text(12pt)
  chapitre
  h(1fr)
  classe
}

Note that here().page() is slightly different to the page counter, but can also be used here just the same if its what you want :)

1 Like