This talk contains a really good explanation of this problem (and also discussion around how it's solved in Typst) https://www.youtube.com/watch?v=yWWVhbyOWWE