Skip to content

Keep with the next paragraph #993

Closed

Description

@G1gg1L3s

Description

Many word processors have options for paragraphs like "keep with the next" or "keep lines together". The first, for example, ensures that no page break happens between the two paragraphs. Most often it's used with headings to prevent the pagebreak between a header and a text. Compare these two examples of google docs with the option disabled and enabled:

It would be cool to have these options as a part of par element, or maybe find a way to make them implementable in a general way.

Use Case

The "keep-with-the-next" options is often used with headings if tldr. But Matthew Butterick described the benefits of these options better than me:

Activity

  1. added
    feature request
    on Apr 26, 2023
  2. SUPERCILEX commented on Apr 26, 2023

    @SUPERCILEX
    Contributor

    I've been thinking about this too. I would love to see some sort of "same paragraph" detection to glue things like equations to text. For example of I don't have a blank like between my text and the $, I would like them to be rendered together.

  3. LaurenzV commented on Apr 26, 2023

    @LaurenzV
    Collaborator

    You might already be aware of this, but as a workaround, you can do it manually by writing something like this:

    #let a = block(breakable: false)[
      = This is the heading
    
      #lorem(200)
    ]
    

    But obviously it's not ideal if you have to do this manually for every heading.

  4. laurmaedje commented on May 8, 2023

    @laurmaedje
    Member

    This specific case with the heading is a bug and should work (#1044), but a general option might be nice.

  5. added
    layout
    on May 8, 2023
  6. Beiri22 commented on Jul 13, 2023

    @Beiri22
    Contributor

    When you put content into a block after some heading, it also breaks directly after the heading. I would really like to have some option to prevent breaking at specific locations. I mean, it can break inside the box, if needed.

    Example:

    #lorem(630) // Change value to break after header
    
    = Header
    #block()[
      Some content inside block. #lorem(100)
    ]
    
  7. dhouck commented on Nov 23, 2023

    @dhouck

    I was shocked when I was looking for this and found an open issue instead of documentation. Typst bills itself as a powerful page-oriented markup language, with comparison to Microsoft Word and Latex on the front page; controlling page breaks is essential for such a language.

  8. Enivex commented on Nov 23, 2023

    @Enivex
    Collaborator

    I was shocked when I was looking for this and found an open issue instead of documentation. Typst bills itself as a powerful page-oriented markup language, with comparison to Microsoft Word and Latex on the front page; controlling page breaks is essential for such a language.

    Typst is still in beta, and revamp of layout is on the roadmap. I'm not sure why you're surprised that there are still certain aspects that aren't as mature as programs that have been around for 40 years.

  9. Beiri22 commented on Nov 23, 2023

    @Beiri22
    Contributor

    Just in case, but maybe typst gives a very mature impression with its online appearance?

  10. dhouck commented on Nov 23, 2023

    @dhouck

    Yeah, that’s fair. I consider this, along with widow/orphan control, a more basic feature than some stuff that already exists, but that doesn’t mean it should have been implemented first.

  11. jtchen2k commented on Dec 1, 2023

    @jtchen2k

    +1 for this. I think this is one of the most fundamental features for a typesetting system.

  12. betoma commented on Dec 7, 2023

    @betoma

    Absolutely 100% behind having this as a feature, it would solve so many problems. However, I will note that I had the exact same issue when working in LaTeX pretty often, and the solution was either really arcane or non-existent in some circumstances, so I don't think this is an area where Typst particularly falls behind in that respect.

  13. PgBiel commented on Feb 12, 2024

    @PgBiel
    Contributor

    For people looking for a workaround, I've cooked something which might be helpful.
    Basically consists of keeping the first element - and the spacing between both elements - unbreakable across pages, but not the second. Thus, it should work for the use case of (unbreakable) heading + (breakable) paragraph.

    https://gist.github.com/PgBiel/2976c9d0ed5638ef57633ce7233928ea

  14. johannesrave commented on May 7, 2024

    @johannesrave

    I'm also using blocks after headings and don't want them to break apart. This appears to be intended behaviour, but would be nice if it was configurable.

  15. Enivex commented on May 7, 2024

    @Enivex
    Collaborator

    I'm also using blocks after headings and don't want them to break apart. This appears to be intended behaviour, but would be nice if it was configurable.

    If you're referring to having an isolated heading at the bottom of a page, that's a bug, as mentioned above.

    #1044

  16. Shrinks99 commented on Jun 27, 2024

    @Shrinks99

    Ran across this issue today. Wanted to highlight my use case of Markdown → Typst conversion & styling.

    Also probably worth pointing out that this is fairly easy to solve on an individual document basis with manual page breaks, but would definitely be a welcome addition for improving automated workflows! :)

  17. dhouck commented on Jul 2, 2024

    @dhouck

    Manual page breaks work great, until you edit the text or styling or paper options in any way.

  18. oliverpool commented on Jul 8, 2024

    @oliverpool
    Contributor

    I am creating a package for a songbook (similar to patacrep, which is based on LaTeX + Songs) and needed something similar.

    Here is my current stance: https://codeberg.org/pfad.fr/typst-songbook/src/branch/main/autobreak.typ

    When using #autobreak[some content], it checks the length of the given content:

    • if it fits on the rest of the current page, put it there
    • if it fits on one page, but not this one, add a break and put the content
    • if it needs more than one page, and we are on an odd page: add a break (i.e. turn the page) and put the content
    • otherwise put the content

    It works best for short content (i.e. less than 1-2 pages), but it may help for something like:

    #autobreak[
    = Heading
    
    First paragraph bla bla
    ]
    
    Second paragraph
  19. oliverpool commented on Jul 10, 2024

    @oliverpool
    Contributor

    My autobreak function fails when there are too many occurrences.. I would be grateful for ideas or suggestions in #4530 (adjusting #993 (comment) takes me further, but the underlying issue is still here)

  20. kokkonisd commented on Dec 6, 2024

    @kokkonisd
    Contributor

    Hi!

    It seems that this is still broken inside blocks:

    // Add enough padding to force the next heading & paragraph to start at the
    // bottom of the page.
    #lorem(650)
    
    // No issue here: the heading & paragraph stay together and move to the next
    // page, since there is not enough space on this page for both of them.
    = Test
    This is the second paragraph.
    
    #pagebreak()
    
    // Add enough padding to force the next heading and paragraph to start at the
    // bottom of the page.
    #lorem(650)
    
    // Problem: even though there is not enough space for both the heading and the
    // paragraph, instead of being moved to the next page, they get split and the
    // header stays on this page.
    #block[
        = Test
        This is the second paragraph.
    ]

    Compiled with:

    $ typst --version
    typst 0.12.0 (7add9b45)
    $ typst c main.typ

    (See attached PDF: main.pdf)

  21. Andrew15-5 commented on Dec 6, 2024

    @Andrew15-5
    Contributor

    Can confirm. My initial thought (which can be true) was that this is because by default block(breakable: true), but the heading should stick to the text below regardless. Maybe the breakable overrides the heading stickiness. Or maybe it's simply a new/untested edge case.

  22. laurmaedje commented on Dec 6, 2024

    @laurmaedje
    Member

    Tracked by #5296

  23. Timmmm commented on Sep 18, 2025

    @Timmmm

    It would be cool if when closing issues you could explain what the fix is. For anyone wondering, you can do this:

    #block(sticky: true)[We have:]
    
    Another paragraph on the same page.
    

    Maybe not the nicest syntax given how common this pattern is. It's regular at least.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestlayout

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions