# How to highlight sub-matrix / nodes with tikz? [duplicate]

I'm trying to highlight a box of different cells of a matrix that I'm rendering. Currently, this highlights the entire matrix in green. What I would like to do is within that box highlight another series of nodes. For instance, I would like to draw a box / circle / etc around N1, N2, N3, and N4. Is there a relatively pain free way of going about doing this?

\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};


EDIT: I was looking for a solution that could both enclose and highlight a subset of nodes, which makes it differ from the duplicate questions a bit.

## marked as duplicate by Martin Schröder, Thorsten, Peter Jansson, cmhughes, ChrisSMar 5 '14 at 21:14

• Possible duplicate: Highlight elements in the matrix – Werner Mar 5 '14 at 20:01
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Mar 5 '14 at 20:08
• With highlight, do you mean just draw a path surrounding those nodes (in which case this is a duplicate of the answer linked by Werner in his comment), or do you also want to draw and fill the path? – Gonzalo Medina Mar 5 '14 at 20:20
• I would like to draw and fill the path. – i_thought_i_was_smart Mar 5 '14 at 20:24

Since filling the path is required here, this is not an exact duplicate of Highlight elements in the matrix, but we can use some of the ideas there. In the code below I show two options; the first one using a simple \draw and the other one using the fit library; since, in both cases, a filling color is added, the filling overwrites the nodes and they must be "redrawn", which is easily accomplished with a \foreach loop:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}

\begin{document}

\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\draw[rounded corners]
([xshift=-10pt]A-3-1.north west) --
([xshift=-10pt,yshift=10pt]A-2-2.north west) --
([xshift=10pt,yshift=10pt]A-2-2.north east) --
([xshift=10pt,yshift=-10pt]A-4-2.south east) --
([xshift=-10pt,yshift=-10pt]A-4-2.south west) --
([xshift=-10pt]A-3-1.south west) -- cycle;
\end{tikzpicture}\hfill
\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\node[draw,fit={(A-3-1) (A-2-2) (A-4-2)}] {};
\end{tikzpicture}

\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\draw[cyan,fill=cyan!20,rounded corners]
([xshift=-10pt]A-3-1.north west) --
([xshift=-10pt,yshift=10pt]A-2-2.north west) --
([xshift=10pt,yshift=10pt]A-2-2.north east) --
([xshift=10pt,yshift=-10pt]A-4-2.south east) --
([xshift=-10pt,yshift=-10pt]A-4-2.south west) --
([xshift=-10pt]A-3-1.south west) -- cycle;
\foreach \Pos/\Nodo in {A-3-1/N1,A-2-2/N2,A-3-2/N3,A-4-2/N4}
\node[inner] at (\Pos) {\Nodo};
\end{tikzpicture}\hfill
\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes,outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\node[draw=red!70!black,fill=red!20,fit={(A-3-1) (A-2-2) (A-4-2)}] {};
\foreach \Pos/\Nodo in {A-3-1/N1,A-2-2/N2,A-3-2/N3,A-4-2/N4}
\node[inner] at (\Pos) {\Nodo};
\end{tikzpicture}

\end{document}


For more complicated paths, I thought about using the nice \convexhull command defined by Jake in his answer to padded boundary of convex hull; however, as of version 3.0.0 of PGF/TikZ, Jake's code no longer works. I used a variation of the simple \draw approach illustrated above and also, a to[bend=...] approach similar to the one used by jackal in his answer to the same question:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,calc}

\begin{document}

\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\draw[cyan,fill=cyan!20,rounded corners]
([xshift=-10pt]A-3-1.north west) --
([xshift=-10pt,yshift=10pt]A-2-2.north west) --
([xshift=10pt,yshift=10pt]A-2-2.north east) --
([xshift=10pt,yshift=-10pt]A-2-2.south east) --
([xshift=10pt]A-3-1.north east) --
([xshift=10pt]A-3-1.south east) --
([xshift=10pt,yshift=10pt]A-4-2.north east) --
([xshift=10pt,yshift=-10pt]A-4-2.south east) --
([xshift=-10pt,yshift=-10pt]A-4-2.south west) --
([xshift=-10pt]A-3-1.south west) -- cycle;
\foreach \Pos/\Nodo in {A-3-1/N1,A-2-2/N2,A-3-2/N3,A-4-2/N4}
\node[inner] at (\Pos) {\Nodo};
\end{tikzpicture}\hfill
\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner,draw}]{
&    & N5 & N9  \\
& N2 & N6 & N10 \\
N1 & N3 & N7 & N11 \\
& N4 & N8 &     \\
};
\path[draw=red!20,line width=1.4cm, line cap=round, line join=round]
([xshift=10pt]A-3-1.west) to[bend left=20] (A-2-2.east)
to[bend right=30] ([xshift=-10pt]A-3-1.east)
to[bend right=30] (A-4-2.east)
to[bend left=20] ([xshift=10pt]A-3-1.west) -- cycle;
\foreach \Pos/\Nodo in {A-3-1/N1,A-2-2/N2,A-3-2/N3,A-4-2/N4}
\node[inner] at (\Pos) {\Nodo};
\end{tikzpicture}

\end{document}


• Awesome! Would there be an easy way to modify this to "wrap around" elements? For instance, what if you didn't want to include N3? – i_thought_i_was_smart Mar 5 '14 at 20:55
• @i_thought_i_was_smart please see my updated answer. – Gonzalo Medina Mar 5 '14 at 21:48