# Highlighting in a formula

In a complex formula or equation, it can be useful to emphasize a specific part. We can achieve this, for example, through highlighting by color or by framing. This is especially useful in presentations, where we could change the highlighted area from frame to frame while explaining.

We will take the challenge to work on more than simple basic math. So we can verify that the method works in arbitrary situations. So our original material will be amsmath matrices. We will illustrate a matrix transposition by highlighting sub matrices and drawing arrows.

We will use TikZ for this. Such a big graphics package may seem to be a bit heavy for such a purpose at first. But it provides us with consistent styles and allows us to do nearly anything graphically.

Note, because of TikZ positioning using markers in the .aux file, you need to compile at least two times to get the positioning correct.

The code, and more, is explained step by step in the LaTeX Cookbook, Chapter 10, Advanced Mathematics, Highlighting in a formula. Edit and compile if you like:
% Highlighting in a formula
% Author: Stefan Kottwitz
% https://www.packtpub.com/hardware-and-creative/latex-cookbook
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\overlay}[]{\tikz[overlay,
remember picture, #1]{#2}}
\tikzset{
highlighted/.style = { draw, thick, rectangle,
rounded corners, inner sep = 0pt,
fill = red!15, fill opacity = 0.5
}
}
\newcommand{\highlight}{%
\overlay{
\node [fit = (left.north west) (right.south east),
highlighted] (#1) {}; }
}
\newcommand{\flag}{\overlay[baseline=(#1.base)]
{\node (#1) {$#2$};}}
\begin{document}
$M = \begin{pmatrix} \flag{left}{1} & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & \flag{before}{12} & \flag{right}{13} & 14 & 15 \\ 16 & 17 & 18 & 19 & 20 \end{pmatrix} \highlight{N} \qquad M^T = \begin{pmatrix} \flag{left}{1} & 6 & 11 & 16 \\ 2 & 7 & \flag{after}{12} & 17 \\ 3 & 8 & \flag{right}{13} & 18 \\ 4 & 9 & 14 & 19 \\ 5 & 10 & 15 & 20 \end{pmatrix} \highlight{NT}$
\overlay{
\draw[->, thick, red, dotted] (before) -- (after);
\draw[->, thick, red, dashed] (N) -- (NT)
node [pos=0.68, above] {Transpose};
\node[above of = N ] { $N$   };
\node[above of = NT] { $N^T$ };
}
\end{document}