2

I'd like to have beautiful state machine without any thinking of it's layout. I also need some node grouping with description of the group. So, I wrote the following:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{backgrounds, positioning, fit, graphdrawing, graphs, automata, shapes.geometric}
\usetikzlibrary{shapes, snakes, petri}
\usetikzlibrary{arrows, decorations.pathmorphing}
\usegdlibrary{trees, layered, force}

\begin{document}
\begin{tikzpicture}
  \graph[tree layout, grow' = right,
         nodes = {ellipse, draw}]
  {
    s -> {s1, s2, s3};
    s1 -> s1v2;
    s1v2 -> s1v1 ->[bend right] s1v2;
    s1v2 -> s1v3t;
    s2 -> s2v1 -> s2v2 ->[bend right] s2v1;
    s2v2 -> s2v3t;
    s2 -> s2v3 -> s2v2t;
    s3 -> s3v2 -> {s3v3t, s3v1t};
    NS1[draw, rectangle] // {s1v2, s1v1};
    NS2[draw, rectangle] // {s2v1, s2v2}
  };
\end{tikzpicture}
\end{document}

However, subgraph nodes is overlapping with the regular ones. (If you want to see it for yourself, compile code with lualatex - it's required for TikZ 3 graph lay out algorithms). I think it's a consequence of them being created after graph has been laid out (see pgf manual). What can I do?

  • Okay, the only (for now) present answer from @esdd is good, but (if there are TikZ experts here) is there more automated way? That would be completely beautiful :-) – Andrew Zabavnikov Jul 16 '14 at 21:09
3

Maybe you can enlarge the sibling sep either for the whole graph

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{backgrounds, positioning, fit, graphdrawing, graphs, automata, shapes.geometric}
\usetikzlibrary{shapes, snakes, petri}
\usetikzlibrary{arrows, decorations.pathmorphing}
\usegdlibrary{trees, layered, force}

\begin{document}
\begin{tikzpicture}
  \graph[tree layout, grow' = right,
         nodes = {ellipse, draw},
         sibling sep=3em,% enlarge the sibling sep to 3em
        ]
  {
    s -> {s1, s2, s3};
    s1 -> s1v2;
    s1v2 -> s1v1 ->[bend right] s1v2;
    s1v2 -> s1v3t;
    s2 -> s2v1 -> s2v2 ->[bend right] s2v1;
    s2v2 -> s2v3t;
    s2 -> s2v3 -> s2v2t;
    s3 -> s3v2 -> {s3v3t, s3v1t};
    NS1[draw, rectangle] // {s1v2, s1v1};
    NS2[draw, rectangle] // {s2v1, s2v2}
  };
\end{tikzpicture}
\end{document}

enter image description here

or only for s1v3t

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{backgrounds, positioning, fit, graphdrawing, graphs, automata, shapes.geometric}
\usetikzlibrary{shapes, snakes, petri}
\usetikzlibrary{arrows, decorations.pathmorphing}
\usegdlibrary{trees, layered, force}

\begin{document}
\begin{tikzpicture}
  \graph[tree layout, grow' = right,
         nodes = {ellipse, draw},
        ]
  {
    s -> {s1, s2, s3};
    s1 -> s1v2;
    s1v2 -> s1v1 ->[bend right] s1v2;
    {[sibling sep=6em]s1v2 -> s1v3t};% enlarge sibling sep
    s2 -> s2v1 -> s2v2 ->[bend right] s2v1;
    s2v2 -> s2v3t;
    s2 -> s2v3 -> s2v2t;
    s3 -> s3v2 -> {s3v3t, s3v1t};
    NS1[draw, rectangle] // {s1v2, s1v1};
    NS2[draw, rectangle] // {s2v1, s2v2};
  };
\end{tikzpicture}
\end{document}

enter image description here

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.