3

I'm using the graphs TikZ library to draw directed acyclic graphs. When I apply a particular style to a given node, I would like that style to be inherited by all descendants of the node in question.

To fix ideas, say I've defined a node style called alert that modifies the default node style. When I apply that alert style to a node locally, I would like all descendants of that node to inherit the style in question, without having to apply that style to all its descendants manually.

Here is an illustrative example:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs}

\tikzset{
  every node/.style={draw=black},
  alert/.style={draw=red},
}

\begin{document}
\begin{tikzpicture}
\graph{
  A -> {
    B,
    C[alert] -> D -> { E, F},
  },
};
\end{tikzpicture}
\end{document}

enter image description here

At the moment, alert is only applied to node C. However, by applying the alert style to node C, I would like all descendants of C (i.e. nodes D, E, and F) to inherit that style, without having to type

\graph{
  A -> {
    B,
    C[alert] -> D[alert] -> { E[alert], F[alert]},
  },
};

Is there a way of doing that?


Edit: Harish Kumar's approach can make my life much easier, but is not exactly what I want. Here is a more complex example illustrating what's missing:

\graph{
  A -> { B, C[alert] }  -> D -> { E -> F, G -> H }
};

enter image description here

Here, I'd like nodes D to H to inherit the alert style applied to their ancestor, node C. I could follow Harish's advice and write

\graph{
  A -> { B, C[alert] }  -> {[nodes=alert] D -> { E -> F, G -> H }}
};

enter image description here

but that would still force me to apply alert twice in the graph. I would like a particular style applied to one node to be inherited by all nodes further down the chain

  • without having to that style anywhere else, and
  • no matter how the graph is specified.

Is that even possible?

  • You can use a scope. – John Kormylo Sep 6 '14 at 17:33
  • @JohnKormylo Do you mean a scope environment? – jub0bs Sep 6 '14 at 17:39
  • There are limitations as {B, C} fall within a group. Whatever you specify for node C, it is difficult to carry over to D E etc. – user11232 Sep 7 '14 at 13:32
  • @HarishKumar I'm wondering whether what I'm asking for is even possible... – jub0bs Sep 7 '14 at 13:38
  • @Jubobs Well, you know the rules ;-) May be some wizard will appear with some black magic! Let us see. – user11232 Sep 7 '14 at 13:39
3

You can use

{[nodes=alert]C -> D -> { E, F},
  }

code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs}

\tikzset{
  every node/.style={draw=black},
  alert/.style={draw=red},
}

\begin{document}
\begin{tikzpicture}
\graph{
  A -> {
    B,
    {[nodes=alert]C -> D -> { E, F},
  }},
};
\end{tikzpicture}
\end{document}

enter image description here

  • Yes! Thanks, I missed that in the manual. For information, it's documented in 19.3.2 Syntax of Group Specifications. – jub0bs Sep 7 '14 at 9:03
  • That's actually not exactly what I want, but that will do for now. – jub0bs Sep 7 '14 at 10:32
  • @jubobs did I leave anything out? – user11232 Sep 7 '14 at 11:13
  • I'll edit my question to add a more involved example. – jub0bs Sep 7 '14 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.