节点中并排移动 tikzpictures 中的边缘标签

节点中并排移动 tikzpictures 中的边缘标签

我尝试制作两个图形并将它们并排放置,并在两个图形的边缘上放置边缘标签(如图所示),但在第二张图中没有成功。我的方法是在一个图中创建两个节点,并在这些节点中tikzpicture放置另外两个tikzpicture节点(图形本身)。这是我的代码:

\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}

我的结果如下: 在此处输入图片描述

我做错了什么或遗漏了什么? 我将不胜感激任何建议。(我需要这样做是因为图表的精确定位以及连接这些图表的附加箭头。)

编辑:实际上,我需要在两个图之间画一个箭头:

\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}

在此处输入图片描述 我不确定这是否可以实现pic...

答案1

有一种pic语法允许人们在 中绘制小​​图片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}

在此处输入图片描述

如果您想改变某些事情,那么您可以使用参数。(请注意,这\tikzstyle已被弃用。)

编辑:若要引用s外部的节点pic,只需添加适当的前缀,例如

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

将产生

在此处输入图片描述

解释:图片内的节点可以被引用,这样图片中的<pic name><node name>节点就变成了 grapha。(只是缩短了箭头。)请注意,您也可以使用节点和路径图片键,但这可能不太优雅。agraphshorten

相关内容