#how to make section header as page header?

26 messages · Page 1 of 1 (latest)

full token
#

Hello, I want to set the header of the odd-numbered page as the title of the section in the current page and the header of the even-numbered page as the title of the thesis in my thesis layout. Can this be achieved with typst?

vestal needle
#

Use this:

vestal needle
# full token Hello, I want to set the header of the odd-numbered page as the title of the sec...
#let document-title = "My Book"
#set page(header: locate(loc => {
  let this-page = loc.page()
  if this-page == 1 {
    []  // don't display anything at the top in the first page
  } else if calc.even(this-page) {
    h(1fr)  // maximizing horizontal space to push it to the right
    [*#document-title;*]
  } else {  // odd page
    let headings-after = query(heading, after: loc)
    let this-heading = if headings-after.len() > 0 {
      headings-after.first()
    } else {
      none
    }

    if this-heading != none and this-heading.location().page() != this-page {
      this-heading = none  // only accept headings from the current page
    }

    // if there are no headings in the current page, then get the latest one from before
    if this-heading == none {
      let headings-before = query(heading, before: loc)
      
      this-heading = if headings-before.len() > 0 {
        headings-before.last()
      } else {
        none
      }
    }

    // if there is a heading in this page or a heading before, display it
    if this-heading != none {
      let number = if this-heading.numbering != none {
        numbering(this-heading.numbering, ..counter(heading).at(this-heading.location()))
      } else {
        []
      }

      [*#number #this-heading.body*]
    }
  }
}))
#

note that you will have to manually replace "My Book" with your title up there

#

(it can't detect the title as there's no dedicated element for this, you need to set it through a variable)

dire rivet
#

shouldn't it be headings-after.first()?

vestal needle
#

hmm

#

well

#

I guess that's up to you

#

lol

#

but sure

#

I'll use that instead

#

actually

#

yeah I just thought about it

#

it'd get headings from other pages

#

it'd be weird

#

lol

dire rivet
#

i mean, headings-after.last() would always give you the last heading of the document, right?

vestal needle
#

yeah lol

#

I did the dummy dum there

#

neuron 1 said something to neuron 2 but neuron 2 was like "sorry i didnt hear you" and that was about it

graceful cypress
vestal needle
#

It’s now query(selector(heading).before(loc), loc)

#

It was done that way to be more consistent (you can use that same selector() syntax on #show rules)

graceful cypress
#

thank you!