tikz马尔可夫链图中的箭头重叠

tikz马尔可夫链图中的箭头重叠

我正在尝试使用 绘制马尔可夫链tikz。该图的设置正确,只是从状态 2 和 3 指向的箭头与另外两个箭头重叠。我尝试使用 重新定位状态,node distance但似乎不起作用。如何强制箭头不重叠? 马尔可夫链

%latex
\documentclass[reqno]{amsart}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween}

\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary{positioning}  %                 ...positioning nodes
\usetikzlibrary{arrows}       %                 ...customizing arrows
\tikzset{node distance=4.5cm, % Minimum distance between two nodes. Change if necessary.
         every state/.style={ % Sets the properties for each state
           semithick,
           fill=gray!10},
         initial text={},     % No label on start arrow
         double distance=4pt, % Adjust appearance of accept states
         every edge/.style={  % Sets the properties for each transition
         draw,
           ->,>=stealth',     % Makes edges directed with bold arrowheads
           auto,
           semithick}}

\begin{document}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\node[state] (s1) {State 1};
\node[state, below right of=s1] (s2) {State 2};
\node[state, below left of=s1] (s3) {State 3};

\draw (s1) edge[loop above] node {} (s1);
\draw (s1) edge[bend left] node {} (s2);
\draw (s1) edge[bend right] node {} (s3);

\draw (s2) edge[bend left] node {} (s1);
\draw (s2) edge[loop right] node {} (s2);
\draw (s2) edge[bend right] node {} (s3);

\draw (s3) edge[bend right] node {} (s1);
\draw (s3) edge[bend right] node {} (s2);
\draw (s3) edge[loop left] node {} (s3);

\end{tikzpicture}
\end{figure}

\end{document}

答案1

bend leftbend right带有参数,即弯曲角度。调整它们可以让你避免交叉。(顺便说一句,我还删除了未使用的包。还请注意,该arrows库已被取代,arrows.meta但我arrows暂时保留了下来。)

\documentclass[reqno]{amsart}
\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary{positioning}  %                 ...positioning nodes
\usetikzlibrary{arrows}       %                 ...customizing arrows
\tikzset{node distance=4.5cm, % Minimum distance between two nodes. Change if necessary.
         every state/.style={ % Sets the properties for each state
           semithick,
           fill=gray!10},
         initial text={},     % No label on start arrow
         double distance=4pt, % Adjust appearance of accept states
         every edge/.style={  % Sets the properties for each transition
         draw,
           ->,>=stealth',     % Makes edges directed with bold arrowheads
           auto,
           semithick}}

\begin{document}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\node[state] (s1) {State 1};
\node[state, below right of=s1] (s2) {State 2};
\node[state, below left of=s1] (s3) {State 3};

\draw (s1) edge[loop above]  (s1);
\draw (s1) edge[bend left]  (s2);
\draw (s1) edge[bend right]  (s3);

\draw (s2) edge[bend left=12]  (s1);
\draw (s2) edge[loop right]  (s2);
\draw (s2) edge[bend right=12]  (s3);

\draw (s3) edge[bend right=12]  (s1);
\draw (s3) edge[bend right]  (s2);
\draw (s3) edge[loop left]  (s3);

\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案2

您可以减少弯曲角度的默认值。只需添加bend angle=15到您的tikzset(类似@marmoth 将其本地更改为两个箭头弯曲)。

无关:

  • 对于箭头的标记来说,使用quotes库很方便,然后将其写成示例... (s1) edge["label",bend left] (s2)
  • 软件包hyperref必须在前言中最后加载(极少数情况除外)

    \documentclass[reqno]{amsart}
    \usepackage{amsmath, amssymb}
    
    \usepackage{pgfplots}         % it load tikz too
    \pgfplotsset{compat=1.16}
    \usetikzlibrary{automata,
                    arrows.meta,    %   ...customizing arrows
                    positioning,    %   ...positioning nodes
                    quotes}         % For edge labels
    \usepgfplotslibrary{fillbetween}
    \tikzset{node distance=4.5cm,   % Minimum distance between nodes. Change if necessary.
             every state/.style={   % Sets the properties for each state
                    semithick,
                    fill=gray!10},
             initial text={},       % No label on start arrow
             double distance=4pt,   % Adjust appearance of accept states
             every edge/.style={    % Sets the properties for each transition
                    draw,
                    semithick,
                    -Stealth,       % Makes edges directed with bold arrowheads
                    auto},
             bend angle=15          % Reduce default bend angle
             }
    
    \usepackage{hyperref}           % had to be last in preamble
    
    \begin{document}
        \begin{figure}[htb]
        \centering
    \begin{tikzpicture}[]
    \node[state] (s1) {State 1};
    \node[state, below right of=s1] (s2) {State 2};
    \node[state, below left of=s1] (s3) {State 3};
    
    \draw   (s1) edge[loop above]   (s1)
            (s1) edge[bend left]    (s2)
            (s1) edge[bend right]   (s3)
    %
            (s2) edge[bend left]    (s1)
            (s2) edge[loop right]   (s2)
            (s2) edge[bend right]   (s3)
    %
            (s3) edge[bend right]   (s1)
            (s3) edge[bend right]   (s2)
            (s3) edge[loop left]    (s3);
    \end{tikzpicture}
        \end{figure}
    \end{document}
    

在此处输入图片描述

相关内容