0

How does one draw the elliptical blue background and the nodes? I am quite lost here.

automaton image

  • My suggestion is that you se the "dot language" to create the automaton and then use the well known script dot2tex to export (indeed you can also convert from dot to pdf). Take a look at https://martin-thoma.com/how-to-draw-a-finite-state-machine/ – boumol Mar 04 '16 at 19:31
  • I can see that's an advantage if you already know the language @boumol, but otherwise, that seems to introduce a quite unnecessary level of complexity. – cfr Mar 04 '16 at 22:00

1 Answers1

6

You can use children for the parent nodes. The labels can be appended when connecting the nodes. Something to get you started:

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{shapes,backgrounds,fit}

\begin{document}
\tikzset{every node/.style={fill=yellow,draw=black,circle},
myedgestyle/.style={edge from parent/.style={thick,draw,->}}
}
\begin{tikzpicture}[level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm}]
\node (parent) {1}
child[myedgestyle] {node (child1) {2}
    child {node (child2) {4} edge from parent node[left,draw=none,fill=none]{c}}
    child {node (child3) {5} edge from parent node[right,draw=none,fill=none]{d}}
edge from parent node[left,draw=none,fill=none]{a}}
child[myedgestyle] {node (child4) {3}
    child {node (child5) {6} edge from parent node[left,draw=none,fill=none]{e}}
    child {node (child6) {7} edge from parent node[right,draw=none,fill=none]{f}}
edge from parent node[right,draw=none,fill=none]{b}};

% For the cyan-coloured circles and ellipses behind the nodes and children
\begin{pgfonlayer}{background}
\filldraw[cyan] (parent.center) circle (5mm);
\filldraw[cyan] (child1.center) circle (5mm);
\filldraw[cyan] (child4.center) circle (5mm);
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child2) (child3)}] {};
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child5) (child6)}] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

The edge from parent node option has to be provided for every child. I couldn't find a way to predefine it in \tikzset. For a parent node, edge for parent node has to be written after the edge labels have been defined for its children.

I still haven't found a way to implement the background blue colour. It might have to do something with the backgrounds tikzlibrary.

EDIT: So using this link, we can call the fit library to get the ellipses for the child nodes. The backgrounds library makes sure we get the shapes after creating the nodes.

result

smike
  • 151
  • I added a class so your code can be compiled. If you intended a different one, you should edit it. Also, I removed a stray ; and added an image of the output. Feel free to roll-back if you object at all. – cfr Mar 04 '16 at 21:56
  • @smike This seems really helpful. However, I made a change. \node[shape=ellipse,cyan,inner sep=0pt,fit={(child2) (child3)}] {}; – GermanShepherd Mar 05 '16 at 03:06
  • Suppose I want two edges out of the same node, say, an edge labelled "x" from node 1 to node 2. How do I do that? – GermanShepherd Mar 05 '16 at 03:07
  • @cfr Thank you for the edit! I had to include the result graph, but wasn't sure exactly how (this is my first time answering on SE!).

    @GermanShepherd From the PGF manual, on page 325, it shows how to define custom edge from parent macros. I think that if there we change the path to \tikzchildnode\tikzchildanchor -- #2 \tikzparentnode\tikzparentanchor, then we can get a new edge from node 1 to node 2. In addition to that, we just need to make another edge from parent node label.

    – smike Mar 05 '16 at 14:12
  • @GermanShepherd I have so far not been able to define the macro correctly. I am not sure how to include it. Using edge from parent macro=mymacro in \tikzset didn't help. – smike Mar 05 '16 at 14:13
  • in the picture which you have drawn, if I added an edge edge from parent node[left,draw=none,fill=none]{h},edge from parent node[right,draw=none,fill=none]{g}}, I get a single edge labelled g and h on either side of the edge. I hope this should suffice for now. However, I am intrigued as to how to draw two edges from the same node. – GermanShepherd Mar 05 '16 at 15:59
  • The code in this question has some application of \tikzparentnode and \tikzchildnode in \drawing a path.

    When I define myedgestyle as:

    myedgestyle/.append style={edge from parent path={(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzchildanchor) ([xshift=20mm]\tikzchildnode) -- ([xshift=20mm]\tikzparentnode)}}

    I get doublelines, but not parallel, and require quite a lot of tweaking to get the right form, which goes against the principles of good TikZing:

    – smike Mar 05 '16 at 16:31
  • The other issue is that we do not get two arrows, but one arrow ending back to the parent. – smike Mar 05 '16 at 16:32
  • @smike Welcome! – cfr Mar 06 '16 at 14:07