tikz 中带边界的边缘标签

tikz 中带边界的边缘标签

我有以下图表,我想用 Tikz 复制它。我无法创建方形边缘标签容器。有没有简单的方法可以在 tikz 中做到这一点? 在此处输入图片描述

编辑

我结合@John Kormylo 的建议做了一个小例子:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\begin{document}
\tikzstyle{circ} = [circle, draw, thick, text centered, minimum size=3em, align=left]

    \begin{tikzpicture}[->,>=stealth',node distance = 3cm, thick, auto]
    \node [circ] (s1) {1};
    \node [circ, right =of s1] (s2) {2};
    \node [circ, below = of s1] (s3) at ($(s1)!0.5!(s2)$) {3};

    \path[every node/.style={}]
    (s1) edge[bend left=20] node [very near start] {c} node [midway, below, draw, sloped] {1} node [very near end] {e}  (s2)
    (s1) edge[bend right=20] node [very near start,swap] {d} node [midway, below, draw, sloped] {2} node [very near end,swap] {f}  (s3)
    (s2) edge[bend left=20] node [very near start] {i} node [midway, below, draw, sloped] {3} node [very near end,swap] {g}  (s3);

\end{tikzpicture}

\end{document}

结果如下:

在此处输入图片描述

现在我只需要一种方法来旋转方块内的实际标签。有什么想法吗?

答案1

您可以通过添加节点文本来label避免其倾斜。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\begin{document}

    \begin{tikzpicture}[circ/.style={circle, draw, thick, text centered,
        minimum size=3em, align=left},
        >=Stealth,node distance = 3cm, thick, auto,
        box/.style 2 args={edge node={node[sloped,draw,minimum
        size=1.5em,#1,label={[anchor=center]center:#2}]{}}}
        ]
    \node [circ] (s1) {1};
    \node [circ, right =of s1] (s2) {2};
    \node [circ, below = of s1] (s3) at ($(s1)!0.5!(s2)$) {3};

    \path[every node/.style={},->]
    (s1) edge[bend left=20,box={midway,below}{1}]  node [very near start] {c} 
    node [very near end] {e}  (s2)
    (s1) edge[bend right=20,box={midway,below}{2}] node [very near start,swap] {d}  node [very near end,swap] {f}  (s3)
    (s2) edge[bend left=20,box={midway,below}{3}] node [very near start] {i}  node [very near end,swap] {g}  (s3);
\end{tikzpicture}

\end{document}

在此处输入图片描述

由于您有如此多的边缘标签,因此您可以使用该quotes库。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending,quotes}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[circ/.style={circle, draw, thick, text centered,
        minimum size=3em, align=left},
        >={Stealth[bend]},node distance = 3cm, thick, auto,
        box/.style={draw,minimum size=1.5em,label={[anchor=center]center:#1}}]
  \node [circ] (s1) {1};
  \node [circ, right =of s1] (s2) {2};
  % the usage below is unusual but efficient
  \node [circ, below = of s1] (s3) at ($(s1)!0.5!(s2)$) {3};

  \path[->]
    (s1) edge[bend left=20,"c" very near start,""' {box=1},"e" very near end] (s2)
    (s1) edge[bend right=20,"d" very near start,""' {sloped,box=2},"e" very near end](s3)
    (s2) edge[bend left=20,"i" very near start,""' {sloped,box=3},"g"' very near end](s3);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容