How to properly do hanging/suspended hyphens?

I have a typst document that contains the phrase “Zellkulturschalen und -flaschen”. But typst breaks the phrase right after the hyphen. However, i think the hyphen should “stick” to its word, so a linebreak should occur either before the hyphen or not at all. My current workaround is #box[-flaschen], but I really want to make sure this can’t occur anywhere in the document.

Hi laalsaas, welcome to the forum.

There is a unicode character that seems to be just for this situation, the NON-BREAKING HYPHEN.

This can be used in Typst with this syntax: \u{2011}. You could use it explicitly anywhere you need it:

Zellkulturschalen und \u{2011}flaschen

Or you could use a show rule.

#show "-": [\u{2011}]

Note that this affects all hyphens which might not be desirable.


Or a show rule with regex. This one matches any space character (\s) followed by a hyphen then any number of word characters (\w+). Then another show rule replaces the hyphen and leaves the rest of it alone.

#show regex(`\s-\w+`.text): it => {
  show "-": [\u{2011}]
  it
}

One point of failure of this is that styling is not captured by regex so #emph[-flaschen] and -#emph[flaschen] both fail.


Results:

Full Code
#set page(width: 50mm, margin: 3mm, height: auto)
*As described in OP*:
Zellkulturschalen und -flaschen

*Explicitly use unicode character*:
Zellkulturschalen und \u{2011}flaschen

*Show rule*:
#[
  #show "-": [\u{2011}]
  Zellkulturschalen und -flaschen
]

*Regex show rule*:
#[
  #show regex(`\s-\w+`.text): it => {
    show "-": [\u{2011}]
    it
  }
  Zellkulturschalen und -flaschen
]