23

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.

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

17

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}
  • Thanks, this seems to work much better for nested underlining than Herbert’s box solution (haven’t tried alan’s soul solution). – Lucas Werkmeister Nov 27 '16 at 17:10
10

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. – Steven C. Howell Nov 13 '15 at 3:00
3

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

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

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy