How to avoid headings too far down the page?

Hi there! I’m currently using the following:

#show heading: it => [
  ~ 
  #if it.location().position().y > 650pt [
    #pagebreak()
  ]
  #if it.level == 1 [
    Partie 
  ]
  #counter(heading).display(it.numbering)
  --- #it.body
]

And I’m hitting the 5 iteration convergence threshold. In my understanding, layout is computed from top to bottom, which should make this converge in 1 iteration (the ~ at the beginning is here to make sure that the y-coordinate doesn’t actually change). How can I fix this ? The behavior I want is that if a heading ends up too far down a page, I want to move it to the next page.

Hi @T.C, welcome to the forum! I have edited the title of your question to follow our guidelines.

My guess for the convergence issue is that after one pass, some of the tests for > 650pt return true, which causes the insertion of a few pagebreaks. The first of these pagebreaks causes a relayout for everything after it. Then in this relayout, some previously inserted pagebreaks might be removed, and some others might be added. The first of these changes will cause a relayout of everything below, etc. But it’s just an idea, I don’t know if that’s really what happens.

In any case, it sounds like you can get the desired behavior using the same trick as was used previously to prevent orphan headings (heading at the bottom of a page and next content starting on the next page). See discussions in How to pagebreak before an heading, only if a certain condition is achieved? and How to make sure heading and the first following content stay on the same page? (this particular problem with orphan headings is now solved with the block(sticky: true, ...) property).

You could use this trick like this:

#show heading: it => [
  #block(height: 80pt, breakable: false)
  #v(-80pt)
  #if it.level == 1 [
    Partie 
  ]
  #counter(heading).display(it.numbering)
  --- #it.body
]

(replace 80pt with the minimum space you want from the top of the heading to the bottom of the text area.)