4

I have two nodes in tikzpicture and in both nodes there is a graph in nested tikzpicture. Now, I would like an arrow to be pointing from one graph to the other - from the center of one graph to the center of the other graph. That's why the graphs are in the nodes - I can connect the two nodes with arrow. Problem is that edge labels in the second graph are not placed exactly over the edges. enter image description here Code:

\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}

\begin{document}
    \begin{tikzpicture}
    \tikzstyle{every node}=[circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white],
    \tikzstyle{vertex}=[circle, minimum size=4pt, inner sep=0pt, fill=orange]
        \node(graph) at (0,1) {
            \begin{tikzpicture}[font=\footnotesize, thick]
            \begin{scope}
                \node[vertex] (root)    at (4,  5) {};
                \node[vertex] (o)       at (4,  4) {};
                \node[vertex] (oc)      at (4,  3) {};
                \node[vertex] (a)       at (5,  4) {};

                \foreach \xfrom/\xto/\xlabel in {
                    root/o/o, o/oc/c,
                    root/a/a} {
                    \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
                };
            \end{scope}             
            \end{tikzpicture}
        };

        \node[right = of graph] (graph2) {
            \begin{tikzpicture}[font=\footnotesize, thick]
            \begin{scope}
                \node[vertex] (root)    at (4,  5) {};          
                \node[vertex] (o)       at (4,  4) {};
                \node[vertex] (oc)      at (4,  3) {};          
                \node[vertex] (a)       at (5,  4) {};

                \foreach \xfrom/\xto/\xlabel in {
                    root/o/o, o/oc/c} 
                {
                    \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
                };
                \foreach \xfrom/\xto/\xlabel in {
                    root/a/a} 
                {
                    \draw (\xfrom) to node[midway]{\xlabel} (\xto);
                };
            \end{scope}             
            \end{tikzpicture}
        };  

        \begin{scope}[segment amplitude=4]
        \draw[snake=triangles] (graph) -- (graph2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Possible solution is using of pic, but that's not exactly what I want, because it chooses one exact point in both graphs and connects them and not the graphs themselves (and so the arrow must be redirected every time the graph changes to lead from its center).

enter image description here

Code:

\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}

\begin{document}
    \tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
        vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}

    \tikzset{
        mygraph/.pic={
            \begin{scope}[font=\footnotesize, thick]
                \node[vertex] (root)    at (4,  5) {};
                \node[vertex] (o)       at (4,  4) {};
                \node[vertex] (oc)      at (4,  3) {};
                \node[vertex] (oco)     at (4,  2) {};
                \node[vertex] (a)       at (5,  4) {};

                \foreach \xfrom/\xto/\xlabel in {
                    root/o/o, o/oc/c, oc/oco/o,
                    root/a/a} {
                    \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
                };
            \end{scope}
        }
    }

    \begin{tikzpicture}
        \pic (graph) at (0,0) {mygraph=Hallo};
        \pic (graph2) at (3,0) {mygraph=Hallo};

        \path (grapha) -- (graph2o) coordinate[pos=0.2] (start) coordinate[pos=0.8] (end); \draw[snake=triangles,segment amplitude=4] (start) to (end);
    \end{tikzpicture}
\end{document}

2 Answers 2

4

This is one example of why nesting tikzpictures is not recommended.

An alternative might be two scopes with a local bounding box setting for each, and an xshift for the second.

enter image description here

\documentclass[12pt,a4paper,border=5mm]{standalone}
\usepackage[czech]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}

\begin{document}
\begin{tikzpicture}[
 every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
 vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}
]

\begin{scope}[local bounding box=graph1]
    \node[vertex] (root)    at (4,  5) {};
    \node[vertex] (o)       at (4,  4) {};
    \node[vertex] (oc)      at (4,  3) {};
    \node[vertex] (a)       at (5,  4) {};

    \foreach \xfrom/\xto/\xlabel in {
        root/o/o, o/oc/c,
        root/a/a} {
        \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
    };
\end{scope}             


\begin{scope}[local bounding box=graph2, xshift=3cm]
    \node[vertex] (root)    at (4,  5) {};          
    \node[vertex] (o)       at (4,  4) {};
    \node[vertex] (oc)      at (4,  3) {};          
    \node[vertex] (a)       at (5,  4) {};

    \foreach \xfrom/\xto/\xlabel in {
        root/o/o, o/oc/c} 
    {
        \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
    };
    \foreach \xfrom/\xto/\xlabel in {
        root/a/a} 
    {
        \draw (\xfrom) to node[midway]{\xlabel} (\xto);
    };
\end{scope}


  \path (graph1) -- (graph2) coordinate[pos=0.3] (start) coordinate[pos=0.8] (end);
  \draw[snake=triangles,segment amplitude=4] (start) to (end);

\end{tikzpicture}
\end{document}
4
4

Torbjørn T. local bounding box trick is way superior, so full credit to him. Yet I would like to argue that the pic thingy helps to avoid repetition. And one can then easily refer to elements of these pics with the prefix method.

\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}

\begin{document}
    \tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
        vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}

    \tikzset{
        mygraph/.pic={
            \begin{scope}[font=\footnotesize, thick]
                \node[vertex] (root)    at (4,  5) {};
                \node[vertex] (o)       at (4,  4) {};
                \node[vertex] (oc)      at (4,  3) {};
                \node[vertex] (oco)     at (4,  2) {};
                \node[vertex] (a)       at (5,  4) {};

                \foreach \xfrom/\xto/\xlabel in {
                    root/o/o, o/oc/c, oc/oco/o,
                    root/a/a} {
                    \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
                };
            \end{scope}
        }
    }

    \begin{tikzpicture}
    \begin{scope}[local bounding box=graph1]
        \pic (graph1) at (0,0) {mygraph=Hallo};
    \end{scope} 
    \begin{scope}[local bounding box=graph2]
        \pic (graph2) at (3,0) {mygraph=Hallo};
    \end{scope}
    \draw[snake=triangles,segment amplitude=4] ([xshift=2mm]graph1.east) to 
    ([xshift=-2mm]graph2.west);
    % just for fun 
    \draw[-latex,shorten >=1mm,shorten <=1mm] (graph1a) to[out=-10,in=160] (graph2root);
    \end{tikzpicture}
\end{document}

enter image description here

One possibility is to wrap the pics into nodes.

\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing,
snakes,fit,backgrounds}

\begin{document}
    \tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
        vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}

    \tikzset{
        mygraph/.pic={
            \begin{scope}[font=\footnotesize, thick]
                \node[vertex] (root)    at (4,  5) {};
                \node[vertex] (o)       at (4,  4) {};
                \node[vertex] (oc)      at (4,  3) {};
                \node[vertex] (oco)     at (4,  2) {};
                \node[vertex] (a)       at (5,  4) {};

                \foreach \xfrom/\xto/\xlabel in {
                    root/o/o, o/oc/c, oc/oco/o,
                    root/a/a} {
                    \draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
                };
            \end{scope}
        }
    }

    \begin{tikzpicture}
        \pic (graph1) at (0,0) {mygraph=Hallo};
        \pic (graph2) at (3,0) {mygraph=Hallo};
        \begin{scope}[on background layer]
        \node[fit=(graph1oco) (graph1a) (graph1root),rectangle] (leftgraph){};
        \node[fit=(graph2oco) (graph2a) (graph2root),rectangle] (rightgraph){};
        \end{scope}
        \draw[snake=triangles,segment amplitude=4] ([xshift=2mm]leftgraph.east) to
        ([xshift=-2mm]rightgraph.west);
    \end{tikzpicture}
\end{document}

enter image description here

Another possibility is to work with path pictures, but I think this would become more messy (but I might be proven wrong and learn something;-).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .