How to nest #text and #highlight while using gradient?

Hi, I’m trying to highlight some formatted text that contains math in my document with some fun colours. I did the following:

#text(fill: olive)[
  Implication: when a function has a left and right inverse, the two inverses are equal. To prove this statement in the general case (without assuming $f, g, h: RR -> RR$), suppose $f: A -> B, g, h: B -> A$. Then $ h compose f compose g \ (h compose f) compose g = g \ h compose (f compose g) = h $ 
  
  Here $g = h = f^(-1)$. #highlight(gradient.linear(..color.map.rainbow))[Caution: $f compose f^(-1): B->B, f^(-1) compose f: A->A$]
]

The compiler is giving me an error:

Error: Expected content, found gradient
How to fix this?

  • Replace the gradient.linear(…color.map.rainbow) with content.
  • Did you store content in a variable? Insert the variable’s name.

I’m a little confused why this is the case, because I took this syntax from an example in the documentation on highlight and gradient:

This is #highlight(
  fill: blue
)[highlighted with blue].

As a fill to paint the interior of a shape: rect(fill: gradient.linear(..))

If things were working as I expected, everything in the red rectangle should have rainbow highlight:

How do I fix this?

When you call highlight(gradient.linear(...)), the argument is interpreted as the body because it is the only positional argument of the highlight function. This what the error is trying to tell you. Use the named argument highlight(fill: gradient.linear(...)) instead to apply the gradient as the fill of the highlight element.

One additional comment regarding highlight and equations. As you can already see in your second image, the highlight only covers the text. If you want the gradient to extend to the end of the equation, you can wrap everything in a box and set the fill with the linear gradient. You need to play around with the vertical size a bit to make sure that the box has the same extent as the highlight.

1 Like

Thank you @janekfleper, this worked (and also made me realize that the rainbow highlight has terrible contrast with the olive font colour :melting_face: :melting_face:)

In general, does #highlight not work with math? Is the #box solution the most idiomatic solution if I want to highlight math in typst?

I honestly thought both the text color and the gradient color were supposed to be a joke and you never actually wanted to use either of them. :sweat_smile:

Text decorations (highlight, underline etc.) generally do not work in math mode. There is a very old issue on this Text decoration functions not working with inline equations · Issue #2200 · typst/typst · GitHub where some possible solutions are discussed. Using a box with a small inset is also suggested as a replacement for highlight in equations.

Another option would be to use the package mannot – Typst Universe which uses a manually placed Rectangle - Typst Documentation to realize the function markhl(). Note that the function can only be used from within an equation, which requires nested equations if you want the highlight to cover both text and math.

#import "@preview/mannot:0.4.0": markhl

Here $g = h = f^(-1)$. $#markhl(fill: gradient.linear(..color.map.rainbow))[Caution: $f compose f^(-1): B->B, f^(-1) compose f: A->A$]$
1 Like