typst: Can't use `over-/underline()` with any space symbol or `h()`

Description

I need to extend underline to the end of the line like this:

\documentclass{article}
\usepackage{ulem}
\begin{document}
Text or no text \uline{\hfill}
\end{document}

But neither #underline(h(100%)) (or 1fr) nor #underline(sym.space) is working. I didn’t find any of the settings that can replicate the same behavior, and #line() ignores any text + it is drawn automatically on the next line (not inline).

I consider this a bug, since it should work in theory, and it does work in LaTeX.

Update (v0.12.0)

Now #underline(sym.space) does work and as was pointed out in https://github.com/typst/typst/issues/1716#issuecomment-2435645252 #underline(box(width: 1fr, repeat(sym.space))) now also works. I tried pretty much all fonts that I use, and they all work. I think this should simplify some of my underline util functions.

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 7
  • Comments: 17 (13 by maintainers)

Most upvoted comments

I think this is a more universal version and all underlines (real/artificial) are on the same level (and have the same thickness).

#let underline-thickness = 0.5pt
#let underline-offset = 1pt
#set underline(stroke: underline-thickness, offset: underline-offset)
// Underline box
#let ubox(..box_args) = box(
  width: 1fr,
  stroke: (bottom: underline-thickness),
  outset: (bottom: underline-offset),
  ..box_args,
)

You can (re)define any box() arguments, including default width: 1fr (#ubox(width: 4cm)) and others.

Basically, if there is no error, and everything works correctly, don’t use context blindly.

https://typst.app/docs/reference/context

Workaround by @bluss from here:

#let fillend2(text) = context {
  underline(text)
  show: box.with(width: 1fr, clip: true, outset: (y: 1em))
  underline(extent: page.width)[#sym.space.nobreak]
}
This is some text. #fillend2[yoghurt *helloy*]

Image

I’ve changed https://github.com/typst/typst/issues/1716#issuecomment-1690842054 to something like this:

#let ubox(width: 1fr) = {
  let spacer = underline(extent: 10000cm)[#sym.space.nobreak]
  box(width: width, clip: true, outset: (y: 1em), spacer)
}

I tried, and it looks good for any length, so I guess it’s precise. The main advantage over the old one is that the lines not only look uniform and as continuous lines on any zoom level (in Okular/MuPDF), but also you don’t have to fiddle with default offset/thickness anymore. And evasion obviously works. I also already changed one of my templates to this and it looks better, I guess basically like it supposed to.

You can use it like #underline[ipsum]#ubox(width: ...), or make a wrapper like this:

#let culine(text, sides: 1fr, left: auto, right: auto) = {
  let (left, right) = (left, right)
  if left == auto { left = sides }
  if right == auto { right = sides }
  let spacer = underline(extent: 10000cm)[#sym.space.nobreak]
  let hack(width) = box(width: width, clip: true, outset: (y: 1em), spacer)
  hack(left)
  underline(text)
  hack(right)
}

Prefix #underline["poggies" emote]

Prefix #culine(sides: 0pt)["poggies" emote]

Prefix #culine["poggies" emote]

Prefix #culine(sides: 0.5em)["poggies" emote]

#culine(left: 0pt)["poggies" emote]

#culine(right: 0pt)["poggies" emote]

Image

Image

I have made a workaround helper today, hopefully useful for you:

#let long-underline(left: 0, right: 0, stroke: black, fake-text: "a ", content, ..args) = {
  let lspace(n) = {
    set text(fill: rgb("00000000"))
    underline(stroke: stroke, evade: false, fake-text * n, ..args)
  }
  if left > 0 {
    lspace(left)
  }
  underline(..args, content)
  if right > 0 {
    lspace(right)
  }
}

Usage:

image

Try using this:

Text or no text #box(width: 1fr, stroke: (bottom: 0.5pt), none)

or

Text or no text #box(width: 1fr, line(length: 100%, stroke: 0.5pt))

underline skips whitespace I think.