Skip to content

Large inline equations can overlap with other lines #1028

Open
0 of 1 issue completed
Open

Description

@stemvork

Description

The square root is rendered through the vector. See HTI Dictaat Wiskunde 3.pdf.

Reproduction URL

https://typst.app/project/pxKIAuoZZl7hgc5cpu4nnH (not shared)
https://typst.app/project/rrfl7-zYW_qh1tI8FeYwXE (read-only shared link)

Operating system

Web app

Typst version

  • I am using the latest version of Typst

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    bug
    on Apr 28, 2023
  2. changed the title [-]rendering: vector and square root[/-] [+]Paragraph leading does not respect large inline equations[/+] on May 8, 2023
  3. added
    math
    text
    on May 8, 2023
  4. added
    layout
    and removed
    text
    on Sep 6, 2023
  5. pauladam94 commented on Feb 7, 2024

    @pauladam94

    What is the state of this isssue ? It's actually a major bug because it limits a lot doing package that user will use with math block a lot.
    Is the value of bottom-edge and top-edge suppose to be the fix like mention in #1626 ?
    Because those parameters doesn't fix at all the problem that I have mention in #3368.

  6. laurmaedje commented on Feb 7, 2024

    @laurmaedje
    Member

    #1626 fixed a different issues. Fixing this properly would require some smart heuristic in the paragraph layout. Note that you can work around this by using block equations instead of inline equations.

  7. pauladam94 commented on Feb 7, 2024

    @pauladam94

    @laurmaedje Okk thank you ! There is a fix !

  8. changed the title [-]Paragraph leading does not respect large inline equations[/-] [+]Large inline equations can overlap with other lines[/+] on Mar 21, 2024
  9. Enter-tainer commented on Mar 21, 2024

    @Enter-tainer
    Contributor

    Fixing this properly would require some smart heuristic in the paragraph layout.

    Can you elaberate more about this? I currently don't quite understand why this happens. Which code is related?

  10. laurmaedje commented on Mar 21, 2024

    @laurmaedje
    Member

    @Enter-tainer It would be easy to just always expand lines to fit the equation. But that's also undesirable because it leads to uneven line spacing in many very common cases. Hence, we would need the paragraph layout to only expand the leading if truly necessary.

  11. Enter-tainer commented on Mar 21, 2024

    @Enter-tainer
    Contributor

    I wonder if there is any example about this. I cannot understand "It would be easy ... But that's also undesirable" well maybe because I cannot think of when it will become ugly. Is there a place I can tweak the code and see what it looks like if I want typst "always expand lines to fit the equation"?

  12. Enter-tainer commented on Mar 22, 2024

    @Enter-tainer
    Contributor

    I wonder if LaTeX does such "smart heuristic" when layouting inline equations. It seems that it always expand line height whenever it doesn't fit.

    I also wonder if it is easy to fix(or implement) "always expand lines to fit the equation". It looks like typst currently tries to expand the line but for some reason(maybe there are already some heuristics?) it fails to correctly do it.

  13. laurmaedje commented on Apr 1, 2024

    @laurmaedje
    Member

    @Enter-tainer The following example demonstrates the case I'm talking about:
    Bildschirmfoto 2024-04-01 um 17 29 37

    If the bounding boxes would be expanded fully, Typst's paragraph layout would add leading in between them and space the lines apart a lot. This case occurs quite frequently and the results would look really bad. For this reason, Typst has the naive workaround of just always pushing the bounding box of inline math somewhat inwards. A cleaner solution would be to ensure that nothing overlaps while keeping the lines spaced normally somehow.

    As you can see, LaTeX also doesn't space the lines apart:
    Bildschirmfoto 2024-04-01 um 17 26 15

    I'm not sure how it works in LaTeX, but it would only partly apply to Typst anyway because their line spacing model is very different. It's based on baselineskip rather than leading in between line bounding boxes.

  14. Enter-tainer commented on Apr 2, 2024

    @Enter-tainer
    Contributor

    @laurmaedje which files are relevant? Can you give me a list so I can learn how typst handles this at this moment.(And we can discuss how to improve it) I've discussed offline with @peng1999 and @Myriad-Dreamin about this.

  15. peng1999 commented on Apr 2, 2024

    @peng1999
    Contributor

    I'm not sure how it works in LaTeX, but it would only partly apply to Typst anyway because their line spacing model is very different. It's based on baselineskip rather than leading in between line bounding boxes.

    What do you think of this method: we calculate the baseline skip by adding the leading, along with the main text's ascender and descender height. We then adhere to this baseline skip unless formulas or other elements cause the bounding box to collide with the previous line's descender.

    This is basically switching from the original line spacing model to the LaTeX model. Hopefully at most of the time they are compatible.

  16. Enter-tainer commented on May 22, 2024

    @Enter-tainer
    Contributor

    Typst has the naive workaround of just always pushing the bounding box of inline math somewhat inwards

    Relevant code is:

    let slack = ParElem::leading_in(styles) * 0.7;
    let top_edge = TextElem::top_edge_in(styles).resolve(font_size, &font, None);
    let bottom_edge =
    -TextElem::bottom_edge_in(styles).resolve(font_size, &font, None);
    let ascent = top_edge.max(frame.ascent() - slack);
    let descent = bottom_edge.max(frame.descent() - slack);

  17. Enter-tainer commented on May 22, 2024

    @Enter-tainer
    Contributor

    edit: update code and image
    image

    I'm trying to replicate the LaTeX behavior in a very rough way. And it looks good

    1. Remove hacks for inline math bbox
    2. When actually layouting lines, follow the algo described in texbook
    diff --git a/crates/typst/src/layout/flow.rs b/crates/typst/src/layout/flow.rs
    index 6f3b8f16..236ee801 100644
    --- a/crates/typst/src/layout/flow.rs
    +++ b/crates/typst/src/layout/flow.rs
    @@ -279,8 +279,15 @@ impl<'a> FlowLayouter<'a> {
                 }
             }
     
    -        for (i, frame) in lines.into_iter().enumerate() {
    +        for (i, frame) in lines.clone().into_iter().enumerate() {
                 if i > 0 {
    +                let baselineskip = Abs::pt(14.0);
    +                let leading =
    +                    if lines[i - 1].descent().abs() + frame.ascent() > baselineskip {
    +                        Abs::pt(1.0)
    +                    } else {
    +                        baselineskip - lines[i - 1].descent().abs() - frame.ascent()
    +                    };
                     self.layout_item(engine, FlowItem::Absolute(leading, true))?;
                 }
     
    diff --git a/crates/typst/src/layout/inline/mod.rs b/crates/typst/src/layout/inline/mod.rs
    index 5a74d3d2..fa9749a9 100644
    --- a/crates/typst/src/layout/inline/mod.rs
    +++ b/crates/typst/src/layout/inline/mod.rs
    @@ -1282,23 +1282,23 @@ fn finalize(
             .collect::<SourceResult<_>>()?;
     
         // Positive ratios enable prevention, while zero and negative ratios disable it.
    -    if p.costs.orphan().get() > 0.0 {
    -        // Prevent orphans.
    -        if frames.len() >= 2 && !frames[1].is_empty() {
    -            let second = frames.remove(1);
    -            let first = &mut frames[0];
    -            merge(first, second, p.leading);
    -        }
    -    }
    -    if p.costs.widow().get() > 0.0 {
    -        // Prevent widows.
    -        let len = frames.len();
    -        if len >= 2 && !frames[len - 2].is_empty() {
    -            let second = frames.pop().unwrap();
    -            let first = frames.last_mut().unwrap();
    -            merge(first, second, p.leading);
    -        }
    -    }
    +    // if p.costs.orphan().get() > 0.0 {
    +    //     // Prevent orphans.
    +    //     if frames.len() >= 2 && !frames[1].is_empty() {
    +    //         let second = frames.remove(1);
    +    //         let first = &mut frames[0];
    +    //         merge(first, second, p.leading);
    +    //     }
    +    // }
    +    // if p.costs.widow().get() > 0.0 {
    +    //     // Prevent widows.
    +    //     let len = frames.len();
    +    //     if len >= 2 && !frames[len - 2].is_empty() {
    +    //         let second = frames.pop().unwrap();
    +    //         let first = frames.last_mut().unwrap();
    +    //         merge(first, second, p.leading);
    +    //     }
    +    // }
     
         Ok(Fragment::frames(frames))
     }
    diff --git a/crates/typst/src/math/equation.rs b/crates/typst/src/math/equation.rs
    index 947f01f2..a78fd747 100644
    --- a/crates/typst/src/math/equation.rs
    +++ b/crates/typst/src/math/equation.rs
    @@ -233,13 +233,12 @@ impl Packed<EquationElem> {
                 let MathParItem::Frame(frame) = item else { continue };
     
                 let font_size = scaled_font_size(&ctx, styles);
    -            let slack = ParElem::leading_in(styles) * 0.7;
                 let top_edge = TextElem::top_edge_in(styles).resolve(font_size, &font, None);
                 let bottom_edge =
                     -TextElem::bottom_edge_in(styles).resolve(font_size, &font, None);
     
    -            let ascent = top_edge.max(frame.ascent() - slack);
    -            let descent = bottom_edge.max(frame.descent() - slack);
    +            let ascent = top_edge.max(frame.ascent());
    +            let descent = bottom_edge.max(frame.descent());
                 frame.translate(Point::with_y(ascent - frame.baseline()));
                 frame.size_mut().y = ascent + descent;
             }
    
  18. pauladam94 commented on Oct 11, 2024

    @pauladam94

    @Enter-tainer The Image you have given is an image of the output given by Typst after your fix ? Or is it the one from Latex ?

    I think the next step of this issue is to compare the behavior of :

    • Latex
    • Typst before your fix
    • Typist after your fix

    First it has to be done visually on "edge cases". Then we have to check that other tests are fine with that change.

  19. Enter-tainer commented on Oct 11, 2024

    @Enter-tainer
    Contributor

    My pr #4236 has been abandoned for whatever reason i cannot remember.

  20. jassler commented on Mar 30, 2026

    @jassler
    Contributor

    For what it's worth, I added these examples in a duplicate issue (sorry for not checking if the issue was already reported):

    In LaTeX, it seems that if a line contains no descenders (e.g., no p, y, g, j), the overall line height / distance to the next baseline is reduced.

    Image

    Typst does not seem to consider increasing (decreasing) the spacing between lines if descenders are present (not present).

    Image
  21. AprilGrimoire commented on Mar 31, 2026

    @AprilGrimoire

    The same issue happens here:

    Image
      - For each $(i + 1 < n) , m < omega, exists x_1, dots.c, x_m (and.big_(1 <= j < k <= m) x_j != x_k and and.big_(1 <= j <= m) P_i (x_j) and not P_(i + 1) (x_j))$
      - For each $m < omega, exists x_1, dots.c, x_m (and.big_(1 <= j < k <= m) x_j != x_k and and.big_(1 <= j <= m) P_n (x_j))$
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

    buglayoutmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions