在有向图中,如何将字母放置在节点上?

在有向图中,如何将字母放置在节点上?

查询链接

在有向图中,如何将字母放置在 MWE T1 上方以放置字母 p1。我试过,但字母走得很远,我需要在不干扰任何其他节点的情况下放置它,以及如何省略链接号。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, automata}
\begin{document}
\begin{figure}
\begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=2cm,on grid,initial/.style={}]
    \node[state] (T1) {$T1$};
    \node[state] (T4) [below =of T1] {$T4$};
    \node[state] (T3) [left  =of T4] {$T3$};
    \node[state] (T2) [left  =of T3] {$T2$};
    \node[state] (T5) [right =of T4] {$T5$};
    \node[state] (T6) [right =of T5] {$T6$};
    \node[state] (T8) [below =of T4] {$T8$};
    \node[state] (T7) [left  =of T8] {$T7$};
    \node[state] (T9) [right =of T8] {$T9$};
    \node[state] (T10) [below =of T8] {$T10$};
    \node [above =of T1] {$P1$};
    \node [above =of T2] {$P1$};
    \node [above =of T3] {$P2$};
    \node [above =of T4] {$P3$};
    \node [above =of T5] {$P3$};
    \node [above =of T6] {$P3$};
    \node [above =of T7] {$P2$};
    \node [above =of T8] {$P1$};
    \node [above =of T9] {$P2$};
    \node [above =of T10] {$P2$};
    \tikzset{mystyle/.style={->,double=black}}
    \tikzset{every node/.style={fill=white}}
    \path (T1) edge [mystyle] node {$18$} (T2);
    \path (T1) edge [mystyle] node {$12$} (T3);
    \path (T1) edge [mystyle] node {$9$}  (T4);
    \path (T1) edge [mystyle] node {$11$} (T5);
    \path (T1) edge [mystyle] node {$14$} (T6);
    \path (T2) edge [mystyle] node[pos=0.7] {$19$} (T8);
    \path (T2) edge [mystyle] node {$16$} (T9);
    \path (T3) edge [mystyle] node[pos=0.7] {$23$} (T7);
    \path (T4) edge [mystyle] node {$27$} (T8);
    \path (T4) edge [mystyle] node {$23$} (T9);
    \path (T5) edge [mystyle] node {$13$} (T9);
    \path (T6) edge [mystyle] node {$15$} (T8);
    \path (T7) edge [mystyle] node {$17$} (T10);
    \path (T8) edge [mystyle] node {$11$} (T10);
    \path (T9) edge [mystyle] node {$13$} (T10);
\end{tikzpicture}

\end{figure}
\end{document} 

在此处输入图片描述

答案1

你想要这样的东西吗?

连接节点

作为Torbjørn T. 说您可以使用label来放置标记主节点的节点。您需要稍微改变位置以避免冲突。我不确定 的意义是什么double=black。如果您还有 ,那就更有意义了draw=white。我已更新箭头规范以使用当前库。

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows.meta, positioning, automata}
\begin{document}
\begin{tikzpicture}[>={Stealth[color=black]},shorten >=1pt,node distance=2cm,on grid,initial/.style={}]
  \node[state, label=above:$P1$] (T1) {$T1$};
  \node[state, label=95:$P3$] (T4) [below =of T1] {$T4$};
  \node[state, label=above:$P2$] (T3) [left  =of T4] {$T3$};
  \node[state, label=above:$P1$] (T2) [left  =of T3] {$T2$};
  \node[state, label=above:$P3$] (T5) [right =of T4] {$T5$};
  \node[state, label=above:$P3$] (T6) [right =of T5] {$T6$};
  \node[state, label=120:$P1$] (T8) [below =of T4] {$T8$};
  \node[state, label=95:$P2$] (T7) [left  =of T8] {$T7$};
  \node[state, label=85:$P2$] (T9) [right =of T8] {$T9$};
  \node[state, label=95:$P2$] (T10) [below =of T8] {$T10$};
  \begin{scope}[every edge/.append style={->, double=black, draw=white}]
    \path (T1)
      edge   (T2)
      edge   (T3)
      edge   (T4)
      edge   (T5)
      edge   (T6);
    \path (T2)
      edge   (T8)
      edge   (T9);
    \path (T3) edge   (T7);
    \path (T4) edge   (T8);
    \path (T4) edge   (T9);
    \path (T5) edge   (T9);
    \path (T6) edge   (T8);
    \path (T7) edge   (T10);
    \path (T8) edge   (T10);
    \path (T9) edge   (T10);
  \end{scope}
\end{tikzpicture}
\end{document}

相关内容