You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Boxing an inline equation shifts the baseline #4796
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.
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.
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.
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?
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(...))).
Activity
Leedehai commentedon Aug 20, 2024
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.
Enivex commentedon Aug 20, 2024
It can't possibly be WAI. Boxing an inline equation should never shift it.
Enter-tainer commentedon Aug 21, 2024
previous discussion in discord https://discord.com/channels/1054443721975922748/1088371867913572452/1273702753415200890
mkorje commentedon Sep 18, 2024
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_frameis called to layout theBoxElem's body content (and ultimatelycrate::layout::flowis called). An inlineEquationElem's baseline is preserved just fine with inline content all the way until thefinish_regionmethod inFlowLayouteris called. Here the final frame for one region is created, where all of theFlowItems 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
BoxElemas 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 modifyingTextElem, as this is on theStyleChainand 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 theFlowItem's frame's baseline shouldn't be used (mostly leads to stuffing up the baseline of aBoxElemin 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 theInlineItems 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 forInlineElem. Regardless, I'm way out of my depth here, especially if solving this might require a large refactor or a new approach.francoisthire commentedon Nov 1, 2024
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
boxfunction? If so, is there a way to know a good value or this is trials and errors?wisp3rwind commentedon Apr 29, 2025
Just stumbled upon this in another context: Adding an inline icon with
image, and wrapping that in aboxwith shifted baseline to shift the icon vertically: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
fix(typst): avoid box wrapper due to known baseline bug