TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 13 down vote accepted


\[ 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

share|improve this answer

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.

share|improve this answer
Unfortunately this does not work in beamer. – stvn66 Nov 13 '15 at 3:00

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 \]

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
Welcome to TeX.SX! – egreg Jan 13 '14 at 18:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.