35

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

2 Answers 2

51

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

3

Complementing Xavier answer, if you want to highlight using circles, ellipses or more exotic shapes, here is some tikz MWE.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{shapes}

\begin{document}
            $ \max_{\alpha_i\geq0}\sum_{1\leq i\leq n}\alpha_i-\frac12\sum_{1\leq i,j\leq n}\alpha_i\alpha_jy_iy_j
            \tikz[baseline]{
                \node[fill=red!25, ellipse, anchor=base]
                {$\langle\mathbf x_i,\mathbf x_j\rangle$};
            },$
\end{document}

enter image description here

1
  • 1
    Is there a way to modify this so that the shape more closely fits the expression it surrounds and doesn't introduce extra spacing, especially with an ellipse? I'm aware of tex.stackexchange.com/a/542997/218142 but that solution doesn't use TikZ. Nov 7, 2020 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .