# Highlight the different parts of a matrix

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}

• You are using tikz but you are nowhere using tikzpicture environment. Jun 24 at 19:33
• I've used this link and I have tried to modified it latex-cookbook.net/highlight-formula Jun 24 at 19:34
• Another thing is how you set the matrix. You don't separate an element from each other using commas followed by ampersand. You only use ampersand. Edit: Did you mean to represent this as column vector? Jun 24 at 19:41
• Yes. I want a column vector. Jun 24 at 19:46
• Pleas see the update with arrows. Jun 24 at 20:57

I applied the nicematrix package to replicate the manual drawing. It uses Tikz nodes, so the syntax will be familiar to you.

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\tikzset{highlight/.style={rectangle,
fill=red!15,
blend mode = multiply,
rounded corners = 0.5 mm,
inner sep=1pt,
fit = #1}}

$M = \begin{bNiceMatrix}[first-row, extra-margin=2pt] & x & & & &y & & \\ p_1,& p_2, & p_3, & \cdots & p_1, & p_2-p_1, & p_3-p_2, &\cdots \\ \CodeAfter \tikz \node [highlight = (1-1) (1-3)] {} ; \tikz \node [highlight = (1-5) (1-7)] {} ; \end{bNiceMatrix}$

\bigskip

$M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt] &z & \\ p_1, & p_2, & p_3 \\ p_1, & p_2-p_1, & p_3-p_2 \CodeAfter \tikz \node [highlight = (2-1) (2-3)] {} ; \end{bNiceMatrix}$

\end{document}


UPDATE (with arrows)

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

% from https://latex-cookbook.net/highlight-formula/
\newcommand{\overlay}[2][]{\tikz[overlay,remember picture, #1]{#2}}

\newcommand{\flag}[2]{\overlay[baseline=(#1.base)]
{\node (#1) {$#2$};}}

\begin{document}

\tikzset{highlight/.style={rectangle,
fill=red!15,
blend mode = multiply,
rounded corners = 0.5 mm,
inner sep=1pt,
fit = #1}}

$M = \begin{bNiceMatrix}[first-row, extra-margin=2pt] & x & & & &y & & \\ p_1,& p_2, \flag{Mx}{} & p_3, & \cdots & p_1, & p_2-p_1, & \flag{My}{}p_3-p_2, &\cdots \\ \CodeAfter \tikz \node [highlight = (1-1) (1-3)] {} ; \tikz \node [highlight = (1-5) (1-7)] {} ; \end{bNiceMatrix}$

\bigskip

$M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt] &\flag{MTz}{z} & \\ p_1, & p_2, & p_3 \\ p_1, & p_2-p_1, & p_3-p_2 \CodeAfter \tikz \node [highlight = (2-1) (2-3)] {} ; \end{bNiceMatrix}$

\overlay{
\draw[->, thick, red, dotted] (Mx) -- (MTz);
\draw[->, thick, red, dotted] (My) -- (MTz);
}

\end{document}

• Very nicely done. I used nicematrix for this and it positioned the elements very well. Normal matrix environments seem to displace the elements out of parenthesis. Now I recommend giving a semicolon after the first cdots in matrix M to actually make it a column vector. Plus remove commas in the matrix M^T and cdots at the end of each row to make a valid transpose. Jun 24 at 20:35
• He did also draw the arrows in the example. You might want to draw that here as well. Jun 24 at 20:38
• Thank you so much. Jun 24 at 20:39
• Remark: With the latest version of nicematrix (v. 5.16 of 2021-06-20), it's possible to obtain the same output without the use of transparency (due in your code to blend mode = multiply). Jun 24 at 20:40

A slight modification of Simon Dispa's answer which does not use transparency thanks to the latest version of nicematrix (v 5.16 of 2021-06-20).

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\tikzset{highlight/.style={rectangle,
fill=red!15,
rounded corners = 0.5 mm,
inner sep=1pt,
fit = #1}}

$M = \begin{bNiceMatrix}[first-row, extra-margin=2pt] \CodeBefore [create-cell-nodes] \tikz \node [highlight = (1-1) (1-3)] {} ; \tikz \node [highlight = (1-5) (1-7)] {} ; \Body & x & & & &y & & \\ p_1,& p_2, & p_3, & \cdots & p_1, & p_2-p_1, & p_3-p_2, &\cdots \\ \end{bNiceMatrix}$

\bigskip

$M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt] \CodeBefore [create-cell-nodes] \tikz \node [highlight = (2-1) (2-3)] {} ; \Body &z & \\ p_1, & p_2, & p_3 \\ p_1, & p_2-p_1, & p_3-p_2 \end{bNiceMatrix}$

\end{document}


As usual with nicematrix, you need several compilations (because of the PGF/Tikz nodes).