Skip to content

Add math.stretch element function - #5030

Merged
laurmaedje merged 8 commits into
typst:mainfrom
mkorje:math-stretch-elem
Sep 26, 2024
Merged

laurmaedje merged 8 commits into
typst:mainfrom
mkorje:math-stretch-elem

Conversation

@mkorje

@mkorje mkorje commented Sep 25, 2024 •

Copy link
Copy Markdown
Collaborator

Closes #1304. This PR continues @Leedehai's work in #4569.

This PR adds the element function math.stretch, which allows you to stretch a glyph horizontally or vertically (only if the glyph is stretchable in the font - this means it has entries in the MathVariants table). It automatically stretches the glyph along the correct axis. You specify a size to stretch the glyph to with the size parameter, which is auto or relative (to the glyph's current size). This size parameter defaults to auto, which will only have an effect if a top/bottom attachment is attached to stretch(...).

When there are top/bottom attachments present, and when math.stretch(attach: true) is set (true is the default), the size parameter will instead be relative to the top/bottom attachments' width. Here is also where auto comes into play, which will stretch the glyph to the size of the top/bottom attachments.

The example below sums up how it all works.

Code Image
#set page(width: 5cm, height: auto)

$
  // Stretch vertically.
  stretch(||, size: #2em)
  stretch(arrow.t, size: #200%)
  stretch(angle.r, size: #(200% + 0.5em)) \

  // Stretch horizontally.
  stretch(=, size: #3em) \
  stretch(arrow.hook, size: #3em) \

  // Nested stretch.
  stretch(
    stretch(harpoon.lb, size: #2em),
    size: #200%,
  ) \
  stretch(
    stretch(harpoon.lb, size: #8em),
    size: #50%,
  ) \
  stretch(harpoon.lb, size: #4em) \
  
  // Attachments
  stretch(<-)^"define" quad
  stretch(->)_"define" quad
  // Attachments don't do anything to the 
  // vertically stretched glyphs.
  stretch(arrow.t)^"define" quad
  stretch(arrow.b)_"define" \

  // Relative size with attachments.
  stretch(arrow.hook, size: #150%)_"injective map" \
  stretch(arrow.hook, size: #50%)_"injective map" \
  stretch(arrow.hook)_"injective map" \

  // Absolute size with attachments.
  // Acts the same as if there weren't attachments.
  stretch(arrow.l.r.double, size: #3em)^"text" \
  stretch(arrow.l.r.double, size: #3em) \

  // You can disable sizing being relative to
  // attachments when needed.
  // Or you can always disable it globally with
  // a show rule.
  stretch(->>, size: #200%, attach: #false)_"surjection" \
  stretch(->>, size: #200%)
$

test

See the tests for some more examples.

Implementation

I've been able to avoid adding to the proliferation of fields on the structs in fragment.rs by checking if the base of an attachment is a StretchElem and then peeking inside.

The StretchElem lives in stretch.rs. I've moved the delimiter_alignment function there from mod.rs. I've also changed some of the code in lr.rs to avoid some repetition, by relying on the functions in stretch.rs.

If a glyph for some reason has both horizontal and vertical variants, I've opted to panic for now. This should probably never happen, and if it does we'd want to know the culprit font...

I also don't know if the name of the parameter attach is great. Idk what else to name it though.

Comment thread crates/typst/src/math/attach.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread tests/ref/math-stretch-attach.png Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved
Comment thread crates/typst/src/math/stretch.rs Resolved

@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.

found one more thing. otherwise, it looks ready to merge from my end.

@laurmaedje
laurmaedje added this pull request to the merge queue Sep 26, 2024
@laurmaedje

Copy link
Copy Markdown
Member

Thank you. Nice work! Also thanks to @Leedehai for getting the ball rolling on this!

Merged via the queue into typst:main with commit e00e3e4 Sep 26, 2024
@mkorje
mkorje deleted the math-stretch-elem branch September 26, 2024 14:50
@mkorje

mkorje commented Sep 30, 2024

Copy link
Copy Markdown
Collaborator Author

I've found an issue I made when implementing this: stretching things vertically doesn't work at all with an attachment.

I'll try to fix this.

Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make arrows in math stretchable

2 participants