Skip to content

Block sticky parameter doesn't work at the start of block #5296

Closed

Description

@davystrong

Description

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:

#lorem(650)

#{
  block(fill: red, sticky: true, [block1])
  block(fill: blue, [block1])
}

While this sample splits between two pages:

#lorem(650)

#block(fill: green,
  {
    block(fill: red, sticky: true, [block1])
    block(fill: blue, [block1])
  }
)

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.

Reproduction URL

No response

Operating system

MacOS

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Oct 25, 2024
  2. davystrong commented on Oct 25, 2024

    @davystrong
    Author

    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).

  3. added
    layout
    on Oct 26, 2024
  4. added
    urgent
    on Nov 7, 2024
  5. added
    needs-investigation
    on Dec 6, 2024
  6. self-assigned this
    on Dec 7, 2024
  7. PgBiel commented on Dec 12, 2024

    @PgBiel
    Contributor

    Upon further investigation, sticky does work inside blocks, as seen here:

    #set page(height: 3cm)
    
    #block[
      abc
      #v(0.8cm)
      == H
    
      b
    
      c
    ]

    H sent to the next page

    The problem arises when the sticky block (in this case, the heading) is the first thing (more specifically, frame - ignoring spacing) in the block:

    #set page(height: 3cm)
    
    abc
    
    #block[
      #v(0.8cm)
      == H
    
      b
    
      c
    ]

    H on a separate page from b

    As it turns out, the fact that this happens within a block is a red herring. The same happens outside of blocks:

    #set page(height: 3cm)
    #v(1.5cm)
    
    == A
    
    b

    A and b are in separate pages

    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:

    /// Distributes as many children as fit from `composer.work` into the first
    /// region and returns the resulting frame.
    pub fn distribute(composer: &mut Composer, regions: Regions) -> FlowResult<Frame> {
    let mut distributor = Distributor {
    composer,
    regions,
    items: vec![],
    sticky: None,
    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).

    fn frame(
    &mut self,
    frame: Frame,
    align: Axes<FixedAlignment>,
    sticky: bool,
    breakable: bool,
    ) -> FlowResult<()> {
    if sticky {
    // 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.
    if self.stickable && self.sticky.is_none() {
    self.sticky = Some(self.snapshot());
    }
    } else if !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.

  8. 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
  9. removed
    needs-investigation
    on Dec 12, 2024
  10. PgBiel commented on Dec 13, 2024

    @PgBiel
    Contributor

    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):

    #set page(height: 80pt)
    #set block(sticky: true)
    #block[A]
    #block[B]
    #block[C]
    #block[D]
    E
    #block[F]
    #block[G]

    block-sticky-many test: page with A B C, page with D E, page with F G (before), page with A, page with B, page with C D E, page with F G (after)

    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.

    page 1 = A B C, page 2 = E F G

    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:

    1. 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.

    2. 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.

    Footnotes

    1. Discussion about sticky + non-sticky height being larger than the page: https://discord.com/channels/1054443721975922748/1310848449671069737 ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buglayouturgent

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions