两个图中节点之间的虚线

两个图中节点之间的虚线

我需要为我的硕士论文报告创建一些匹配图,如下所示: 在此处输入图片描述

我找到了一种叫做 PG 3.0 的东西,它可以制作图表,但我不知道如何制作虚线或树的名称(参见上图中的 T1 和 T2)

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=stealth, every node/.style={circle, draw, minimum size=0.75cm}]
\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
    {
        R -> { 
          a,
          b,
          c
        },
        R -> {
            a, 
            q,
            c
        }
    };
\end{tikzpicture}
\end{document}

答案1

下面我提出两个选择:

  1. 这是一个使用“pure”TikZ和两个\graphs 的选项;命名它们允许使用节点来绘制线条:

    \documentclass[tikz,border=10pt]{standalone}
    \usetikzlibrary{graphdrawing}
    \usetikzlibrary{graphs}
    \usegdlibrary{trees}
    
    \begin{document}
    
    \begin{tikzpicture}[>=stealth, every node/.style={circle, draw, minimum size=0.75cm}]
    % tree to the left
    \graph[tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
        {[name=left]
            R -> { 
              a -> {
                  b, 
                  c
                  },
              d  
            },
        };
    % tree to the right
    \begin{scope}[xshift=4cm]    
    \graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
        {[name=right]
            R -> { 
              a' -> {
                  b, 
                  {[name=m] b}
                  },
              d -> {
                  e, 
                  f
                  }  
            },
        };
    \end{scope}
    
    % naming the trees
    \node[draw=none,label={above left:$T_{1}$}] at (left R) {};    
    \node[draw=none,label={above left:$T_{2}$}] at (right R) {};    
    
    % connecting the trees
    \draw[dashed]
          (left R) to[out=30,in=150] (right R);
    \draw[dashed]
          (left a) to[out=40,in=140] (right a');
    \draw[dashed]
          (left d) to[out=40,in=140] (right d);
    \draw[dashed]
          (left b) to[out=-40,in=-140] (right b);
    \draw[dashed]
          (left b) to[out=-40,in=-140] (right m b);
    \end{tikzpicture}
    
    \end{document}
    

    在此处输入图片描述

  2. 这是现在使用这个时间的另一种选择forest包和一棵树(同样,关键是要有一个可用的命名机制):

    \documentclass[varwidth,border=10pt]{standalone}
    \usepackage{forest}
    
    \begin{document}
    \begin{forest}
    for tree={
      draw,
      circle,
      minimum size=20pt
    }
    [,phantom,s sep=1cm
    [R,name=rl,
      [a,name=al
        [b,name=bl]
        [b]
      ]
      [d,name=dl
      ]
    ]
    [R,name=rr
      [a',name=ar
        [b,name=bri]
        [b,name=brii]
      ]
      [d,name=dr
        [e]
        [f]
      ]
    ]
    ]
    % naming the trees
    \node[inner sep=7pt,label={above left:$T_{1}$}] at (rl) {};
    \node[inner sep=7pt,label={above left:$T_{2}$}] at (rr) {};
    % connecting the trees
    \draw[dashed]
          (rl) to[out=30,in=150] (rr);
    \draw[dashed]
          (al) to[out=40,in=140] (ar);
    \draw[dashed]
          (dl) to[out=40,in=140] (dr);
    \draw[dashed]
          (bl) to[out=-40,in=-140] (bri);
    \draw[dashed]
          (bl) to[out=-40,in=-140] (brii);
    \end{forest}
    
    \end{document}
    

    在此处输入图片描述

答案2

下面是另一个建议,使用\graph内部有两个子图:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[
    >=stealth,
    subgraph text top=text centered,
  ]
\graph[
    tree layout,
    nodes={circle, draw, minimum size=0.75cm},
    level distance=0.5in,
    sibling distance=0.5in,
    component sep=3em % sep between the two subgraphs/trees
  ]
  {
    $T_{1}$//%first subgraph
      {[name=left]R->{a->{b,c},d}};
    $T_{2}$[xshift=-1em]//% second subgraph
      {[name=right]R->{a/a'->{bl/b,br/b},d->{e,f}}};
  };
  \foreach \n in {R,a,d}
    \draw[dashed](left \n)to[out=30,in=150](right \n);
  \foreach \n in {l,r}
    \draw[dashed](left b)to[out=330,in=210](right b\n);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容