# How do I draw a path of grey background through elements of a matrix?

I would like to draw a highlighted path along elements of a matrix, as in the following example (which looks quite ugly as I drew the path manually with Gimp).

I know ways of changing the color of individual cells, rows and columns, but I haven't seen yet someone showing how to do such (shadow-like) highlighted paths.

Is there an easy way to do this for standard LaTeX matrices? If not, then I suppose it's not too hard with TikZ matrices? (How exactly?)

• Please provide what you try so far, at list code of your matrix . With TikZ matrix this is not so complicated. – Zarko May 15 '16 at 18:59
• @Zarko: I have an ordinary LaTeX matrix, and I am hoping that this is possible without TikZ matrices, as I would like to keep exactly the same look for all matrices in the text. I am using the pmatrix* environment. – Jay May 15 '16 at 19:10

This is slightly adopted on my old example of matrix with highlighted elements (it quit differ from your image):

To change path to your wish you only need to select nodes in matrix, through which path should go.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\centering
\begin{tikzpicture}[
strip/.style = {
draw=gray, rounded corners=3mm,
line width=3mm, opacity=0.2,
line cap=round
},
]
\matrix (M)  [matrix of math nodes,
column sep=1em,
nodes={text height=1ex,text width=2ex}]
{
1 &2 &3 &4 & 5 & 6  \\
1 &2 &3 &4 & 5 & 6  \\
1 &2 &3 &4 & 5 & 6  \\
1 &2 &3 &4 & 5 & 6  \\
1 &2 &3 &4 & 5 & 6  \\
1 &2 &3 &4 & 5 & 6  \\
};
\draw[strip,transform canvas={xshift=-0.5mm}]
(M-1-1.north) -- (M-4-1.center)
-- (M-4-4.center)
-- (M-2-4.center)
-- (M-2-2.center) -- (M-1-2.north);
\end{tikzpicture}
\end{document}


It's easy to do with pstricks, as it allows using a usual ams matrix environment. The relevant entries of the matrix are defined as \rnodes, and these nodes are connected with \ncboxes:

\documentclass{article}
\usepackage{amsmath}
\usepackage[table, x11names]{xcolor}
\usepackage{fourier}
\usepackage{pst-node, multido}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{equation*}
\begin{postscript}
\begin{matrix}
\rnode{A1}{1} & -1 & 3              & \rnode{A8}{8} & 10             & -5 \\
2             & 3  & 4              & \rnode{A7}{1} & \rnode{A6}{-2} & 2  \\
\rnode{A2}{5} & 1  & \rnode{A3}{-3} & 2             & 1              & 7  \\
0             & 1  & 0              & 2             & -1             & 4  \\
1             & 0  & \rnode{A4}{1}  & 0             & \rnode{A5}{-2} & 3  \\
8             & 1  & 2              & -2            & 1              & 0
\end{matrix}%
\psset{boxsize=1.5ex, boxdepth=1.5ex, nodesep =0.5ex,  linestyle=none, fillstyle=solid, fillcolor=VioletRed4, opacity=0.15}
\multido{\i=1+2, \in=2+2}{4}{\ncbox{A\i}{A\in}}
\psset{boxsize=1.2ex, boxdepth=1.2ex,  nodesepA=0.95ex, nodesepB=0.2ex}
\multido{\i=2+2, \in=3+2}{2}{\ncbox{A\i}{A\in}}
\ncbox{A7}{A6}
\end{postscript}
\end{equation*}

\end{document}