How to get first heading in each column?

I have set the columns for my page to 2. I’m wanting to set up the page header so that it shows the first headings of each column to the left and the right in the header but I cannot figure out how to query/select the first heading in each column.

Anyone have any ideas/solutions ?

Thanks

Hello. If there is no heading in a column, what should be displayed? What about numbering? There is a very similar question in How to get the number of the first and last paragraph on a page and show the result in the header?.

#set page(header: [Heading 1#h(1fr)Heading 2], columns: 2)
#set par(justify: true)

= Heading 1
#lorem(50)

#lorem(60)

#lorem(70)

#lorem(80)

#lorem(50)

= Heading 2
#lorem(60)

#lorem(70)

#lorem(80)

#lorem(70)

Is this how it should look like?

I’ll take a look at that link, but yes that is what it should look like (but obviously the header values need to be dynamic :smile: ).

Good question re what to do if no heading in the column… I think blank will do in my case.

Wow, this was actually very easy after thinking for a little bit:

#let header = context {
  let headings = query(heading).filter(x => x.location().page() == here().page())
  let leftm = if page.margin == auto { 2.5cm } else { page.margin.left }
  let rightm = if page.margin == auto { 2.5cm } else { page.margin.right }
  let middle = (page.width - leftm - rightm) / 2
  let left = headings.filter(x => x.location().position().x < middle)
  let right = headings.filter(x => x not in left)
  let left = if left.len() != 0 { left.first().body }
  let right = if right.len() != 0 { right.first().body }
  [#left#h(1fr)#right]
}
#set page(header: header, columns: 2)
#set heading(numbering: "1.")
#set par(justify: true)

= Heading 1
#lorem(50)

#lorem(60)

#lorem(70)

#lorem(80)

#lorem(50)

= Heading 2
#lorem(60)

#lorem(70)

#lorem(80)

#lorem(70)

= Heading 3
#lorem(80)

image

Ah nice. Very cunning!

1 Like