Skip to content

Text decoration functions not working with inline equations #2200

Open

Description

@cprieto

Description

Highlight is not highlighting equations but it works with text.

Example:

This is a #highlight[highlight sample], it works.

This is an equation: #highlight[$a_n = C_0 a_(n-1)$], it doesn't

Produces:
image

Reproduction URL

https://typst.app/project/ww-AG8xGryot5E-agddCDs

Operating system

Web app

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Sep 21, 2023
  2. added
    math
    on Sep 21, 2023
  3. 8LWXpg commented on Sep 23, 2023

    @8LWXpg
    Contributor

    similar issue also happens to strike and text(baseline: length)

    $
    #highlight[x $x$ xx]\
    #strike[x $x$ xx]\
    #text(baseline: -5pt)[x $x$ xx]\
    $
    
    #highlight[x $x$ xx]\
    #strike[x $x$ xx]\
    #text(baseline: -5pt)[x $x$ xx]\
    image
  4. LuxxxLucy commented on Sep 30, 2023

    @LuxxxLucy
    Contributor

    It seems that for now only TextElem would apply the line decorations (highlight, strikethrough, etc). It shoud not be difficult to support math, I will work on it.

    The only problem I have is that the metrics provided as arguments to the line decoration, such as baseline, ascender, may only make sense to a TextElem, but does not really make sense for a math expression. For example, if we have a fraction surrounded by some normal text,

    #highlight[We have $inline(1/2 < (x+1)/2)$ here]

    image

    how should we decide the height of the highlight rectangle for the inline math? As you can see, the math expr can easily go beyond the height of normal text, and the normal font metrics like descender/ascender does not make sense anymore. Should we simply place a highlight rectangle that covers the entirety of the math expression? Maybe we need to first define what those decorations would work on the math expr.

  5. davidleejy commented on Nov 20, 2023

    @davidleejy

    Not sure if this quick and dirty workaround could help any Typst users out there:

    If you find short character sequences in math equations not getting highlighted while longer ones do, for example:

    If $ a + b = #highlight[c] $ does not result in the c symbol being highlighted, and lengthening the argument of the highlight command by adding more characters: $ a + b = #highlight[ccc] $ successfully highlights ccc.

    Then you may like to consider adding non-space characters without glyphs to artificially lengthen the sequence passed to the highlight command. I tried adding U+034F "COMBINING GRAPHEME JOINER" characters to lengthen the sequence, and it works. 👍🏻

    Adding U+034F to the Typst code view does not result in visible differences since this character has no glyph and is thus not rendered -- the code would still read like $ a + b = #highlight[c] $ after U+034F characters are appended to c, and the rendered equation will possess the desired highlight spanning the symbol c.

    Using characters that are widely treated as a space doesn't appear to work.

    There exists several non-space characters without glyphs, and I'm not sure which one is the "best" character to use in this circumstance if one or more of these are indeed better than the rest. https://invisible-characters.com/ contains a list of invisible characters, some of which are treated as spaces (which aren't suitable for this workaround) and others that are non-space characters (suitable for this workaround).

  6. SolidTux commented on Apr 5, 2024

    @SolidTux

    Is there some update on this? Or some place I could try to help with?

  7. laurmaedje commented on Apr 6, 2024

    @laurmaedje
    Member

    As @LuxxxLucy pointed out, we first need to define how highlight should behave in the first place. I think the most reasonable thing would be to highlight the whole equation bounding box. If we agree on that, the fix should be relatively simple by checking for the presence of the highlighting style when doing the inline equation layout.

  8. LuxxxLucy commented on Apr 12, 2024

    @LuxxxLucy
    Contributor

    Sorry for not being active. Will work a solution and update soon. I think I am going with the bounding box approach.

  9. LuxxxLucy commented on Apr 14, 2024

    @LuxxxLucy
    Contributor

    Had some initial try. I find that we can do this in layout_inline for the equation, we check the decorations styles and then do line-decoration (such as highlight), Result would be something like this
    image

    However, I found that there are some tricky case we have multiple MathParItem (with potential space) in the equation, it seems that it would need some extra effort to check the x and y coordinates. After I figure it out I will submit a PR (or perhaps two PRs, one for fixing the highlight for equations, and then a second PR that does some refactoring and add support for the other line-decorations)

  10. changed the title [-]highlight function not working with equations[/-] [+]Text decoration functions not working with inline equations[/+] on Jul 16, 2024
  11. DzmingLi commented on Jul 27, 2024

    @DzmingLi
    Contributor

    hope that highlight etc.... function is available in math mode

  12. Enivex commented on Jul 27, 2024

    @Enivex
    Collaborator

    hope that highlight etc.... function is available in math mode

    It will eventually, but there are some things blocking it

  13. conema commented on Feb 19, 2025

    @conema

    A quick workaround working with simple inline math:

      #let hl(content) = box(
        content,
        inset: 0.2em,
        fill: rgb("#fffe69"),
      ) 
    

    Image

  14. FnControlOption commented on May 20, 2025

    @FnControlOption

    Here are workarounds for underline and strikethrough with inline math:


    Underline:

    #let ul(content) = box(
      content,
      outset: (bottom: .1em),
      stroke: (bottom: .5pt)
    )
    
    Image

    Strikethrough:

    #let st(content) = context {
      let width = measure(content).width
      box[
        #content
        #place(
          dy: -3pt,
          line(stroke: .5pt, length: width)
        )
      ]
    }
    
    Image
  15. tedzards509 commented on Aug 21, 2025

    @tedzards509

    A quick workaround working with simple inline math:

      #let hl(content) = box(
        content,
        inset: 0.2em,
        fill: rgb("#fffe69"),
      ) 
    

    A problem I found with this was that box doesn't break. Thanks to catte_ from the Discord, I got the following workaround using show rules to highlight an equation inside a highlight with the highlight color:

    #show highlight: it => {
      show math.equation: box.with(fill: it.fill)
      it
    }
  16. mariansam commented on Feb 26, 2026

    @mariansam

    I merged these two approaches to make the underline work:

    #let ul(content) = [
      #show math.equation: content => box(
        content,
        outset: (bottom: .078em),
        stroke: (bottom: .44pt)
      )
      #underline[#content]
    ]
    
    This is #ul[underlined $and$ with math, complicated $sum_(k=0)^n binom(a, b)$ is not good]
    Image
  17. marked easier way to strike through #8279 as a duplicate of this issue on May 13, 2026
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

    bugmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions