Highlight Parts of An Equation in LaTeX

May. 19, 2025 • Updated May. 23, 2025

Method 1: by the \colorbox command1

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass{article}
\usepackage{amsmath}
\usepackage[svgnames]{xcolor}

\begin{document}

\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= \colorbox{LightBlue}{$a^3+b^3$}
\end{align*}

\end{document}

image-20250519201656344

Note that, the equation, i.e. a^3+b^3, should be put in an in-line equation environment wrapped in two $s.


Method 2: by the \tcboxmath command of tcolorbox package2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}
\usepackage{amsmath}
\usepackage[svgnames]{xcolor}
\usepackage[most]{tcolorbox}
 
\begin{document}

\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= \tcboxmath[colback=LightBlue!25!white,colframe=blue]{a^3+b^3}
\end{align*}

\end{document}

image-20250519203017829

Note that, this method will change the position of the highlighted parts as be centered in the box.


Method 3: by redefining the \boxed command using the hf-tikz package

The \boxed command is from the amsmath package, and it can be used to box parts of an equation:

1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{article}
\usepackage{amsmath}
 
\begin{document}

\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \boxed{a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2} \notag \\
&= a^3 + b^3
\end{align*}

\end{document}

image-20250523171743187

And we can use the hf-tikz package to redefine the \boxed command, to make the box more beautiful34:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\documentclass{article}
\usepackage{amsmath}
\usepackage[svgnames]{xcolor}
\usepackage[customcolors,shade]{hf-tikz}

\newcommand*{\boxcolor}{blue}
\makeatletter
\renewcommand{\boxed}[1]{\textcolor{\boxcolor}{
	\tikz[baseline={([yshift=-1ex]current bounding box.center)}]
	\node[rectangle,minimum width=1ex,rounded corners,draw,fill=LightBlue!25] 
	{\normalcolor\m@th$\displaystyle#1$};}}
\makeatother
 
\begin{document}

\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \boxed{a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2} \notag \\
&= a^3 + b^3
\end{align*}

\end{document}

image-20250519202726765

Method 4: by the hf-tikz package

All of above methods don’t support the syntax of spanning multiple rows of an equation, something like:

1
2
3
4
5
6
7
8
9
% ...

\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \colorbox{LightBlue}{a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= $a^3+b^3$}
\end{align*}

% ...

or,

1
2
3
4
5
\begin{align*}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tcboxmath[colback=LightBlue!25!white,colframe=blue]{a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3+b^3}
\end{align*}

I guess this is because some special commands like \\ or & will break those commands. So, from this perspective, the method provided by the hf-tikz package56, using \tikzmarkin{node-id} and \tikzmarkend{node-id} to mark the beginning (i.e. the left upper corner) and the end (i.e. the right lower corner) of the highlighted box, is much more flexible. Here are some examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
\documentclass{article}
\usepackage{amsmath}
\usepackage[svgnames]{xcolor}
\usepackage[customcolors,shade]{hf-tikz}
 
\begin{document}

\begin{equation}
\begin{split}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tikzmarkin[set fill color=LightBlue!10, set border color=blue]{a} a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3 + b^3\tikzmarkend{a} % Default position
\end{split}
\end{equation}

\begin{equation}
\begin{split}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tikzmarkin[set fill color=LightBlue!10, set border color=blue, below right offset={0,0},above left offset={0,0}]{b} a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3 + b^3\tikzmarkend{b}
\end{split}
\end{equation}

\begin{equation}
\begin{split}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tikzmarkin[set fill color=LightBlue!10, set border color=blue]{c}(0,0)(0,0) a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3 + b^3\tikzmarkend{c}
\end{split}
\end{equation}

\begin{equation}
\begin{split}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tikzmarkin[set fill color=LightBlue!10, set border color=blue, below right offset={3.8,-0.2},above left offset={-0.07,0.4}]{d}a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3 + b^3\tikzmarkend{d}
\end{split}
\end{equation}

\hfsetfillcolor{red!10}
\hfsetbordercolor{red}
\begin{equation}
\begin{split}
&\mathrel{\phantom{=}} (a+b)(a^2-ab+b^2) \notag \\
&= \tikzmarkin[below right offset={3.8,-0.2},above left offset={-0.07,0.4}]{e}a^3 - a^2b + ab^2 + a^2b - ab^2 + b^2 \notag \\
&= a^3 + b^3\tikzmarkend{e}
\end{split}
\end{equation}

\end{document}

image-20250519203630437

Note that here we showed two syntaxes to change the size and position of the highlighted box (see6 in detail):

1
\tikzmarkin{marker-id}(below right offset)(above left offset)

and

1
\tikzmarkin[below right offset={xxx,xxx},above left offset={xxx,xxx}]


References