TikZ matrix, style for combination or rows and columns

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


More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{myset/.style args = {(#1,#2)}{%
    row #1 column #2/.style={nodes={text=red}}}}
\begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,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


This can be done via \foreach indeed, but since the keys set with \pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with \globaldef, as in the following example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\newcommand{\rows}[2]{% #1 = rows, #2 = style
\foreach \r in {#1} {%
  \globaldefs=1\relax
  \tikzset{row \r/.style={#2}}
}%
}

\newcommand{\cols}[2]{% #1 = columns, #2 = style
\foreach \r in {#1} {%
  \globaldefs=1\relax
  \tikzset{column \r/.style={#2}}
}%
}

\begin{tikzpicture}
\cols{1,3}{blue}
\rows{1,3}{red}
\matrix [matrix of nodes]
{
 8 & 1 & 6 \\
 3 & 5 & 7 \\
 4 & 9 & 2 \\
  };
 \end{tikzpicture}
\end{document}

Result