Skip to content

Boxing an inline equation shifts the baseline #4796

Closed

Description

@Enivex

Description

$p/q$ #box($p/q$)

image

Reproduction URL

No response

Operating system

No response

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    math
    on Aug 20, 2024
  2. Leedehai commented on Aug 20, 2024

    @Leedehai
    Contributor

    This is because a multi-line math expression, like a fraction, needs to set its baseline at the math axis height (a constant provided by the font, then scaled by the font size), but a boxed frame probably set its baseline with another value or never set it.

    I'm not sure if this boxed frame's behavior is WAI or a bug, though.

  3. Enivex commented on Aug 20, 2024

    @Enivex
    CollaboratorAuthor

    This is because a multi-line math expression, like a fraction, needs to set its baseline at the math axis height (a constant provided by the font, then scaled by the font size), but a boxed frame probably set its baseline with another value or never set it.

    I'm not sure if this boxed frame's behavior is WAI or a bug, though.

    It can't possibly be WAI. Boxing an inline equation should never shift it.

  4. mkorje commented on Sep 18, 2024

    @mkorje
    Collaborator

    I've looked into this and it isn't really an issue of BoxElem's baseline being set incorrectly, or the baseline of multi-line math expression needing to be at the math axis height, per se.

    crate::layout::layout_frame is called to layout the BoxElem's body content (and ultimately crate::layout::flow is called). An inline EquationElem's baseline is preserved just fine with inline content all the way until the finish_region method in FlowLayouter is called. Here the final frame for one region is created, where all of the FlowItems are laid out. A baseline is not set for this final frame (and so it is equal to its height), as (I think) the notion of baseline for a region doesn't really make sense.

    Now this only becomes a problem with BoxElem as it layouts its body inline, as part of a paragraph. The baseline now does matter, as there's potentially other content with a baseline around it with which it should match. At this point though, it's too late to try and recover the correct baseline.

    In theory then, this isn't solely an issue with inline equations, but any element that is in the "last line" of BoxElem's body (once laid out) which has a baseline different from its height. Note that this doesn't include elements where the change in baseline is from modifying TextElem, as this is on the StyleChain and is dealt with earlier when building a line by translating the frame (see here). I haven't been able to find another element/situation where this happens though.

    A solution I tried is to calculate a baseline in finish_region, and set the final frame's baseline to this value. Though this breaks 16 tests as there are some situations where the FlowItem's frame's baseline shouldn't be used (mostly leads to stuffing up the baseline of a BoxElem in other situations). I got this down to 11 by only setting the baseline if the frame size isn't changed (see here).

    Maybe adding TextElem::set_baseline(...) to the InlineItems created (but these aren't content...) from an inline equation would solve this nicely? But there's the problem of when/where to do this exactly, since the layouter is special for InlineElem. Regardless, I'm way out of my depth here, especially if solving this might require a large refactor or a new approach.

  5. francoisthire commented on Nov 1, 2024

    @francoisthire

    I stumbled across a similar issue.

    What is the best way to get around this issue at the moment? Setting the baseline parameter of the box function? If so, is there a way to know a good value or this is trials and errors?

  6. wisp3rwind commented on Apr 29, 2025

    @wisp3rwind

    Just stumbled upon this in another context: Adding an inline icon with image, and wrapping that in a box with shifted baseline to shift the icon vertically:

    Text before the icon
    #box(stroke: 1pt)[
      #box(
        baseline: 0.2em,
        //image("icon.svg", height: 1em),
        "X"  // would be the icon in my real usecase
      )
      Text after the icon
    ]

    where both pieces of text will not line up: Due to the present issue, the outer box' baseline is not the same as the baseline of the inner text (when removing the outer box, things work correctly).

    EDIT: Working around this now via box(... /* no baseline adjustment here */, move(dy: ..., image(...))).

    EDIT: Possible somewhat related: #5225

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

    bugmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions