[Tex/LaTex] Color underline a formula

colormath-mode

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.

Best Answer

\documentclass{article}
\usepackage{xcolor}

\newsavebox\MBox
\newcommand\Cline[2][red]{{\sbox\MBox{$#2$}%
  \rlap{\usebox\MBox}\color{#1}\rule[-1.2\dp\MBox]{\wd\MBox}{0.5pt}}}
\begin{document}

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

\end{document}

alt text