Tikz:在状态图中绘制双向箭头

Tikz:在状态图中绘制双向箭头

我想绘制像这样的状态图: 状态图

我正在使用以下代码:

\documentclass[tikz]{standalone}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}
\node[state]                               (0) {0};
\node[state,right=of 0]                    (1) {1};
\node[state,right=of 1]                    (2) {2};
\coordinate[draw=none,right=of 2]          (2-g);
\node[state,right=of {2-g},text depth=0pt] (g) {g};

\draw[
    >=latex,
%   every node/.style={above,midway},% either
    auto=right,                      % or
    loop above/.style={out=75,in=105,loop},
    every loop,
    ]
     (g)   edge[loop above] node {$p_{gg}$}   (g)
       edge             node {$p_{gg-1}$} (2-g)
     (2-g) to               node {$p_{32}$}   (2)
       edge[loop above] node {$p_{22}$}   (2)
     (2)   edge             node {$p_{21}$}   (1)
     (1)   edge[loop above] node {$p_{11}$}   (1)
       edge             node {$p_{10}$}   (0)
     (0)   edge[loop above] node {$p_{00}$}   (0);
\end{tikzpicture}
\end{document}

但我希望所有箭头都是双向的。我该怎么做?谢谢。

答案1

我们只需要调整三种样式:

  • 路径绘制的风格\draw
  • edges绘画的风格\draw
  • loop边缘绘制的风格\draw

我们可以附加<->\draw命令并使用.append style对样式执行相同的操作every loopevery edge

  \draw[
    >=latex,
    auto=right,
    loop above/.style={out=75,in=105,loop},
    every loop/.append style={<->},
    every edge/.append style={<->},
    <->
  ]
  ...

双重麻烦

完整代码:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}
  \node[state]                               (0) {0};
  \node[state,right=of 0]                    (1) {1};
  \node[state,right=of 1]                    (2) {2};
  \coordinate[draw=none,right=of 2]          (2-g);
  \node[state,right=of {2-g},text depth=0pt] (g) {g};
  \draw[
    >=latex,
    auto=right,
    loop above/.style={out=75,in=105,loop},
    every loop/.append style={<->},
    every edge/.append style={<->},
    <->
  ]
  (g)   edge[loop above] node {$p_{gg}$}   (g)
  edge             node {$p_{gg-1}$} (2-g)
  (2-g) to               node {$p_{32}$}   (2)
  edge[loop above] node {$p_{22}$}   (2)
  (2)   edge             node {$p_{21}$}   (1)
  (1)   edge[loop above] node {$p_{11}$}   (1)
  edge             node {$p_{10}$}   (0)
  (0)   edge[loop above] node {$p_{00}$}   (0);
\end{tikzpicture}
\end{document}

相关内容