TikZ-PGF – Styling Rows and Columns in a TikZ Matrix

tikz-matrixtikz-pgf

In the TikZ manual we have the example below that shows how to apply an style to an entry of the matrix. It is also easy to apply it for an entire row and an entire column. My question is how can we achieve the same for several entries? Is there a simple way to specify a style, say for example, rows 1 & 3 and columns 1 & 2?

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[row 2 column 3/.style=red]
\matrix [matrix of nodes]
{
 8 & 1 & 6 \\
 3 & 5 & 7 \\
 4 & 9 & 2 \\
  };
 \end{tikzpicture}
 \end{document}

Best Answer

You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
\begin{tikzpicture}[myset/.list={1,3,5}]
\matrix [matrix of nodes]
{
 8 & 1 & 6 & 8 & 1 & 6 \\
 3 & 5 & 7 & 3 & 5 & 7 \\
 4 & 9 & 2 & 4 & 9 & 2 \\
  };
\end{tikzpicture}
\end{document}

enter image description here