Skip to content

Make arrows in math stretchable #1304

Closed

Description

@ice1000

Description

There are times when I want to write some text above an arrow like this

image

Currently I'm using typst-cd with

#align(center)[
  #commutative_diagram(
    node((0, 0), [$$]),
    node((0, 2), [$$]),
    arr((0,0), (0,2), [injective map], "inj", label_pos: -1em)
  )
]

But it would be nice if there are native support for:

  1. long, hooked arrows
  2. putting text above an arrow (instead of just braces or brackets like we have now)

Use Case

As above. This can be used to describe injective maps.

Activity

  1. added
    feature request
    on May 23, 2023
  2. Enivex commented on May 24, 2023

    @Enivex
    Collaborator

    I don't know how typst-cd does it, but here's a typst solution that uses the symbol in your actual font

    (this assumes that the arrow heads do not occupy more than 40% of the width, so it may need modification if the font has particularly large arrow heads)

    #let long-symbol(sym, factor) = {
      assert(type(sym) == "symbol", message: "Input needs to be a symbol")
      assert(type(factor) == "integer" or type(factor) == "float", message: "Scale factor must be a number")
      assert(factor >= 1, message: "Scale factor must be >= 1")
      
      factor = 5*factor - 4
      let body = [#sym]
      style(styles => {
        let (body-w,body-h) = measure(body,styles).values()
        align(left)[
          #box(width: body-w*2/5,height: body-h,clip: true)[
            #align(left)[
              #body
            ]
          ]
          #h(0cm)
          #box(height: body-h, width: body-w*1/5*factor)[
            #scale(x: factor*100%,origin:left)[
              #box(height: body-h, width: body-w*1/5,clip:true)[
                #align(center)[
                  #body
                ]
              ]
            ]
          ]
          #h(0cm)
          #box(width: body-w*2/5,clip: true)[
            #align(right)[
              #body
            ]
          ]
        ]
      })
    }
    
    $
      f: X attach(limits(#long-symbol(sym.arrow.r.hook,8)), t: "injective map") Y\
      #long-symbol(sym.arrow.l.r.double.long,10) \
      #long-symbol(sym.arrow.l.filled,5)
    $

    image

  3. added
    visualize
    on May 25, 2023
  4. mwyschan commented on Jun 26, 2023

    @mwyschan

    I'd just like to add that this syntax is also useful for denoting chemical reactions, e.g. catalysis:

    In the case of reversible reactions using ⇌, I had to modify long-symbol to shift the symbol slightly upwards (body-h+2pt seems to work), since the right arrow was getting clipped:
    image

  5. apgrigor commented on Jul 12, 2023

    @apgrigor

    Hello! I ended up writing my own little function that places annotations on top of symbols, I based it on Enivex's solution.
    In case someone finds it useful:

    #let annotated-symbol(
        sym, annot, 
        breakpoints: 2/5, 
        breakpoint-padding: 0%,
        scale-mid: 1
    ) = {
        assert(
            type(sym) == "symbol", 
            message: "Input needs to be a symbol, is actually " + type(sym)
        )
        assert(
            type(annot) == "content", 
            message: "Annotation needs to be of type content, is actually " +
                type(annot)
        )
        assert(
            (type(breakpoints) == "integer" or type(breakpoints) == "float") 
            and breakpoints < 0.5,
            message: "breakpoints must be a number less than 1/2, is actually " +
                str(breakpoints) +
                ", of type " +
                type(breakpoints)
        )
        assert(
            type(breakpoint-padding) == "length" or 
            type(breakpoint-padding) == "relative length" or
            type(breakpoint-padding) == "ratio",
            message: "breakpoint-padding must be a length, relative length or ratio, is actually " +
                type(breakpoint-padding)
        )
        assert(
            type(scale-mid) == "integer" or type(scale-mid) == "float",
            message: "scale-mid must be a number, is actually " + 
                type(scale-mid)
        )
        
        let sym-content = [#sym]
    
        style(styles => {
            let s = measure(sym-content, styles)
            let a = measure(math.script(annot), styles)
            let out-segment-length = s.width * breakpoints
            let mid-segment-length = s.width * (1 - 2 * breakpoints)
            let factor = a.width / mid-segment-length * scale-mid
    
            let sym-back = box(
                width: out-segment-length, 
                height: s.height, 
                clip: true,
                place(left, 
                    sym-content
                )
            )
    
            let sym-middle = box(
                width: mid-segment-length,
                height: s.height,
                clip:true,
                place(left, 
                    dx: -out-segment-length,
                    sym-content
                )
            )
    
            let sym-front = box(
                width: out-segment-length, 
                height: s.height, 
                clip: true,
                place(left, 
                    dx: -(out-segment-length + mid-segment-length),
                    sym-content
                )
            )
    
            let scaled-sym = box(
                width: 2 * out-segment-length + factor * mid-segment-length,
                height: s.height
            )[
                #place(left + top,
                    sym-back
                )
                #place(left + top,
                    dx: out-segment-length + breakpoint-padding,
                    scale(x: factor * 100%, origin: left, sym-middle)
                )
                #place(left + top,
                    dx: out-segment-length + factor * mid-segment-length + 2 * breakpoint-padding,
                    sym-front
                )
            ]
    
            math.attach(
                math.limits(scaled-sym),
                t: annot
            )
        })
    }
    
    $
        f: X #annotated-symbol(sym.arrow.r.hook, [injective map], scale-mid: 1.3) Y
    $

    sym_test

  6. ice1000 commented on Jul 12, 2023

    @ice1000
    SponsorAuthor

    Nice! Stolen

  7. Enivex commented on Jul 12, 2023

    @Enivex
    Collaborator

    Note that there's now a package (xarrow) that does this, by @loutr, using a more flexible variant of what I did in my comment

    https://typst.app/docs/packages/

  8. DieracDelta commented on Sep 14, 2023

    @DieracDelta

    This xarrow package gets confused with fractions. For example, something like:

    #import "@preview/xarrow:0.1.1": xarrow
    $ "some proof thing" / (xarrow("this is a very long thing")) $
    

    Though, maybe I'm missing something?

  9. laurmaedje commented on Sep 18, 2023

    @laurmaedje
    Member

    Would it be desirable for the default arrow to scale automatically when you write $ ->^"something long" $? Or are there cases where you don't want that?

  10. Enivex commented on Sep 18, 2023

    @Enivex
    Collaborator

    Would it be desirable for the default arrow to scale automatically when you write $ ->^"something long" $? Or are there cases where you don't want that?

    That would be desirable, but it would have to be quite robust. I imagine the approach in xarrow might break for certain fonts with weird arrows

  11. laurmaedje commented on Sep 18, 2023

    @laurmaedje
    Member

    @Enivex New CM arrows support the OpenType glyph construction feature. It would work just the same as $ arrow("very long text") $.

  12. added
    good contribution
    on Sep 18, 2023
  13. laurmaedje commented on Sep 18, 2023

    @laurmaedje
    Member

    So basically, implementing this would mean stretching the base in AttachElem::layout_math according to the max width of top and bottom attachment (if they are limits). I'm not sure if there are any unforeseen consequences on other attachment combinations though.

  14. changed the title [-]How to draw long hooked arrows?[/-] [+]Make arrows in math stretchable[/+] on Nov 14, 2023
  15. added
    math
    and removed
    visualize
    on Nov 14, 2023
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

    feature requestgood contributionmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions