5

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.

1 Answers1

5

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}

enter image description here

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}

enter image description here

Gonzalo Medina
  • 505,128