1

I'm trying to make two graphs and place them side by side and place edge labels over edges in the two graphs (as can be seen in the picture), but I am not successful in the second graph. My approach is to create two nodes in one tikzpicture nad place another two tikzpictures (the graphs itself) in these nodes. Here is my code:

\documentclass[12pt,a4paper]{article}
\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}
        };  
    \end{tikzpicture}
\end{document}

My result is as follows: enter image description here

What am I doing wrong or what am I missing? I would appreciate any suggestions. (I need to do it this way because of exact positioning of the graphs and because of additional arrows that will connect these graphs.)

EDIT: Actually, I need to make an arrow between the two graphs:

\documentclass[12pt,a4paper]{article}
\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}

enter image description here I'm not sure if this is possible with pic...

  • 1
    You should not nest tikzpicture environments. – user121799 May 3 '18 at 18:15
  • OK, so my approach is completely wrong? There is no way how to make my example working? – Frimlik May 3 '18 at 18:18
  • There is the pic syntax, but since this is too long for a comment I posted an answer. – user121799 May 3 '18 at 18:25
3

There is the pic syntax allowing one to draw small pictures in a tikzpicture.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}
%
    \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] (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{document}
    \begin{tikzpicture}
    \pic (graph) {mygraph};
    \pic at (3,0) (graph2) {mygraph};
    \end{tikzpicture}
\end{document}

enter image description here

If you want to change some things, then you can work with parameters. (And please note that \tikzstyle is deprecated.)

EDIT: To reference the nodes inside the pics outside, you just need to add the appropriate prefix, e.g.

\draw[-latex,shorten >=3mm,shorten <=3mm] (grapha) -- (graph2o);

will yield

enter image description here

Explanation: the nodes inside the picture can be referenced as <pic name><node name> such that the node a in the pic named graph becomes grapha. (shorten just shortens the arrow.) Notice that you could also play with nodes and the path picture key, but for this this might be less elegant.

  • So instead of nested tikzpictures I only add some pics... Nice solution! Thank you very much! – Frimlik May 3 '18 at 18:30
  • And regarging the note about tikzstyle - I am a very occasional user of LaTeX, so I'm not very familiar about tikzset. So thank you also for information and warning. – Frimlik May 3 '18 at 18:32
  • Is there a way to name the two pics to be able to draw an arrow between them? – Frimlik May 3 '18 at 19:10
  • @Frimlik Sure, just do \draw[-latex,shorten >=3mm,shorten <=3mm] (grapha) -- (graph2o);. Explanation: the nodes inside the picture can be referenced as <pic name><node name> such that the node a in the pic named graph becomes grapha. (shorten just shortens the arrow.) Notice that you could also play with nodes and the path picture key, but for this this might be less elegant. – user121799 May 3 '18 at 19:22
  • I edited question - I need the arrow to be directed from center of the left and to the center of the right graph. – Frimlik May 3 '18 at 19:55

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.