How to add colored underline in the math mode?

Hello, I want to underline parts of math equation with colorful strokes but sadly function math.underline only accepts body as a parameter. Is there a way to do it or a package implementing it?

#show math.underline: set text(fill: red)
1 Like

Thanks, this has particially helped me.
How can I filter different math content for underline? Cause I want to have underlines of different color in one equation.

That is what I’ve tried to do:

#show math.underline.where([$2 A_2$]): set text(fill: blue)
$ underline(2 A_2) + A_2 x^2 + A_1 x + A_0 = x^2 - 1 $
$ cases(A_2 = 1\ A_1 = 0\ 2 A_2 + A_0 = -1) <=> cases(A_2 = 1\ A_1 = 0\ A_0 = -3) $

But I can’t understand how to manage with “where” if the function(“math.underline”) has only one unnamed argument “content” and it has kind of mathematical type.

I wanted to do something similar in the past but gave up in the end. The code splits the equation, which causes misalignments. Perhaps you can find a way to make it work.

#let colorunderline(color: black, equation) = block(
  stroke: (bottom: .5pt + color), 
  outset: (bottom: 1.5pt), 
  $ equation $
)

$
  colorunderline(a =) colorunderline(color: #red, a +) colorunderline(color: #green, b / c ) \
  
  colorunderline(color: #orange, a =) colorunderline(color: #gradient.linear(..color.map.rainbow), a + b / c) \
  
  sum_i x_i/2 = colorunderline(sum_i x_i )\
$
Output

1 Like

In that case, you might define some custom underline functions, e.g.

#let myuline(fill: black, it) = {
  set text(fill: fill)
  math.underline(it)
}

Then you can have

$ myuline(fill: #blue, A + B) = myuline(fill: #red, C + D) $

Result

2 Likes