Multiple TikZ keys with foreach

Here's another version, not all that different from the previously supplied but combines them in a slightly different way. In effect, it is a bit like defining a dynamic style alias that expands to the given list of options. When a style like red drawing/.style={draw,red} is defined, then calling red drawing executes \tikzset{draw,red} (sort of, actually it's pgfkeys). So we simulate this by defining a key that executes \tikzset on whatever it is passed. So apply style={do,something,and,something,else} will execute \tikzset{do,something,and,something,else}. On its own, this isn't all that useful. But when combined with the /.expand once key handler we can pass it a macro, get that macro expanded once, and then execute the style it contains.

\documentclass{article}
\usepackage{tikz}
\tikzset{%
  apply style/.code={%
    \tikzset{#1}%
  }
}
\begin{document}

\begin{tikzpicture}
\path \foreach \x/\content/\style in {%
  0/a/draw,
  1/b/{draw,red},
  2/c/{circle,draw=blue},
  3/d/draw%
} {
  node[apply style/.expand once=\style] at (\x,0) {\content}
} ;
\end{tikzpicture}
\end{document}

Use indirect styles numbered (perhaps more verbose but flexible):

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \tikzset{
    s0/.style={draw},
    s1/.style={draw,red},
    s2/.style={circle,draw=blue},
    s3/.style={draw},
  }
  \foreach \x/\content in {%
    0/a,
    1/b,
    2/c,
    3/d%
  } {
    \node[s\x] at (\x,0) {\content};
  };
\end{tikzpicture}
\end{document}

enter image description here


Personally I always use the next method and in a lot of cases I use \protected@edef with LaTeX. With the next method you can use \path node ... node ... ;.

\documentclass{standalone}
\usepackage{tikz}
 % \makeatletter\let\protectededef\protected@edef   not useful here but It' sometimes  
 % interesting
\begin{document}
  \begin{tikzpicture}
    \path 
    \foreach \x/\content/\style in {0/a/draw,
                                    1/b/{draw,red},
                                    2/c/{draw,circle,blue},
                                    3/d/draw}{
  \pgfextra{\edef\tmp{   % or \pgfextra{\protectededef\tmp{
  node[\style] at (\x,0) {\content}}}
  \tmp
  };
  \end{tikzpicture}  
\end{document}

enter image description here