6

A diagram drawn with TikZ is used a couple of times and I defined a command for it.

In the first picture I want to mark a subset of the nodes by filling them with light gray, and another subset of nodes in the second picture. Is this possible, or do I need to duplicate the whole command for this?

The MWE shows the output before the desired node fillings.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows}

\tikzstyle{main}=[circle, minimum size = 10mm, thick, draw =black!80, fill = white!100, node distance = 12mm]
\tikzstyle{connect}=[-latex, thick]
\tikzstyle{box}=[rectangle, draw=black!100]
\newcommand{\model}{
  \node[main] (alpha) {$\alpha$};
  \node[main] (gamma) [right=of alpha] {$\lambda$};
  \node[main] (Z) [below=of alpha] {$Z$};
  \node[main] (C) [below=of gamma] {$C$};
  \node[main] (W) [right=of C] {$W$};
  \node[main] (R) [below=of W, fill=lightgray] {$R$};
  \node[main] (bias) [above=of W] {$b$};
  \node[main] (sigma) [right=of bias] {$\sigma$};
  \path (alpha) edge [connect] (Z)
    (gamma) edge [connect] (C)
    (Z) edge [connect] (C)
    (C) edge [connect] (R)
    (W) edge [connect] (R)
    (bias) edge [connect, bend left] (R)
    (sigma) edge [connect, bend left] (R)
    (C) edge [connect] (W);
}
\begin{document}
\begin{tikzpicture}
\model
%TODO: fill some nodes here
\end{tikzpicture}

\begin{tikzpicture}
\model
%TODO: fill other nodes here
\end{tikzpicture}
\end{document}

Output

Qrrbrbirlbel
  • 119,821
mektah
  • 633
  • Assign a style for the nodes of a subset and define this style differently for the usage of the picture. Basically, what percusse did in his answer. – Qrrbrbirlbel Sep 25 '13 at 13:00

1 Answers1

5

You can come up with your own option set by using the /.list (for repeating the same key mechanism to a comma separated list of argumments) and /.try (for checking if there is any style declared or not) handlers. Here is a very basic setting.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows}

\tikzset{main/.style={circle, minimum size = 10mm, thick, draw =black!80, fill = white!100, node distance = 12mm},
        connect/.style={-latex, thick},
        box/.style={rectangle, draw=black!100},
        furnish/.style args={#1 with #2}{
            /tikz/my #1/.append style={#2}
            }
        }

\newcommand{\model}[1]{
\tikzset{furnish/.list={#1}}
  \node[main,my alpha/.try] (alpha) {$\alpha$};
  \node[main,my gamma/.try] (gamma) [right=of alpha] {$\lambda$};
  \node[main,my Z/.try] (Z) [below=of alpha] {$Z$};
  \node[main,my C/.try] (C) [below=of gamma] {$C$};
  \node[main,my W/.try] (W) [right=of C] {$W$};
  \node[main,my R/.try] (R) [below=of W] {$R$};
  \node[main,my bias/.try] (bias) [above=of W] {$b$};
  \node[main,my sigma/.try] (sigma) [right=of bias] {$\sigma$};
  \path (alpha) edge [connect] (Z)
    (gamma) edge [connect] (C)
    (Z) edge [connect] (C)
    (C) edge [connect] (R)
    (W) edge [connect] (R)
    (bias) edge [connect, bend left] (R)
    (sigma) edge [connect, bend left] (R)
    (C) edge [connect] (W);
}
\begin{document}

\begin{tikzpicture}
\model{gamma with {rectangle,fill=gray!10},W with {opacity=0.5},R with {fill=lightgray}}
\end{tikzpicture}

\end{document}

enter image description here

An obligatory link :)

Should \tikzset or \tikzstyle be used to define TikZ styles?

percusse
  • 157,807