使用绘制边缘在 TikZ 中移动边缘标签

使用绘制边缘在 TikZ 中移动边缘标签

我刚开始使用 LaTeX 和 TikZ,不知道如何将标签移到边缘上方。我在下面附上了我的代码和结果图片。我想将“5”标签移到其对应边缘上方,以减少歧义。我尝试了above括号中的选项(以及其他选项),"5"但标签仍位于边缘的同一侧。提前致谢!

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, quotes}
\begin{document}

\begin{tikzpicture}[auto, node distance = 5cm]

\node (1) {A};
\node (2) [below left of=1] {B};
\node (3) [below right of=2] {C};
\node (4) [below right of=1] {D};

\tikzset{edge/.style = {line width = 1.0, ->}}

\draw[edge] [loop above] (1) to ["1"] (1);
\draw[edge] (1) to ["5"] (2);
\draw[edge] [bend right] (1) to ["2"] (3);
\draw[edge] [bend left] (1) to ["-4"] (4);

\draw[edge] [bend left] (2) to ["3"] (1);
\draw[edge] [loop left] (2) to ["4"] (2);
\draw[edge] (2) to ["5"] (3);
\draw[edge] [bend right] (2) to ["7"] (4);

\draw[edge] [bend right] (3) to ["8"] (1);
\draw[edge] [bend left] (3) to ["9"] (2);
\draw[edge] [loop below] (3) to ["6"] (3);
\draw[edge] (3) to ["5"] (4);

\draw[edge] (4) to ["5"] (1);
\draw[edge] [bend right] (4) to ["10"] (2);
\draw[edge] [bend left] (4) to ["11"] (3);
\draw[edge] [loop right] (4) to ["12"] (4);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

如果在引用的标签后面加上单引号,它将“翻转”到行的另一侧,例如从右到左或从上到下。

我已更新您的代码以指定arrows.meta库和positioning库的语法。我还将其更改edgemy edgeedge这非常令人困惑,因为它已被 Ti 广泛使用Z.至少,它确实让人困惑. 不过,也许我只是容易困惑。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,quotes,positioning}
\begin{document}
\begin{tikzpicture}[auto, node distance = 5cm]

  \node (1) {A};
  \node (2) [below left=of 1] {B};
  \node (3) [below right=of 2] {C};
  \node (4) [below right=of 1] {D};

  \tikzset{my edge/.style = {line width = 1.0, ->}}

  \draw[my edge] [loop above] (1) to ["1"] (1);
  \draw[my edge] (1) to ["5"'] (2);
  \draw[my edge] [bend right] (1) to ["2"] (3);
  \draw[my edge] [bend left] (1) to ["-4"] (4);

  \draw[my edge] [bend left] (2) to ["3"] (1);
  \draw[my edge] [loop left] (2) to ["4"] (2);
  \draw[my edge] (2) to ["5"'] (3);
  \draw[my edge] [bend right] (2) to ["7"] (4);

  \draw[my edge] [bend right] (3)  to ["8"] (1);
  \draw[my edge] [bend left] (3) to ["9"] (2);
  \draw[my edge] [loop below] (3) to ["6"] (3);
  \draw[my edge] (3) to ["5"'] (4);

  \draw[my edge] (4) to ["5"'] (1);
  \draw[my edge] [bend right] (4) to ["10"] (2);
  \draw[my edge] [bend left] (4) to ["11"] (3);
  \draw[my edge] [loop right] (4) to ["12"] (4);

\end{tikzpicture}
\end{document}

翻转五:

翻转五

答案2

只是代码的小变化成本加运费回答:

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows.meta, positioning, quotes}

\begin{document}
\begin{tikzpicture}[auto, node distance = 5cm, >=Stealth]
  \node (1) {A};
  \node (2) [below left=of 1] {B};
  \node (3) [below right=of 2] {C};
  \node (4) [below right=of 1] {D};

  \draw[line width = 1.0, ->]  
    (1) edge [loop above, "1"] (1)
    (1) edge ["5"'] (2)
    (1) edge [bend right,"2"] (3)
    (1) edge [bend left,"-4"] (4)

    (2) edge [bend left,"3"] (1)
    (2) edge [loop left,"4"] (2)
    (2) edge ["5"'] (3)
    (2) edge [bend right,"7"] (4)

    (3) edge [bend right,"8"] (1)
    (3) edge [bend left,"9"] (2)
    (3) edge [loop below,"6"] (3)
    (3) edge ["5"'] (4)

    (4) edge ["5"'] (1)
    (4) edge [bend right, "10"] (2)
    (4) edge [bend left, "11"] (3)
    (4) edge [loop right, "12"] (4);

\end{tikzpicture}
\end{document}

结果几乎是一样的:

在此处输入图片描述

相关内容