Skip to content

Headings do not always respect widow and orphan prevention #1044

Closed

Description

@matthewelmer

Description

I see in the February 12, 2023 changelog that "Support for widow and orphan prevention within containers" was added. I would like this to also be added for entire pages.

Use Case

This feature would allow users to focus on writing their papers instead of worrying about orphans/widows.

Activity

  1. added
    feature request
    on Apr 30, 2023
  2. laurmaedje commented on Apr 30, 2023

    @laurmaedje
    Member

    I also meant pages with this changelog entry. It should keep the first and last two lines together and also headings attached. The latter part might be buggy though. If you have concrete examples where it doesn't work that would be helpful.

  3. matthewelmer commented on Apr 30, 2023

    @matthewelmer
    Author
    *Summarizing the algorithm:* \
    + Sample from initial Gaussian
    + Propagate according to dynamics (with process noise!)
      - Compute and store conditional mean and covariance for later analysis
    + Update weights per-sample based on how well it agrees with your measurement
      - Compute and store conditional mean and covariance for later analysis
    + Resample if necessary
    + Repeat 2 - 4 till you're all out of measurements

    image

    = 3. Bootstrap Particle Filter (BPF) -- With Resampling
    #figure(
      table(image("../figures/bpf_with_re.svg")),
      caption: [
        Position Error and $plus.minus 3 sigma$ Interval for BPF resampled when $hat(N)_"eff" < 0.1N$
      ]
    )

    image

    = 4. Comparison Between EKF and BPFs
    ```
    EKF                   RMSE: 4.22e-02
    BPF (no resampling)   RMSE: 4.40e-02 (+4.4%)
    BPF (with resampling) RMSE: 4.23e-02 (+0.2%)
    
    EKF                    MAE: 3.67e-02
    BPF (no resampling)    MAE: 3.70e-02 (+1.0%)
    BPF (with resampling)  MAE: 3.66e-02 (-0.1%)
    ```
    The BPF with resampling closesly trades blows with the EKF (+0.2% RMSE, -0.1% MAE), and the BPF without
    resampling is only marginally worse than the EKF (+4.4% RMSE, +1.0% MAE)! I do think that the BPF without
    resampling simply got lucky, however, since the trajectory sorta stagnates at the end so there's not much
    of a threat posed to the few particles by highly nonlinear dynamics.
    

    image

    #figure(
      table(image("../figures/bpf_with_re_neff.svg")),
      caption: [
        Number of effective particles for BPF resampled when $hat(N)_"eff" < 0.1N$
      ]
    )
    
    Resampling was performed 6 times. The black line demarcates the threshold. Notice that $neff$ stays above
    $N_"thr"$ longer when the dynamics are greater! I'm not sure if this is because it's more nonlinear or
    something else, but it's something to maybe experiment with when I have some spare time.
    

    image

  4. laurmaedje commented on Apr 30, 2023

    @laurmaedje
    Member

    Okay, I see. The heading fails to attach if what follows isn't a paragraph.

  5. Enivex commented on Apr 30, 2023

    @Enivex
    Collaborator

    Some of these will presumably improve once typst has floats.

    Is the idea similar to https://ctan.org/pkg/lua-widow-control?lang=en ?

  6. changed the title [-]Add support for widow and orphan prevention within pages[/-] [+]Headings do not always respect widow and orphan prevention[/+] on May 8, 2023
  7. added
    bug
    layout
    and removed
    feature request
    on May 8, 2023
  8. ecstrema commented on Dec 1, 2023

    @ecstrema
    Contributor

    Some of these will presumably improve once typst has floats.

    What will floats change to that issue?

  9. mattharrison commented on Jan 22, 2025

    @mattharrison

    I'm seeing a lot of heading orphans in my book. I'm using typst version 0.12.0.

    Is this bug really fixed?

  10. Enivex commented on Jan 22, 2025

    @Enivex
    Collaborator

    I'm seeing a lot of heading orphans in my book. I'm using typst version 0.12.0.

    Is this bug really fixed?

    Can you provide a minimal example?

    The most likely explanation is that you have a show rule on headings that does not use the sticky flag on the block

  11. mattharrison commented on Jan 22, 2025

    @mattharrison

    Here's a minimal (using my preamble) example,

    Section 2 of chapter 1 is hanging out by itself at the bottom of page 5

    Image

    // Some definitions presupposed by pandoc's typst output.
    #let horizontalrule = [
      #line(start: (25%,0%), end: (75%,0%))
    ]
    
    #let endnote(num, contents) = [
      #stack(dir: ltr, spacing: 3pt, super[#num], contents)
    ]
    
    // Matt Start
    
    #import "@preview/in-dexter:0.5.3": *
    
    
    #let mainfont = "Palatino"
    #let codefont = "Iosevka Fixed SS01"
    #let headingfont = "Roboto"
    #let title = [Effective Bug 1044]
    #let subtitle = [Opinions for ...]
    #let author = [Matt Harrison]
    
    // set monospace font
    #show raw: set text(font: codefont)
    
    
    #set text(
      lang: "en",
      font: mainfont,
      //weight: 100,
      size: 10pt,
      costs: (orphan: 2%)
    )
    
    #set page(
      //paper: "9in",
      binding: left,
      width: 6in,
      height: 9in,
      margin: (inside: .75in, outside: .5in),
    )
    #set par(
      justify: true,
      leading: 0.52em, // this is the space between lines
    )
    
    // Place new chapters on a new odd page
    #let chaptername = state("chaptername", none)
    
    #show heading.where(level: 1): it => {//
      chaptername.update(it.body)
      it
    }
    
    
    #show heading.where(level: 2): it => {//
      [#it.body]
    }
    
    
    
    #show figure.caption: it => {
      set text(font: headingfont, size: 8pt)
      it
    }
    
    //table tweaks
    //------------------
    
    #let frame(stroke) = (x, y) => (
      left: if x > 0 { 0pt } else { stroke },
      right: stroke,
      top: if y < 2 { stroke } else { 0pt },
      bottom: stroke,
    )
    
    #set table.hline(stroke: 0.6pt)
    
    #set table(
      stroke: none,
      align: left,
    )
    
    // Bold titles in tables
    #show table.cell.where(y: 0): set text(font: headingfont, weight: "bold"
    )
    
    // TODO - I had this before but it broke figure captions (they went onto a new page)
    // span pages with tables
    //#show figure: set block(breakable: true)
    
    // don't justify tables
    #show table: set par(justify: false)
    
    // ----------------------
    // title page
    
    #v(1fr)
    
    #align(center)[
      #text(title, size:24pt)\
      #text(subtitle, size:18pt)
      #v(.5fr)
      #text(author, size:12pt)
    ]
    
    #v(4fr)
    
    #pagebreak()
    
    // ------------------
    // Copyright page
    
    #v(1fr)
    
    © 2024 Matt Harrison
        All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher.
    
    #pagebreak()
    
    // ----------------------
    // Table of Contents
    
    #outline(depth: 1, title: [Table of Contents #v(12pt)])
    
    
    #pagebreak()
    
    // ----------------------
    // Start numbering after TOC
    
    // page numbering
    #set page(numbering: "1")
    #set page(footer: context [
      #text(title)
      #h(1fr)
      #counter(page).display(
        "1",
        both: true,
      )
    ])
    
    #let current-chapter = state("current-chapter", none)
    #let chapter(title) = {
      pagebreak(weak:true, to: "odd")
      current-chapter.update(title) 
      set text(
        font: headingfont, size: 20pt,
      )
      heading(level: 1, title)
    }
    
    #let current-section = state("current-section", none)
    #let section(title) = {
      current-section.update(title)
      set text(
        font: headingfont,
      )
      heading(level: 2, title)
    }
    
    
    #let current-chapter = state("current-chapter", none)
    #let current-section = state("current-section", none)
    
    #set page(
      numbering: "1",
    )
    
    #let clean_content(content) = {
      // Convert content to string, clean it, then back to content
      eval(repr(content)
        .trim()
        .replace("\n", " ")
        .replace(regex("\s+"), " "))
    }
    
    #show heading: it => {
      set text(font: headingfont)
      
    //  let cleaned_content = clean_content(it.body)
      let cleaned_content = it.body
      
      if it.level == 1 {
    
        pagebreak(weak: true, to: "odd")
        text(size:16pt)[#it]
        v(2em)
        current-chapter.update(cleaned_content)
      } else if it.level == 2 {
    
        v(1em)
        it
        v(1em)
            current-section.update(cleaned_content)
      } else if it.level == 3 {
        text(size:10pt, weight: "bold")[#it]
        v(.5em)
      }
    
      
    }
    
    
    #set page(
      footer: context {
        let loc = here()
        if calc.rem(loc.page(), 2) == 0 {
          //left page
          set text(
            font: headingfont,
            size: 8pt,
          )
          [#counter(page).display("1")
            #h(1fr)
            #current-section.at(loc)
          ]
        } else {
          //right page
          set text(
            font: headingfont,
            size: 8pt,
          )
          [#current-chapter.at(loc) 
            #h(1fr)
            #counter(page).display("1")
          ]
        }
      }
    )
    
    
    // callout
    #let callout(body, title: "Callout", fill: blue) = {
      block(fill:fill, width: 100%, inset: 8pt,
      [
        #text(font: headingfont, weight: "bold")[#title]
        #linebreak()
        #text[
          #body
        ]
      ])
    }
    #let note = callout.with(title: "Note", fill: rgb(243,245,218)
    )
    
    
    = Chapter 1
    
    #lorem(100)
    
    == Section 1
    
    #lorem(240)
    
    == Section 2
    
    #lorem(100)
    
    = Chapter 2
    
    
    #lorem(100)
    
    == Section 1
    
    #lorem(100)
    
    == Section 2
    
    #lorem(100)
    
    
    
  12. Enivex commented on Jan 22, 2025

    @Enivex
    Collaborator

    I'm not sure I'd call that minimal, since there's a lot of stuff there that has nothing to do with headings.

    The problem is this show rule

    #show heading.where(level: 2): it => {//
      [#it.body]
    }
    
  13. Andrew15-5 commented on Jan 22, 2025

    @Andrew15-5
    Contributor

    @mattharrison, please use a details tag to hide your code block, as it's just waaay too long and makes navigating through messages hard (though it is already a closed issue). (You can edit your comment.) You can also do it for the output image.

    <details><summary>Summary text</summary>
    
    ```typ
    // Your Typst code.
    ```
    
    </details>
    Summary text
    // Your Typst code.

    As a side note on example making, from devenv repository I discovered https://sscce.org/, which is pretty much what everyone should know about MRE etc. And it's very short, too.

  14. ecstrema commented on Jan 23, 2025

    @ecstrema
    Contributor

    The problem is this show rule

    #show heading.where(level: 2): it => {//
      [#it.body]
    }
    

    Mind explaining why this causes the algorithm to stop preventing orphans? I'd argue that this show rule is something anyone could write without expecting it to break all sorts of other things. Maybe a warning would help?

  15. laurmaedje commented on Jan 23, 2025

    @laurmaedje
    Member

    Headings should be blocks, so you should write #show heading.where(level: 2): it => block(it.body). Then, it will automatically pick up the built-in rule show heading: set block(sticky: true).

    Since a show rule can fully override things, it's also easy to break stuff. Documentation on best practices when writing show rules (in general, and for specific elements) would be good. But warning is not really feasible as the compiler can't guess what you want.

  16. ecstrema commented on Jan 23, 2025

    @ecstrema
    Contributor

    I'm not sure this really is the problem though.

    minimum example
    
    
    #show heading: it => {
      if it.level == 2 {
        [#it]
      }
      else {
        it
      }
    
      
    }
    
    
    
    = Chapter 1
    
    #lorem(607)
    
    == Section 2
    
    #lorem(501)
    
    = Chapter 2
    
    
    #lorem(100)
    
    == Section 1
    
    #lorem(100)
    
    == Section 2
    
    #lorem(100)
    
    

    I can't reproduce it with this minimal example. I can't manage to get orphans with this setup.

  17. ecstrema commented on Jan 23, 2025

    @ecstrema
    Contributor

    Headings should be blocks, so you should write #show heading.where(level: 2): it => block(it.body). Then, it will automatically pick up the built-in rule show heading: set block(sticky: true).

    Is there anywhere you'd like to cast a block to content? (I'm not sure what the exact terminology for this is though. Is a block content? Anyway, cast it to non-block)

    Else I'm pretty sure a warning like You are currently mapping a block to a __non_block__, which may make it lose some of its properties, including orphans prevention.

  18. laurmaedje commented on Jan 23, 2025

    @laurmaedje
    Member

    I can't reproduce it with this minimal example. I can't manage to get orphans with this setup.

    That's because you wrote #it and that way you get your show rule on top of the built-in one. In your example, you used #it.body, thus throwing away the built-in show rule.

    Else I'm pretty sure a warning like You are currently mapping a block to a non_block, which may make it lose some of its properties, including orphans prevention.

    In normal circumstances, headings should be blocks, but it's perfectly valid to create a non-block heading if you want a run-in heading style, where the deeper headings become part of the paragraphs. Headings aren't blocks per se, they are just usually shown as blocks.

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

    buglayout

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions