Skip to content

Can't use over-/underline() with any space symbol or h() #1716

Open

Description

@Andrew15-5

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 #1716 (comment) #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

Activity

  1. added
    bug
    on Jul 13, 2023
  2. Andrew15-5 commented on Jul 13, 2023

    @Andrew15-5
    ContributorAuthor

    My current workaround is:

    #let H(n) = {
      for value in range(n) {
        sym.space.hair
      }
      // I think I can write endless number of '0's, but the number must be more than '0.0'
      h(0.000000000000000000000000000000001pt)
    }
    
    #box[some text #underline[#H(1000)]] // Add n spaces to fill the whole line

    I don't know why having h() with any non-zero length causes the spaces before it to show underlining, but without h() nothing is shown. And AFAIK, right now there are no getters, so I can't calculate the precise number of spaces.

    P.S. I should try and use table, I think I can simplify this.

  3. adriandelgado commented on Jul 14, 2023

    @adriandelgado
    Contributor

    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.

  4. Andrew15-5 commented on Jul 14, 2023

    @Andrew15-5
    ContributorAuthor
    // #set box(outset: (bottom: 1.29pt))
    
    Text or no text #underline[test]#box(width: 1fr, stroke: (bottom: 0.5pt))
    
    Text or no text #underline[test]#box(width: 1fr, line(length: 100%, stroke: 0.5pt))

    image

    This solution is much better than mine, but it still tries to mimic the "underline until EOL". Typst was created to be more intuitive than LaTeX, you can create documents faster. And the fact that underline() ignores/skips any whitespace is just wrong, why is this the case?

    Also, there can be default or custom settings for underline() and by using stroke of box() or line() you have to replicate/copy the settings of the underline() which is unnatural and just bad UX and bad practice, IMO.

    One more thing. The underline stroke thickness is determined by the font? W-why is that? In the example above, the fake stroke is a bit thicker than the real one:

    image

    This is a different topic, but this makes it more difficult to use above workarounds. Sure, you can set underline thickness to 0.5pt in every single document, where you want to use underlining (or use template), but why not use some default value in the first place? Why thickness must be related to font (itself or boldness, I don't remember)?

  5. Andrew15-5 commented on Jul 14, 2023

    @Andrew15-5
    ContributorAuthor

    I've come up with the next setup:

    #let underline_thickness = 0.5pt
    #set underline(stroke: underline_thickness)
    #let ubox(..) = box(
      width: 1fr,
      stroke: (bottom: underline_thickness),
      outset: (bottom: 1.3pt), // Font dependant
      ..
    )
    
    Text or no text #underline[test]#ubox()

    image

  6. added
    text
    on Jul 17, 2023
  7. Andrew15-5 commented on Aug 24, 2023

    @Andrew15-5
    ContributorAuthor

    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.

  8. Andrew15-5 commented on Nov 22, 2023

    @Andrew15-5
    ContributorAuthor

    I forgot to mention that this is the one most heavily used thing in blanks/forms making. Whenever I need to make a form with/without filling in information/details, I have to import a whole bag of hacky underline functions that otherwise would be much simpler/slimmer if h() were to work in underline().

  9. OrangeX4 commented on Dec 14, 2023

    @OrangeX4

    And the current underline does not support mixed Chinese-English typography, there are breaks here.

    dbc23453e8b798889cc6dc4c11b49967

  10. Myriad-Dreamin commented on Feb 27, 2024

    @Myriad-Dreamin
    Contributor

    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

  11. changed the title [-]Can't use over-/underline() with any space symbol or h()[/-] [+]Can't use `over-/underline()` with any space symbol or `h()`[/+] on Jul 16, 2024
  12. kanashimia commented on Oct 24, 2024

    @kanashimia

    As of Typst 0.12.0 underline(repeat(" ")) now works, kinda.

    You still can't use h because it isn't counted as text and isn't underlined, for example underline[foo #h(1cm) bar] will produce something like

    foo    bar

    This is better than before:

    #let fill(width) = box(width: width, repeat(sym.space))
    
    #underline[#fill(1fr) hello #fill(1fr)]\

    For some reason not all space symbols work though, and what worse is that which symbols work seems to depend on the specific font used.

  13. Andrew15-5 commented on Oct 24, 2024

    @Andrew15-5
    ContributorAuthor

    As of Typst 0.12.0 underline(repeat(" ")) now works, kinda.

    Interesting.

    This is better than before:

    Indeed.

    For some reason not all space symbols work though, and what worse is that which symbols work seems to depend on the specific font used.

    Oh...no...


    I'm still using the same ubox() hack, but with different wrapper functions on top of it to be useful in different cases (in most cases you also want to use underline instead to use the evade feature). A solid workaround.

  14. Andrew15-5 commented on Nov 10, 2024

    @Andrew15-5
    ContributorAuthor

    This is better than before:

    #let fill(width) = box(width: width, repeat(sym.space))
    
    #underline[#fill(1fr) hello #fill(1fr)]\

    This is better than not working at all, but you shouldn't use it, as I found out (accidentally) that it has some repeat() downsides.

    image

    As you can see, in the best case scenario, the gap between spaces is barely noticeable, but in the worst: you either see the gap or justify: false will make the EOF shorter. I didn't test this with other spaces, but they all probably have the tiny gaps anyway. So I'm back to using the old hack.


    BTW, previously I used ubox[~] in order to keep the line height correct, but a bunch of these will add visible spaces. Instead, the zero-width space should be used, i.e., ubox(sym.zws).

  15. mattfbacon commented on Jun 6, 2025

    @mattfbacon
    Contributor
    lorem #underline[ipsum #box(width: 1fr, repeat(" "))]

    "lorem" then "ipsum" underlined with the underline extending to the end of the page

  16. Andrew15-5 commented on Jun 6, 2025

    @Andrew15-5
    ContributorAuthor

    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 #1716 (comment) 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

  17. neruthes commented on Jul 11, 2025

    @neruthes

    After studying the comments above, I now have a #hspace function in my latexcompat package. Thanks all.

    #let hspace(width) = context {
      box(width: width, repeat(sym.space.nobreak))
    }
  18. Andrew15-5 commented on Jul 15, 2025

    @Andrew15-5
    ContributorAuthor
    #let hspace(width) = context {
      box(width: width, repeat(sym.space.nobreak))
    }

    Why do you have context? It will slow down compilation with each use.

  19. neruthes commented on Jul 15, 2025

    @neruthes
    #let hspace(width) = context {
      box(width: width, repeat(sym.space.nobreak))
    }

    Why do you have context? It will slow down compilation with each use.

    Just to be absolutely sure it is safe enough. Being a new user or Typst, I am not exactly informed about how context works, and I observed situations where it was required. Coming from LaTeX means that I am super tolerant with compilation time. I will be happy to remove it when I fully understand context.

  20. Andrew15-5 commented on Jul 15, 2025

    @Andrew15-5
    ContributorAuthor

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

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

  21. Kogfill commented on Dec 3, 2025

    @Kogfill

    I recently bumped into a similar issue, essentially needing working #underline(h(<width>)), and all solutions above gave some artifacts(the underline had gaps) or layout issues(I'm recreating a page from Microsoft Word, hence the need for wacky text layout). Here's the workaround that worked for me:

    #let underlined_h(width) = {
      underline(text(spacing: width, " "))
    }
  22. phidiascheng commented on Sep 24, 2026

    @phidiascheng

    I just guess the problem is that the underline function does not work on #h() element, thus the solution might be to find some chars to take space in the text flow. But unfortunately continuous multiple spaces are not allowed in typst and are treated as only a single space.
    Today I happen to find that the space symbol \u{0020} is allowed to be repeated continuously in typst.
    So for anyone who wants a simpler solution, the following works:

    #let addspace(number) = for i in range(number){[\u{0020}]}
    #underline(stroke: 2pt, offset: 0.4em)[#addspace(5)have a #addspace(20) try#addspace(5)]
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

    bugtext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions