How do I underline URLs with display texts containing more than one character?

I use following code to automatically underline all my URLs:

#show link: underline

How can I prevent a single link like

#link(<K-1>)[1]

from being underlined?

Hi @Kapati

I’ve renamed your title to keep it consistent with the forum guidelines for questions. For the future, pose your questions like how you would ask a friend.

Although I don’t have access to my computer (am traveling now and don’t have anything better to do than answer some questions on this forum), I hope I can help you in some way.

An option would be to check the length of the display text by converting it somehow to a string and retrieving the length. If it’s length 1, then display the link without underline (through #show) and else just display it as it is.

#show link: it => {
  // check for length of the display text and change the display accordingly (#show link: set underline(none) or something, I don't know the syntax from memory...

  if length == 1 {
    it
  } else {
    underline(it)
  }
}

length would be assigned somewhere before the if statement by you ;)

NOTE changed pseudo code to reflect Typst default of not underlining URLs

If the question is, for any particular link, how do I “cancel” that general show rule? It’s not always so easy, but for this underline rule it should be fine. Like this:

#show link: underline
Link target<K-1>

#link(<K-1>)[1]

#let nounderline(body) = {
  show underline: it => it.body
  body
}
#nounderline(link(<K-1>)[1])

So you can wrap anywhere you want to disable underline in your nounderline function.

#import "@preview/t4t:0.4.2": get

#show link: it => {
  let text = get.text(it.body)
  if text != none and text.clusters().len() == 1 { return it }
  underline(it)
}

#link("link")[#text(green)[1]]

#link("link")[12]

image

2 Likes

I adjusted the title to better reflect the Typst default of not underlining URLs.

@Andrew got a good solution on how to solve this. As by default the URLs are not underlined, we simply look for the correct condition (length > 2 characters) and apply the underline under said condition.

1 Like

Thank you all! I think I found a good solution with bluss answer.