无法向图中添加边

无法向图中添加边
\begin{figure}[H]
        \centering
        
        \begin{tikzpicture}[
        C/.style = {circle, draw}, %   C: circle
        every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
        ]
        \begin{scope}[nodes={C}]
        \node (a1)   at (0,0)        {$v_2$};
        \node (a3) at (5,0)  {$v_4$};
        \node (a2) at (3,1) {$v_3$};
        \node (a0) at (2,-2)  {$v_1$};
        \node (a4) at (3,-1)  {$v_5$};
        \node (a6) at (2,2)  {$v_6$};
        \end{scope}
        %
        \path 
        (a1) edge ["\hspace{40mm} $e_{23}$"]  (a2)
        (a1) edge ["$e_{24}$"]  (a3)
        (a1) edge ["$e_{25}$"]  (a4)
        (a1) edge ["$e_{26}$"]  (a6)
        (a1) edge ["$e_{21}$"]  (a0);
        
    \end{tikzpicture}
\end{figure}

为什么我不能在这个 tikz 图片中的顶点 $3$ 和 $4$ 之间添加边?我不知道为什么当我在顶点 $v_3$ 和 $v_4$ 之间添加带有边标签 $e_{34}$ 的边时,新边不会显示出来,并且整个图形会受到干扰。

我哪里做错了?有人能帮我吗?

答案1

一般来说,为了标记边,你基本上需要沿着边的路径放置一个节点。无需加载任何 TiZ 库,可以按如下方式完成:

\documentclass[tikz,border=1pt]{standalone}

\begin{document}

    \begin{tikzpicture}[
    C/.style = {circle, draw}, %   C: circle
    every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
    ]
    
    \begin{scope}[nodes={C}]
    \node (a1) at (0,0)  {$v_2$};
    \node (a3) at (5,0)  {$v_4$};
    \node (a2) at (3,1)  {$v_3$};
    \node (a0) at (2,-2) {$v_1$};
    \node (a4) at (3,-1) {$v_5$};
    \node (a6) at (2,2)  {$v_6$};
    \end{scope}
    
    \path 
    (a1) edge node {\hspace{40mm} $e_{23}$} (a2)
    (a1) edge node[below, pos=.75] {$e_{24}$} (a3)
    (a1) edge node[below] {$e_{25}$} (a4)
    (a1) edge node[below, xshift=5pt] {$e_{26}$} (a6)
    (a1) edge node[below, yshift=-3pt] {$e_{21}$} (a0);
    
    \end{tikzpicture}

\end{document}

您可以使用选项abovebelow将标签节点放置在边缘上方或下方。默认情况下,它位于边缘起点和终点之间的中间,但您可以使用选项pos来移动它(其中pos=0表示在起点和终点)。您可能希望使用和pos=1额外移动标签。xshiftyshift

然而,你可能从使用 TiZ 库quotes。如果你想使用它,你需要在序言中加载此库:

\documentclass[tikz,border=1pt]{standalone}

\usetikzlibrary{quotes} % <-- add this!

\begin{document}

    \begin{tikzpicture}[
        C/.style = {circle, draw}, %   C: circle
        every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
        ]
        \begin{scope}[nodes={C}]
        \node (a1)   at (0,0)        {$v_2$};
        \node (a3) at (5,0)  {$v_4$};
        \node (a2) at (3,1) {$v_3$};
        \node (a0) at (2,-2)  {$v_1$};
        \node (a4) at (3,-1)  {$v_5$};
        \node (a6) at (2,2)  {$v_6$};
        \end{scope}
        %
        \path 
        (a1) edge ["$e_{23}$"{xshift=5pt}]  (a2)
        (a1) edge ["$e_{24}$"]  (a3)
        (a1) edge ["$e_{25}$"]  (a4)
        (a1) edge ["$e_{26}$"]  (a6)
        (a1) edge ["$e_{21}$"]  (a0);
        
    \end{tikzpicture}

\end{document}

您仍然可以使用上述选项来移动节点。请参阅上例中(a1)节点之间的边的示例。(a2)

在此处输入图片描述

相关内容