You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Block sticky parameter doesn't work at the start of block #5296
bugSomething isn't workinglayoutRelated to the layout category, which is about composing, positioning, etc.urgentShould be fixed before the next release.
Maybe I don't understand how the block.sticky parameter should work but it doesn't work as I expected inside a block. This sample correctly moves to the next page:
This is annoying, for example, with the gentle-clues package. I was trying to prevent the header from splitting from the content but the sticky parameter is ignored inside the wrapper block.
Note that the obvious solution, setting breakable to false, isn't sufficient as I do want the block to break, I just don't want it to break that early (similar to how a paragraph shouldn't break after a single line).
Upon further investigation, sticky does work inside blocks, as seen here:
#setpage(height: 3cm)
#block[
abc
#v(0.8cm)
== H
b
c
]
The problem arises when the sticky block (in this case, the heading) is the first thing (more specifically, frame - ignoring spacing) in the block:
#setpage(height: 3cm)
abc
#block[
#v(0.8cm)
== H
b
c
]
As it turns out, the fact that this happens within a block is a red herring. The same happens outside of blocks:
#setpage(height: 3cm)
#v(1.5cm)
== A
b
This is specifically due to the stickable check in the code. By default, the distributor used to layout a flow (like the root flow of the document, or the block's flow) starts with stickable: false:
And this matters because, when distributing a frame, we only "attach" the sticky block to the next frame in the flow if stickable is set to true, as seen in the snippet below. In the same snippet, we also see the only way to change stickable to true: to place a non-sticky frame. Therefore, any sticky blocks at the beginning of flow distribution are stripped of their sticky effect, since there are no non-sticky frames before them (because there are no frames before them).
// If the frame is sticky and we haven't remembered a preceding
// sticky element, make a checkpoint which we can restore should we
// end on this sticky element.
ifself.stickable && self.sticky.is_none(){
self.sticky = Some(self.snapshot());
}
}elseif !frame.is_empty(){
// If the frame isn't sticky, we can forget a previous snapshot.
self.stickable = true;
self.sticky = None;
}
Given that the docs for stickable describe the parameter with "Whether there was at least one proper block. Otherwise, sticky blocks are disabled (or else they'd keep being migrated).", this suggests that stickable might be too conservative as is. I believe regions.may_progress() should also be considered in the decision of whether to keep the sticky behavior for a block: in the cases shown above, moving to the next region would indeed have improved the lack of space without causing an infinite migration loop.
changed the title [-]Block sticky parameter doesn't work inside block[/-][+]Block sticky parameter doesn't work at the start of block[/+]on Dec 12, 2024
Alright. It's not so simple: while checking for (self.stickable || self.regions.may_progress()) for stickiness does solve the problem in this issue (with a single sticky block before a frame), it affects multiple consecutive sticky blocks differently, since a single sticky block already turns self.regions.may_progress() true for the next blocks, so all sticky blocks but the first in the sequence become sticky, leading to multiple pages where the first sticky block is alone until either we reach a page where all of the remaining sticky blocks fit, or only one sticky block remains. This is shown by how the block-sticky-many test's output changed (left = before (current), right = after the proposed change):
Indeed, self.stickable would become largely redundant with this change, since self.stickable already implies self.regions.may_progress() (assuming a frame with non-zero height was placed to enable stickiness).
So, we have to make a decision on what is the expected behavior here. At first, the behavior to the right doesn't seem that unreasonable from a stricter perspective where Typst should try its hardest to keep as many sticky blocks as possible in the same page as the first non-sticky block after them. However, it is not ideal because, as shown by the pre-change test result, it would already be impossible to keep all of them in the same page as the attached frame anyway, so the pagebreaks between sticky blocks are totally excessive. (Note, however, that infinite loops are still prevented, regardless of this weirdness.)
On the other hand, the current (pre-change) behavior always causes all sticky blocks before the first non-sticky frame to have stickiness disabled, so, by removing sticky block D, we end up seeing zero sticky blocks in the same page as the non-sticky frame, which is also not the best since "C" would clearly fit there, maybe even "B" as well.
Now, with the current snapshot system - where we revert to the first sticky block in a sequence of them if the region ended in the middle of or after the sequence -, it sounds like a bit of a larger challenge to determine at which sticky block we would "make the cut" and try again in the next region in the situation above, or, in other words, to determine the ideal amount of sticky blocks to migrate. We'd like to maximize the amount of sticky blocks staying with the frame, but the non-sticky frame might also be placed several pages ahead with too many sticky blocks, so that could require some annoying amount of lookahead.
With that in mind, if we agree that the new test output is not ideal, I see two possible improvements for now:
Apply the regions.may_progress() fix, but for the whole sequence of sticky blocks at once. That is, if the first sticky block is disabled, then all consecutive ones are as well. However, this still makes it possible to unnecessarily orphan the next non-sticky frame (as shown above, where you can remove D and E is orphaned). That is, it preserves the current behavior for that particular case.
Don't apply it for the whole sequence at once (so, only the first sticky block in a sequence is disabled), but apply the following heuristic: in the event of a region break after a sticky block (that is, we have a sticky snapshot), we use some form of lookahead to determine whether another sticky block is about to come (that is, if the next frame is sticky). If so, don't restore the snapshot. That way, we don't unnecessarily create pagebreaks between sticky blocks, but also allow further sticky blocks to stay with the frame. (However, it won't maximize the amount of sticky blocks together with the frame, but that's not so bad.)
The main problem with this approach is that this could potentially require an unbounded amount of lookahead on each region break, since flow children that aren't frames are irrelevant for this check, which could degrade performance particularly if, for some reason, someone places sticky blocks with a bunch of irrelevant children afterwards, such as #place or #v. Although this could potentially be mitigated with some smart caching at collection time.
In addition, it won't fully prevent orphaning if sticky + non-sticky > page height, but that's impossible to prevent anyway. (There was a recent discussion on Discord about a related situation1.)
I think 1 is the simplest approach for now, so, since it wouldn't regress anything, we could start with it and later experiment with more sophisticated approaches. I'll begin preparing my PR with that in mind, but I'm open to switching approaches at any time. In particular, we can also just accept the new test output if that's better.
bugSomething isn't workinglayoutRelated to the layout category, which is about composing, positioning, etc.urgentShould be fixed before the next release.
Activity
davystrong commentedon Oct 25, 2024
Note that the obvious solution, setting breakable to false, isn't sufficient as I do want the block to break, I just don't want it to break that early (similar to how a paragraph shouldn't break after a single line).
PgBiel commentedon Dec 12, 2024
Upon further investigation, sticky does work inside blocks, as seen here:
The problem arises when the sticky block (in this case, the heading) is the first thing (more specifically, frame - ignoring spacing) in the block:
As it turns out, the fact that this happens within a block is a red herring. The same happens outside of blocks:
This is specifically due to the
stickablecheck in the code. By default, the distributor used to layout a flow (like the root flow of the document, or the block's flow) starts withstickable: false:typst/crates/typst-layout/src/flow/distribute.rs
Lines 12 to 21 in 521ceae
And this matters because, when distributing a frame, we only "attach" the sticky block to the next frame in the flow if
stickableis set totrue, as seen in the snippet below. In the same snippet, we also see the only way to changestickabletotrue: to place a non-sticky frame. Therefore, any sticky blocks at the beginning of flow distribution are stripped of their sticky effect, since there are no non-sticky frames before them (because there are no frames before them).typst/crates/typst-layout/src/flow/distribute.rs
Lines 306 to 324 in 521ceae
Given that the docs for
stickabledescribe the parameter with "Whether there was at least one proper block. Otherwise, sticky blocks are disabled (or else they'd keep being migrated).", this suggests thatstickablemight be too conservative as is. I believeregions.may_progress()should also be considered in the decision of whether to keep the sticky behavior for a block: in the cases shown above, moving to the next region would indeed have improved the lack of space without causing an infinite migration loop.[-]Block sticky parameter doesn't work inside block[/-][+]Block sticky parameter doesn't work at the start of block[/+]PgBiel commentedon Dec 13, 2024
Alright. It's not so simple: while checking for
(self.stickable || self.regions.may_progress())for stickiness does solve the problem in this issue (with a single sticky block before a frame), it affects multiple consecutive sticky blocks differently, since a single sticky block already turnsself.regions.may_progress()true for the next blocks, so all sticky blocks but the first in the sequence become sticky, leading to multiple pages where the first sticky block is alone until either we reach a page where all of the remaining sticky blocks fit, or only one sticky block remains. This is shown by how theblock-sticky-manytest's output changed (left = before (current), right = after the proposed change):Indeed,
self.stickablewould become largely redundant with this change, sinceself.stickablealready impliesself.regions.may_progress()(assuming a frame with non-zero height was placed to enable stickiness).So, we have to make a decision on what is the expected behavior here. At first, the behavior to the right doesn't seem that unreasonable from a stricter perspective where Typst should try its hardest to keep as many sticky blocks as possible in the same page as the first non-sticky block after them. However, it is not ideal because, as shown by the pre-change test result, it would already be impossible to keep all of them in the same page as the attached frame anyway, so the pagebreaks between sticky blocks are totally excessive. (Note, however, that infinite loops are still prevented, regardless of this weirdness.)
On the other hand, the current (pre-change) behavior always causes all sticky blocks before the first non-sticky frame to have stickiness disabled, so, by removing sticky block
D, we end up seeing zero sticky blocks in the same page as the non-sticky frame, which is also not the best since "C" would clearly fit there, maybe even "B" as well.Now, with the current snapshot system - where we revert to the first sticky block in a sequence of them if the region ended in the middle of or after the sequence -, it sounds like a bit of a larger challenge to determine at which sticky block we would "make the cut" and try again in the next region in the situation above, or, in other words, to determine the ideal amount of sticky blocks to migrate. We'd like to maximize the amount of sticky blocks staying with the frame, but the non-sticky frame might also be placed several pages ahead with too many sticky blocks, so that could require some annoying amount of lookahead.
With that in mind, if we agree that the new test output is not ideal, I see two possible improvements for now:
Apply the
regions.may_progress()fix, but for the whole sequence of sticky blocks at once. That is, if the first sticky block is disabled, then all consecutive ones are as well. However, this still makes it possible to unnecessarily orphan the next non-sticky frame (as shown above, where you can removeDandEis orphaned). That is, it preserves the current behavior for that particular case.Don't apply it for the whole sequence at once (so, only the first sticky block in a sequence is disabled), but apply the following heuristic: in the event of a region break after a sticky block (that is, we have a sticky snapshot), we use some form of lookahead to determine whether another sticky block is about to come (that is, if the next frame is sticky). If so, don't restore the snapshot. That way, we don't unnecessarily create pagebreaks between sticky blocks, but also allow further sticky blocks to stay with the frame. (However, it won't maximize the amount of sticky blocks together with the frame, but that's not so bad.)
#placeor#v. Although this could potentially be mitigated with some smart caching at collection time.I think 1 is the simplest approach for now, so, since it wouldn't regress anything, we could start with it and later experiment with more sophisticated approaches. I'll begin preparing my PR with that in mind, but I'm open to switching approaches at any time. In particular, we can also just accept the new test output if that's better.
Footnotes
Discussion about sticky + non-sticky height being larger than the page: https://discord.com/channels/1054443721975922748/1310848449671069737 ↩