Skip to content

Add support for relative and fractional spacing in math - #6358

Open
mkorje wants to merge 3 commits into
typst:mainfrom
mkorje:math-spacing
Open

mkorje wants to merge 3 commits into
typst:mainfrom
mkorje:math-spacing

Conversation

@mkorje

@mkorje mkorje commented May 31, 2025 •

Copy link
Copy Markdown
Collaborator

Closes #6213 and half does #5780. I still need to sort out fractional spacing in display math.

@laurmaedje laurmaedje added layout Related to the layout category, which is about composing, positioning, etc. math Related to math category, with its syntax, layout, etc. labels Jun 4, 2025
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Jun 23, 2025
@supertrianguloid

Copy link
Copy Markdown

Hi! This would be super useful! What is the PR waiting on? Is there anything I can help with?

@mkorje

mkorje commented Aug 6, 2025

Copy link
Copy Markdown
Collaborator Author

Hi! This would be super useful! What is the PR waiting on? Is there anything I can help with?

Sorry for the delay. I still need to figure out a sane way to deal with fractional spacing in display math. The main goal is so that without any alignment points, this will work: #5780 (comment). I'm still not sure what is the most logical way it would work when there are alignment points though. If you have any ideas here that would be great!

@mkorje mkorje left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've rebased this on main so it is working again. I was thinking we just leave out fractional spacing in display math for now, so this can finally be merged. EDIT: Here's some ideas I wrote up https://typst.app/project/rABIqxO4x98oguiHSQplgv

I left some comments to note on review. Mainly some MathML export things.

Comment on lines +508 to +511
MathItem::Fractional(_) => {
// TODO: should probably warn.
return Ok(());
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't silently ignore this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a warning would be good here, too.

Comment on lines +499 to 506
MathItem::Absolute(amount, _, _) => {
// TODO: Relative spacing has no effect in MathML Core.
ctx.push(
HtmlElem::new(tag::mspace)
.with_attr(attr::width, amount.to_css(()))
.pack(),
);
return Ok(());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just pass the absolute part, amount.abs, to the width attribute. If we keep it as is, browsers complain, e.g. in Firefox the HTML from $A #h(50% + 10pt) B$ says

Error in parsing MathML attribute value ‘calc(50% + 10pt)’ as length.  Attribute ignored.
Error in parsing the value ‘calc(50% + 10pt)’ for ‘width’ attribute of <mspace/>.  Attribute ignored.

Should we warn then if the relative part is non-zero?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. I think we should warn, yes.

Comment on lines 162 to 173
pub fn into_frame(self) -> Frame {
match self {
Self::Glyph(glyph) => glyph.into_frame(),
Self::Frame(fragment) => fragment.frame,
Self::Tag(tag) => {
let mut frame = Frame::soft(Size::zero());
frame.push(Point::zero(), FrameItem::Tag(tag));
frame
}
Self::Space(_) => Frame::soft(self.size()),
Self::Space(_) | Self::Fractional(_) => Frame::soft(self.size()),
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just flagging this. If a fractional length shows up nested somewhere, we just turn it into an empty frame of size zero.

Comment on lines -179 to +181
if prev_width.at(*prev_font_size) < width.at(font_size) {
if prev_width.map(|abs| abs.at(*prev_font_size))
< width.map(|abs| abs.at(font_size))
{

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note, the PartialOrd implementation for Rel<T> is

impl<T: NumericLength + PartialOrd> PartialOrd for Rel<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.rel.is_zero() && other.rel.is_zero() {
self.abs.partial_cmp(&other.abs)
} else if self.abs.is_zero() && other.abs.is_zero() {
self.rel.partial_cmp(&other.rel)
} else {
None
}
}
}

This seems incomplete?

Also, the current implementation violates transitivity: a = -1pt + 0%, b = 0pt + 0%, and c = 0pt + 10% has a < b, b < c, but comparing a and c gives None.

@mkorje
mkorje marked this pull request as ready for review September 25, 2026 05:20
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Sep 28, 2026

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be okay with leaving out the fractional spacing in display math for now.

On that matter though: I think I prefer design C. In inline layout fractional h spacing is line by line and C keeps it consistent with that. D would be a bit more consistent with fr in grid/table cells. So I suppose which one feels more consistent depends on whether you think about math line by line or as a grid. I suspect most people think line-by-line. A potential future tab-stop feature in inline layout would likely also keep fr per line.

The main downside is that the frs will not necessarily fill up the entire remaining space in the line if they are used across alignment points. But that would be the cost of keeping 1fr consistent across the line...

Comment on lines +499 to 506
MathItem::Absolute(amount, _, _) => {
// TODO: Relative spacing has no effect in MathML Core.
ctx.push(
HtmlElem::new(tag::mspace)
.with_attr(attr::width, amount.to_css(()))
.pack(),
);
return Ok(());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. I think we should warn, yes.

Comment on lines +508 to +511
MathItem::Fractional(_) => {
// TODO: should probably warn.
return Ok(());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a warning would be good here, too.

/// Explicit spacing with the font size at the point of creation. The
/// boolean indicates whether the spacing is weak.
Spacing(Length, Abs, bool),
Absolute(Rel<Length>, Abs, bool),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Absolute" seem like a misnomer. Maybe just Rel like for the Spacing and Sizing enums?

Comment on lines +193 to +199
// Same as explicit spacing that isn't weak.
RawMathItem::Item(MathItem::Fractional(_)) => {
last = None;
space = None;
resolved.push(item);
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to comment that fractional spacing should destroy weak spacing (unlike explicit spacing), but it appears that this is already inconsistent between h and v in normal layout 🫠

A #h(6cm, weak: true) #h(1fr) B #h(1fr)
#pagebreak()
A #v(6cm, weak: true) #v(1fr) B #v(1fr)

In principle, the idea was that fractional spacing is destructive and therefore eats up the weak spacing.

// Validate that ignorant elements are layouted
#context test(counter("test").get(), (3,))

--- math-spacing-relative paged ---

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this test *-block then?

--- math-spacing-relative-inline paged ---
// Test relative spacing in inline math.
#let mtext = text.with(font: "Libertinus Serif")
Hello#h(40%)world \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use A and B here for consistency and a slightly smaller image.

Comment on lines +203 to +213
let mut frame_prev =
std::mem::replace(&mut frame, Frame::soft(Size::zero()));

finalize_frame(&mut frame_prev, x, ascent, descent);
items.push(InlineItem::Frame(frame_prev));
empty = true;

x = Abs::zero();
ascent = Abs::zero();
descent = Abs::zero();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicating the entire logic from below is a bit unfortunate.

frame
}
Self::Space(_) => Frame::soft(self.size()),
Self::Space(_) | Self::Fractional(_) => Frame::soft(self.size()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose nested fr spacing could also be a warning. Would that have to touch a lot of code?

if let Some(f_next) = iter.peek()
&& !is_space(f_next)
{
if iter.peek().map(is_space).is_some() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sus because the map isn't doing anything when followed by is_some. Should this be is_some_and(is_space)?

Even then, though, the behavior differs if we're at the end of the equation as the else branch would push a zero-width spacing. This is almost unnoticable but could theoretically affect smartquotes.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

layout Related to the layout category, which is about composing, positioning, etc. math Related to math category, with its syntax, layout, etc. waiting-on-author Pull request waits on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spacing with relative size doesn't work inside block math equation?

3 participants