[Tex/LaTex] Highlight terms in equation mode

equationsmath-mode

I am trying to add a green box around the first two terms in my equation (beta_0 and b_i). I also want to add a circle around the second term (b_i). Is it possible to do this in LaTeX? My code is below which produces the equation. Any help would be great.

\begin{equation}

    \mathbf{Y_{ij}} = \beta_{0} +\mathbf{ b_i} + \beta_1x_1 + \ldots + \beta_nx_n + \mathbf{\epsilon_{ij}}

\end{equation}

Best Answer

For complex drawings (circles, etc), you might need to go the tikz road.

But for simple shading of elements, a regular \colorbox should do, i.e.

\documentclass{article}

\usepackage{xcolor}

\newcommand{\highlight}[1]{%
  \colorbox{red!50}{$\displaystyle#1$}}

\begin{document}
\begin{equation}
    \mathbf{Y_{ij}} = \highlight{\beta_{0}} +\mathbf{ b_i} + \beta_1x_1 + \ldots + \beta_nx_n + \mathbf{\epsilon_{ij}}
\end{equation}
\end{document}

which yields

highlight result

In case you want to do more than just highlight terms but are looking to explain the terms, I once wrote the following code for my master's thesis presentation:

\documentclass{article}

\usepackage{xcolor}

\newlength{\overwritelength}
\newlength{\minimumoverwritelength}
\setlength{\minimumoverwritelength}{1cm}
\newcommand{\overwrite}[3][red]{%
  \settowidth{\overwritelength}{$#2$}%
  \ifdim\overwritelength<\minimumoverwritelength%
    \setlength{\overwritelength}{\minimumoverwritelength}\fi%
  \stackrel
    {%
      \begin{minipage}{\overwritelength}%
        \color{#1}\centering\small #3\\%
        \rule{1pt}{9pt}%
      \end{minipage}}
    {\colorbox{#1!50}{\color{black}$\displaystyle#2$}}}

\begin{document}
\begin{equation}
    \mathbf{Y_{ij}} = \overwrite{\beta_{0}}{very important!} +\mathbf{b_i} + \beta_1x_1 + \ldots + \beta_nx_n + \mathbf{\epsilon_{ij}}
\end{equation}
\end{document}

which this time yields:

overwrite result

You can even easily extend that code to fade the color from slide to slide and highlight one term at a time if you're using beamer (just add a \temporal<+> before the \stackrel, duplicate the \stackrel code three times and change the colors in each variant).