How can I keep kerning over a zero-width box?

I want to make chord markings above the text. I use a box with a place function to do that like in the example below.

#let chord(c) = [#sym.wj#box(place(bottom + left, dy: -1em, c))#sym.wj]

Avail A#chord[G]vail

The problem is, as can be seen in the image, that the chord marking cancels kerning of letters around it. With the marking on the second syllable of ‘Avail’, the ‘A’ is farther from the ‘v’ than how it is supposed to be. How can I make the box not distrupt the kerning? A different approach would be to align the marking with a specific point in the text without inserting anything into the text.

I would have a look at packages such as Search — Typst: Universe for some hints on how to do it.

One implementation in particular: typst-chords/src/single.typ at main · ljgago/typst-chords · GitHub

1 Like

chordx solves that by measuring widths of letters from the beginning of the word. It has a bigger issue because it ignores kerning completely (as described in issue #15), but it gave me the idea, and I implemented it so:

#let myChord(position, chord, base) = context {
  let offset = measure([#base.text.slice(0, position - 1)]).width
  [#box(place(bottom+left, dy: -1em, dx: offset, chord))#base]
}

#myChord(2)[G][Avail]