I want to underline different parts of a formula with different colors. What is the best possible option? Soul seems only to provide one color per document.

5 Answers 5



\[ f(x)=\int_1^\infty\Cline{x^2+\frac12\ln(x)}\mathrm{d}x
       = \Cline[blue]{\int_1^\infty x^2} \mathrm{d}x 
       + \Cline[cyan]{\int_1^\infty\frac12\ln(x)} \mathrm{d}x \]


alt text


This is a slightly different method than that provided by Herbert and Alan, using TeX's own little known command \underline.



\[ f(x)=\int_1^\infty\mathunderline{red}{x^2+\frac12\ln(x)}\mathrm{d}x
       = \mathunderline{blue}{\int_1^\infty x^2} \mathrm{d}x 
       + \mathunderline{green}{\int_1^\infty\frac12\ln(x)} \mathrm{d}x \]

  • Thanks, this seems to work much better for nested underlining than Herbert’s box solution (haven’t tried alan’s soul solution). Nov 27, 2016 at 17:10

You can easily create a multicoloured version of the \ul command using the soul package.


%This command takes a colour as an optional argument; the default colour is black.


     \myul[red]{Red underline}

     \myul[blue]{blue underline}

     \myul[green]{green underline}

     \myul{black underline}

It appears this solution doesn't work for underlining in math mode, so Herbert's solution is clearly best. For regular text, this solution will work as advertised.


In ConTeXt, one can use \mframed for underlining text. \mframed is a version of \framed that takes care of math scaling. \mframed takes all the options of \framed, so getting an underline is as simple as setting frame=off and bottomframe=on. You can choose the color of a frame by setting framecolor=<name of color>. Combining all this, here is a macro that changes the color of the underline:



This can be used as follows


  $f(x) = \highlight{\int_1^{\infty} x^2 + \frac {1}{2} \ln(x) dx}$. This can be
  simplified as
  f(x) = \highlight{\int_1^{\infty} x^2 + \frac {1}{2} \ln(x) dx}
       = \highlight[framecolor=blue] { \int_1^{\infty} x^2} dx 
       + \highlight[framecolor=blue] { \int_1^{\infty} \frac {1}{2} \ln(x)} dx

which gives the following result. alt text

Notice that the \int is scaled correctly, both for inline math as well as for display math.


I had difficulty using any of the above methods in beamer, but found that simply changing the color, declaring the underline, and then changing the color back inside the underline command seemed to work. For example:


gives me the new command \rul{sample text} which underlines the black sample text in red. I suspect the above methods are more sophisticated, but this seems to work fine for me, and does not require the soul package.


You must log in to answer this question.