I want to highlight the different parts of a matrix. I have used this code, but it can not work well. How can I modify it?
I want to build the following figure:
I have using the nicematrix in the following, but this is the output using Overleaf:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\overlay}[2][]{\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}[1]{%
\overlay{
\node [fit = (left.north west) (right.south east),
highlighted] (#1) {}; }
}
\newcommand{\flag}[2]{\overlay[baseline=(#1.base)]
{\node (#1) {$#2$};}}
\begin{document}
\[
M = \begin{pmatrix}
\flag{left}{p_1}, & p_2, & \flag{right}{p_3}, & \cdots, & \flag{left}{p_1},& p_2-p_1,& \flag{right}{p_3-p_2},&\cdots \\
\end{pmatrix}
\highlight{N}
\qquad
M^T = \begin{pmatrix}
\flag{left}{p_1} & p_2 & p_3 \\
p_1, & p_2-p_1, & \flag{right}{p_3-p_2}
\end{pmatrix}
\highlight{NT}
\]
\overlay{
\draw[->, thick, red, dotted] (before) -- (after);
\draw[->, thick, red, dashed] (N) -- (NT)
node [pos=0.68, above] {};
\node[above of = N ] { $x$ };
\node[above of = NT] { $z$ };
}
\end{document}
tikz
but you are nowhere usingtikzpicture
environment.