Automatic placing dynamic styling of nodes in a graph

This example was developed in response to a question on TeX.SE and shows how \tikzgraphnodename and \tikzgraphnodetext can be used to dynamically style and typeset nodes in a \graph.

It makes use of the ext.misc library of the tikz-ext bundle of which it uses

  • the /utils/TeX/if and
  • the /utils/TeX/ifnum keys

to check the first character in a node’s name if it’s a c (blue) or a p (gray).
The integer number will be evaluated to determine the intensity of the color.

%!TEX lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{
  graphs,
  graphdrawing,
  ext.misc}
\usegdlibrary{force} % requires LuaLaTeX
\begin{document}
\tikz[
  parse node name/.style={
    circle, minimum size=10mm, draw, densely dashdotted, thick,
    decide color/.expand once=#1},
  decide color/.style 2 args={
    /utils/TeX/if=c#1
      {/utils/TeX/ifnum={#2<5}{bluelight}{bluedark}}
      {/utils/TeX/ifnum={#2<8}{light}{dark}}},
  light/.style={fill=gray!20},  bluelight/.style={fill=blue!10},
  dark/.style ={fill=gray!60},  bluedark/.style ={fill=blue!30}
] \graph[
  spring electrical layout, horizontal=c2 to p13,
  node distance=1.5cm, typeset=$n_{\tikzgraphnodetext}$,
  nodes={parse node name=\tikzgraphnodename}] {
  % outer ring
  c2 -- {p1, p11, p6};
    p1 -- {p8, c6, p11};
      p8 -- {p3, p10, c6};
       p3 -- {p13, p15, p10};
         p13 -- {p15, c7};
           c7  -- {c3/\mathbf{c3}[draw=red], c4, p15};
           c3  -- {p14, c4};
           p14 -- {p7, c4};
         p7 -- {p9, p2, c4};
       p9 -- {c5, p12, p2};
     c5 -- {c1, p4, p12};
   c1 -- {p6, p4};
  p6 -- {p11, p4};
  % inner ring
  p11 -- {c6, p12, p4};
  p5 -- {c6 -- {p10, p12}, p10 -- p15, p15 -- c4, c4 -- p2, p2 -- p12, p12 -- p4};
};
\end{document}

Leave a Reply

Your email address will not be published.