How to set header with a function

i have a long setup for header similar to

#set page(
  numbering: "1",

  header: context [
    HEADER
    #h(1fr)
    #counter(page).display()
  ],
)

= First Chapter

#lorem(200)

which i would move to a function (to better structure the code). I assume i would have to write something like:

#let set-header: it => context [
    HEADER
    #h(1fr)
    #counter(page).display()
  ],
#set page(
  numbering: "1",

  header: set-header()
)

= First Chapter

#lorem(200)

What is the correct approach?

1 Like

Hi, welcome to the forum!

You could define the function with the => syntax (anonymous function) but this is a function with zero argument so you would write: #let set-header = () => context [...]. But it’s a bit weird to define an anonymous function just to store it in a variable set-header. You would rather define a set-header function directly with

#let set-header() = context [ ... ]

But the body of the function is constant so you don’t need a function at all! So the right way to do this here is to define the header content simply as a variable:

#let header = context [ ... ]

and use it as #set page(header: header, ...).

1 Like

@sijo is 100% right that a function is unnecessary here.
Creating a function can have benefits though, such as the ability to easily change the header text when you want.

#let set-header(header-text) = context [
  #header-text
  #h(1fr)
  #counter(page).display()
]

#set page(numbering: "1")

#set page(header: set-header[Header Text 1])
#set page(header: set-header[Header Text 2])

Note, however, that a new page is created every time set page() is called. So you would probably only use this at the start of a new chapter/section.


Something that is not obvious but can have surprising effects on layout is the use of [] here. This even has its own GitHub issue:

A simple change is to use {} instead:

#let set-header(header-text) = context {
  header-text
  h(1fr)
  counter(page).display()
}
3 Likes

I think u want to get rid of the non-content stuff like h(1fr), counter(), SURE u can do it this way

#let Page = (
  num: "1",
  hdr: [HEADER],)

#set page(
  numbering: Page.num,
  header: context {
    Page.hdr
    h(1fr)
    counter(page).display()},)

= First Chapter

#lorem(200)