如何在 tikzpicture 中用边绘制节点内部

如何在 tikzpicture 中用边绘制节点内部

在此处输入图片描述

我如何使用绘制这样的图形tikzpicture

答案1

定义pics如下第 18 章蒂克兹手动操作绝对是最佳选择。下面的代码定义了两个图片,2box以及3box分别包含两个或三个圆圈或正方形的框。2box稍微简单一些,因为它只需要四个参数:两个圆圈中的标签以及箭头上方和下方的标签。使用这个,您可以使用以下方法绘制顶部框

\pic at (0,0) {2box={r,a,1{\mid}0,01}};

定义pic了以下有用的节点:

  • ra整个盒子
  • ra-r第一个圆圈
  • ra-a第二个圆圈

这部分是为了绘制图片,部分是为了稍后绘制箭头。3box图片类似,但它需要七个参数:三个节点标签和两个箭头上方和下方的四个标签。

有了这些,接下来只需使用2box和绘制框3box,然后绘制边缘。起初,我考虑使用positioning库来放置节点,但遗憾的是,picspositioning不能很好地协同工作,所以我手动放置了所有内容以生成:

在此处输入图片描述

为了便于更改,我应用了大量样式。以下是完整代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{
  arrow/.style = {-{LaTeX}, thick},
  label/.style = {pos=0.4, font=\scriptsize},
  mynode/.style = {fill=white, inner sep=3pt, thick, draw=black},
  onode/.style = {circle, mynode},
  snode/.style = {rectangle, mynode},
  box/.style = {thick, fill=gray!50, draw=black, anchor=south west},
  pics/2box/.style args = {#1,#2,#3,#4}{% a,b,top,bot
    code = {
        \node[box, minimum width=2cm, minimum height=1cm](#1#2) at (0,0){};
        \node[onode] (#1#2-#1) at (0.4,0.5){#1};
        \node[onode] (#1#2-#2) at (1.6,0.5){#2};
        \draw[arrow](#1#2-#1) -- node[label,above]{$#3$}
                                 node[label,below]{$#4$} (#1#2-#2);
    }
  },
  pics/3box/.style args = {#1,#2,#3,#4,#5,#6,#7}{% a,b,c,top,bot,top,bot}
    code = {
        \node[box, minimum width=2cm, minimum height=2cm](#1#2#3) at (0,0){};
        \node[snode] (#1#2#2-#1) at (0.4,1.5){#1};
        \node[snode] (#1#2#2-#2) at (0.4,0.5){#2};
        \node[onode] (#1#2#2-#3) at (1.6,1){#3};
        \draw[arrow](#1#2#2-#1.east) to[out=20, in=135]
            node[label,above]{$#4$} node[label,below]{$#5$} (#1#2#2-#3);
        \draw[arrow](#1#2#2-#2.east) to[out=-20, in=225]
            node[label,above]{$#6$} node[label,below]{$#7$} (#1#2#2-#3);
    }
  },
}

\begin{document}

  \begin{tikzpicture}
    \pic at (0,9) {2box={r,a,1{\mid}0,01}};
    \pic at (0,6) {3box={b,q,m,1{\mid}0,02,2{\mid}0,02}};
    \pic at (1,4) {2box={q,b,1{\mid}0,03}};
    \pic at (0,2) {2box={e,p,1{\mid}2,04}};
    \pic at (0,0) {2box={q,e,1{\mid}0,05}};
    \draw[arrow] (ra.south)--(bqm.north);
    \draw[arrow] (bqm.south)--(qb.north);
    \draw[arrow] (bqm) to[out=250, in=120](ep);
    \draw[arrow] (ep.south)--(qe.north);
    \draw[arrow] (qe.south)--++(0,-0.5);
    \draw[thick](0.5,10.6) -- ++(0,-0.1)--++(1,0)--++(0,0.1);
    \draw[{LaTeX}-,thick](ra.north)--++(0,0.5);
    \draw[arrow,rounded corners]
    (2.5,4)--++(0,-0.3)--++(1,0)--++(0,6.65)--++(-2,0)--++(0,-0.35);
  \end{tikzpicture}

\end{document}

2box正如你所见,和的定义3box只是相当相似的小部分蒂克兹代码。里面用的是相对坐标的定义pics。我把\delta标签留作练习。

相关内容