如何改变有向图中节点权重的位置

如何改变有向图中节点权重的位置

我使用以下乳胶代码来绘制这个有向图

\documentclass[12pt]{article}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\begin{document}
\begin{tikzpicture}
       [> = stealth,
        shorten > = 1pt, 
        auto,
        node distance = 2cm, 
        thick ]

    \tikzstyle{every state}=[
        draw = black,
        thick,
        fill = white,
        minimum size = 2mm]

    \node[state] (v1) {$v_1$};
     \node[state] (v2) [right of=v1] {$v_2$};
       \path[->] (v2) edge node {$1$} (v1);
      \node[state] (v3) [right of=v2] {$v_3$};
        \path[->] (v3) edge node {$1$} (v2);
      \node[state] (v4) [right of=v3] {$v_4$};
       \path[->] (v4) edge node {$1$} (v3);
     \node[state] (v5) [right of=v4] {$v_5$};
       \path[->] (v5) edge node {$1$} (v4);
     \node[state] (v6) [right of=v5] {$v_6$};
       \path[->] (v6) edge node {$1$} (v5);

         \path (v1) edge [loop left] node {$u_1$} (v1);
        \path[->] (v1) edge[bend left=30] node {$u_2$}  (v2);
         \path[->] (v1) edge[bend left=40] node  {$u_3$}  (v3);
          \path[->] (v1) edge[bend left=50] node {$u_4$}  (v4);
           \path[->] (v1) edge[bend left=55] node {$u_5$}  (v5);
            \path[->] (v1) edge[bend left=60] node  {$u_6$}  (v6);

   \end{tikzpicture}
   \end{document}

我将非常感激改进我的乳胶代码,使得代码的输出类似于提及的有向图. 实际上我的问题是想要变小的圆的大小以及$u_i$节点的循环的位置v_1

谢谢您的任何建议。

答案1

我喜欢使用quotes库来制作边缘标签,因为它提供的语法(source) edge["label"] (target)比 更短(source) edge node {label} (target)。控制标签位置的选项是pos;我认为pos=0.8会产生合理的输出,但您可以自己摆弄它。我还使用了一个\foreach循环来简化您的第一段代码。

\documentclass[12pt]{article}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata,quotes} % quotes library adds quotes syntax
\begin{document}
\begin{tikzpicture}
       [> = stealth,
        shorten > = 1pt, 
        auto,
        node distance = 2cm, 
        thick , 
        % automatically put nodes in math mode:
        execute at begin node=$,execute at end node=$ 
]

    \tikzset{every state/.style={thick,minimum size = 2mm}}

  \node[state] (v1) {v_1};  
    % Use a loop to make the states. \lastn holds \n minus 1 
  \foreach \n[remember=\n as \lastn (initially 1)] in {2,...,6}{
         \node[state] (v\n) [right of=v\lastn] {v_\n};
         \draw[<-] (v\lastn) edge["1"'] (v\n);
    }


        \draw (v1) edge [loop left,"u_1"]  (v1);
  % Put all the rest of the labels 8/10ths of the way across the edge
    \tikzset{every edge quotes/.style={pos=0.8}}
        \draw[->] (v1) edge[bend left=30,"u_2" ] (v2);
        \draw[->] (v1) edge[bend left=40,"u_3"]  (v3);
        \draw[->] (v1) edge[bend left=50,"u_4"]  (v4);
        \draw[->] (v1) edge[bend left=55,"u_5"]  (v5);
        \draw[->] (v1) edge[bend left=60,"u_6"] (v6);

   \end{tikzpicture}
   \end{document}

答案2

稍微修改一下就好了胡德查塔姆的回答(大部分修改都是题外话):

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

\begin{document}
\begin{tikzpicture}
       [shorten > = 1pt,
        auto,
        node distance = 2cm,
        thick ,
        execute at begin node=$,execute at end node=$,% put nodes in math mode ...
    every state/.style = {thick, minimum size=4mm, inner sep=1mm},  % <--- added
    every  edge/.style = {draw, -stealth},                          % <--- added 
        ]
  \node[state] (v1) {v_1};
  % Use a loop to make the states. \lastn holds \n minus 1
  \foreach \n [remember=\n as \lastn (initially 1)] in {2,...,6}%
  {
    \node[state] (v\n) [right of=v\lastn] {v_\n};
    \draw (v\n) edge["1"] (v\lastn);                                % <--- changed
  }
  \draw (v1) edge [out=135, in=225, looseness=8, "u_1" ']  (v1);    % <--- added
  % bended edges
  \foreach \i [evaluate=\i as \a using 30 + 6*\i] in {2,...,6}      % <--- added
  \draw (v1) edge[inner sep=1pt, pos=0.75, bend left=\a, "u_\i"]    (v\i);  % <--- modified
   \end{tikzpicture}
   \end{document}

在此处输入图片描述

附录:

如果我正确理解了你的评论,你喜欢将第一个解决方案更改为如下内容:

在此处输入图片描述

姆韦以下是与上文相比的变化姆韦表示为% <--- new

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, 
                chains,                                             % <--- new
                positioning,                                        % <--- new
                quotes} 

\begin{document}
\begin{tikzpicture}
       [shorten > = 1pt,
        auto,
        node distance = 2cm,
        start chain = grow right,                                   % <--- new
        thick ,
        execute at begin node=$,execute at end node=$,% put nodes in math mode ...
    every state/.style = {thick, minimum size=4mm, inner sep=1mm,
                          on chain},                                % <--- new
    every  edge/.style = {draw, -stealth},                          
        ]
  \foreach \i [count=\j from 1] in {1,2,3,n}                        % <--- changed
    \node (v\j) [state, on chain] {v_\i};                           % <--- changed
  \foreach \i [count=\j from 1] in {2,...,4}                        % <--- new
  {
    \ifnum\i<4                                                      % <--- new
    \draw (v\i) edge["1"] (v\j);                                    % <--- new
    \else                                                           % <--- new
    \draw[-, very thick, dotted,                                    % <--- new
          shorten <=1mm] (v\i) -- (v\j);                            % <--- new
    \fi                                                             % <--- new
  }
  % bended edges
  \foreach \i [count=\j from 2,                                     % <--- new
               evaluate=\j as \a using 30 + 6*\j] in {2,3,n}        % <--- changed
  \draw (v1) edge[inner sep=1pt, pos=0.75, bend left=\a, "u_\i"]    (v\j);  
  \draw (v1) edge [out=135, in=225, looseness=8, "u_1" ']  (v1);    
\end{tikzpicture}
\end{document}

相关内容