10

I know that by using a package like xcolor I can use $\color{<color>} <math symbols>$ to typeset math symbols in my preferred color. But how can I isolate the color to specific symbols only?

Say for instance the illustrations of commented equations in Howard Anton's Calculus book have colors for underbraces and the bounding text boxes but have none for the included text.

Consider the following MWE

\documentclass[]{article}

\usepackage{amsmath}
\usepackage{xcolor}

\begin{document}

\begin{equation}
\dfrac{d}{dx}[\sin(3x^2+2)]=\underbrace{\cos(3x^2+2)}_{\text{
\fbox{\parbox[b][]{2cm}{
Derivative of the outise evaluated at the inside}
}}}
\cdot 
\underbrace{6x}_{\text{
\color{blue}{\fbox{\parbox[b][]{1.25cm}{
Derivative of the inside}
}}}}
\end{equation}
\end{document}

which outputs

enter image description here

How can I isolate the coloring to the underbraces and the bounding box to blue without affecting the other symbols/text?

azetina
  • 28,884
hpesoj626
  • 17,282

2 Answers2

9
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\makeatletter
\renewcommand\underbrace[2][olive]{%
\mathop{\vtop{\m@th\ialign{##\crcr
$\hfil\displaystyle{#2}\hfil$\crcr
\noalign{\kern3\p@\nointerlineskip}%
\textcolor{#1}{\upbracefill}\crcr\noalign{\kern3\p@}}}}\limits}
\makeatother
\newcommand\ColorBox[3][olive]{\text{\fcolorbox{#1}{white}{\parbox[b][]{#2}{\raggedright#3}}}}

\begin{document}

\begin{equation}
\frac{d}{dx}[\sin(3x^2+2)]=\underbrace{\cos(3x^2+2)}_{\ColorBox{2cm}{Derivative of the outise evaluated at the inside}}
\cdot 
\underbrace[red!60!black]{6x}_{\ColorBox[red!60!black]{1.25cm}{Derivative of the inside}}
\end{equation}

\end{document}

enter image description here

The syntax:

\underbrace[<color>]{<text>}
\ColorBox[<color>]{<width>}{<text>}

The default color: olive.

Gonzalo Medina
  • 505,128
4

Here are some minor alternatives to Gonzalo's answer, provided by the abraces package. More specifically, it allows for inserting arbitrary code within the brace construction using @{<stuff>}:

enter image description here

\documentclass{article}
\usepackage[overload]{abraces}% http://ctan.org/pkg/abraces
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\begin{equation}
  \dfrac{\mathrm{d}}{\mathrm{d}x}\big[\sin(3x^2+2)\big]=
    \underbrace[@{\color{olive}}l1D1r]{\cos(3x^2+2)}_{\color{olive}
      \text{\fbox{\parbox[b]{2cm}{\raggedright%
        \color{black}Derivative of the outside evaluated at the inside}
  }}}
  \cdot
  \underbrace[@{\color{red!60!black}}l1D1r]{\vphantom{()}6x}_{\color{red!60!black}
    \text{\fbox{\parbox[b]{1.25cm}{\raggedright%
      \color{black}Derivative of the inside}
  }}}
\end{equation}
\end{document}

Other modifications include:

  • Using \mathrm{d} for d/dx;
  • Enlarging the brackets around the LHS using \big[ and \big]; and
  • Inserting \vphantom to lower the \underbrace for both components of the chain rule to the same depth.
Werner
  • 603,163