# Color underline a formula

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.

-

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


-

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

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}

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

\newcommand{\myul}[2][black]{\setulcolor{#1}\ul{#2}\setulcolor{black}}

\begin{document}
\myul[red]{Red underline}

\myul[blue]{blue underline}

\myul[green]{green underline}

\myul{black underline}
\end{document}


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.

-
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.

\documentclass{article}
\usepackage{xcolor}

\def\mathunderline#1#2{\color{#1}\underline{{\color{black}#2}}\color{black}}
\begin{document}

$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$

\end{document}

-

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:

\def\highlight
{\dosingleargument\dohighlight}

\def\dohighlight[#1]%
{\mframed[frame=off,bottomframe=on,framecolor=red,frameoffset=3pt,#1]}


This can be used as follows

\setupcolors[state=start]

\starttext
Consider
$f(x) = \highlight{\int_1^{\infty} x^2 + \frac {1}{2} \ln(x) dx}$. This can be
simplified as
\startformula
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
\stopformula
\stoptext


which gives the following result.

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:

\newcommand{\rul}[1]{\textcolor{red}{\underline{\textcolor{black}{#1}}}}


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.

-
Welcome to TeX.SX! – egreg Jan 13 '14 at 18:32