7

I need to change the color of only underbrace in the following post.

horizontal curly braces.

MWE from above link is:

\overbrace{your-formula}^\text{your comment} 
\underbrace{your-formula}_\text{your comment} 

An example taken from Wikibooks:

\[
  z = \overbrace{
    \underbrace{x}_\text{real} +
    \underbrace{iy}_\text{imaginary}
   }^\text{complex number}
 \]

Their result: enter image description here

NAASI
  • 2,809

1 Answers1

8

Package color helps. The range of the color is limited by TeX groups. Curly braces have a side effect of creating a sub formula in math mode, which affects the horizontal spacing, therefore \begingroup and \endgroup is used without that side effect:

\documentclass{article}
\usepackage{color}
\usepackage{amsmath}

\begin{document}
\[
    z = \overbrace{
    \begingroup
      \color{red}
      \underbrace{\color{black}x}_\text{\color{black}real}
    \endgroup
    +
    \begingroup
      \color{blue}
      \underbrace{\color{black}iy}_\text{\color{black}imaginary}
    \endgroup
   }^\text{complex number}
\]
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Wouldn't \textcolor instead of \color as in \[ z = \overbrace{ \textcolor{red}{\underbrace{\color{black}x}_\text{\color{black}real}} + \textcolor{blue}{\underbrace{\color{black}iy}_\text{\color{black}imaginary}} }^\text{complex number} \] be simpler? – Gonzalo Medina Aug 14 '15 at 00:31
  • 1
    @GonzaloMedina \textcolor uses curly braces and therefore it becomes a subformula. Therefore it only can be used to color ordinary math atoms. In this example it would work, but it will not, when coloring the binary plus sign or the equals sign. – Heiko Oberdiek Aug 14 '15 at 04:22
  • For small items, \underbracket (from mathtools) would look nicer than \underbrace, in my opinion. – Bernard Aug 14 '15 at 08:25