1

I am trying to draw a FSM (with \usetikzlibrary{automata}) that has trees as states. With my current code, the edges of trees inside states are only drawn incompletely. What may be causing this and how can I fix it?

My code:

\begin{tikzpicture}[->]
    \node[state, inner sep=0] (q) {
        \begin{tikzpicture}[-]
            \node {$ \{ q_I, f, g, h, i \} $}
            child {node {$ \{ q_I \} $}}
            child {node {$ \{ f, g, h \} $}
            child {node {$ \{ g \} $}}
            child {node {$ \{ h \} $}}
            };
        \end{tikzpicture}
    };
\end{tikzpicture}

The result:

tree inside state

  • Welcome to TeX-SE. You should never nest tikzpictures. –  May 11 '19 at 14:16
  • @marmot Thanks! Could you explain that a little bit further? Obviously, the nesting produced a bad result in this case, but why is that? Where can I learn more about that? – flugdrachen May 11 '19 at 15:51
  • Please see here why nesting tikzpictures is a bad idea. Notice that by now this is even "more unsupported" than it used tp be, and future versions of TikZ may issue a warning if you do it, so don't. ;-) –  May 11 '19 at 15:57

1 Answers1

2

The reason why this happens is that you are nesting tikzpictures, which should be avoided. Here is a way to draw this figure without nesting tikzpictures.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{automata,fit}
\begin{document}
\begin{tikzpicture}
 \begin{scope}[local bounding box=tree]
            \node  {$ \{ q_I, f, g, h, i \} $}
            child {node {$ \{ q_I \} $}}
            child {node {$ \{ f, g, h \} $}
            child {node {$ \{ g \} $}}
            child {node {$ \{ h \} $}}
            };
 \end{scope}
 \node[state, inner sep=0,fit=(tree)] (q) {};           
\end{tikzpicture}
\end{document}

enter image description here

  • This works for one state, but how can I combine it with positioning of several states? If, e. g., I use \node[state, below of=q3, inner sep=0, fit=(tree4)] (q4) {};, only the circle marking the state is moved, but not the tree that I want inside. – flugdrachen May 11 '19 at 15:48
  • @flugdrachen You seem to have created a more complex example, so I do not know what tree4 is. If you use \begin{scope}[local bounding box=<something>] ... \end{scope} and then \node[circle,draw,fit=(<something>)] {};, the circle will be drawn around the stuff that is inside the scope. If you add a complete example to your above question that explains what tree4 stands for I can say more (or you could ask a separate question on that). –  May 11 '19 at 16:01
  • nvm, looking up the documentation for fit answered my question. The problem was that I was positioning the state around the box containing the tree with fit, then repositioning it with below of relative to another state, when instead I should have used below of on the tree box. – flugdrachen May 11 '19 at 16:32
  • Nope, it did not work at last. I put it in a new question. – flugdrachen May 11 '19 at 21:16
  • @flugdrachen Yes, I saw, upvoted and answered it. –  May 11 '19 at 21:16