在 tikz 中移动箭头描述/标签,使它们不重叠

在 tikz 中移动箭头描述/标签,使它们不重叠

我正在使用这段命令:

\begin{figure}
    \centering
    \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
                thick,main node/.style={circle,draw,font=\Large\bfseries}]
        \node[main node] (S) {$S$};
        \node[main node] (R) [below left of=S] {$R$};
        \node[main node] (B) [below right of=S] {$B$};

        \path
        (S) edge [loop above] node {0.4} (S)
            edge [bend right] node {0.2} (R)
            edge node [above] {0.4} (B)
        (R) edge node [below] {0.15} (S)
            edge [loop left] node {0.5} (R)
            edge node [below] {0.35} (B)
        (B) edge [bend right] node {0.2} (S)
            edge [bend left] node {0.35} (R)
            edge [loop right] node {0.45} (B);
  
        
    
    \end{tikzpicture}
    \caption {Your caption here}
    \label{fig:your _ label _ here}
    
\end{figure}

结果如下:

在此处输入图片描述

如您所见,箭头上的标签不仅相互重叠,而且箭头本身也重叠。我该如何避免这种情况?

答案1

您可以swap为此目的使用:

在此处输入图片描述

注意:考虑从arrows库更改为arrows.meta

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

    \begin{tikzpicture}[->,>=stealth,shorten >=1pt,auto,node distance=4cm,
                thick,main node/.style={circle,draw,font=\Large\bfseries}]
        \node[main node] (S) {$S$};
        \node[main node] (R) [below left of=S] {$R$};
        \node[main node] (B) [below right of=S] {$B$};
        \path
        (S) edge [loop above] node {0.4} (S)
            edge [bend right] node[swap] {0.2} (R)
            edge node [swap] {0.4} (B)
        (R) edge node [swap] {0.15} (S)
            edge [loop left] node {0.5} (R)
            edge node [above] {0.35} (B)
        (B) edge [bend right] node[swap] {0.2} (S)
            edge [bend left] node {0.35} (R)
            edge [loop right] node {0.45} (B);   
    \end{tikzpicture}

\end{document}

答案2

为了好玩,有一种方法可以获得该图表pstricks– 更准确地说是使用psmatrix以下环境pst-node

\documentclass{article}
\usepackage{pst-node}

\begin{document}

\begin{psmatrix}[rowsep=2cm]
%%% Matrix nodes
 & \circlenode{S}{S} \\
\circlenode{R}{R} & & \circlenode{B}{B}
%% Line Arrows
\psset{arrows=->, arrowinset=0.2, nodesep=0pt, nrot=:U}
\ncline{R}{S} \nbput{0.15}
\ncline{R}{B} \naput{0.35}
\ncline{S}{B} \nbput{0.35}
%% Curved Arrows
\psset{arcangle=30, arrows=<- }
\ncarc{R}{S} \naput{0.2}
\ncarc{S}{B} \naput{0.2}
\ncarc[arcangle=-30]{R}{B} \nbput{0.35}
\end{psmatrix}

\end{document} 

在此处输入图片描述

答案3

通过使用quotes库和'作为swap选项的缩写:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}
\usepackage{bm}

\begin{document}
    \begin{tikzpicture}[
node distance = 4cm and 3cm,
     N/.style = {circle, draw, font=\Large, 
                 minimum size=1.1em},
       every edge/.style = {draw, -Stealth, semithick, shorten >=1pt},
every edge quotes/.style = {auto=right, inner sep=2pt}
                        ]
\node[N] (S) {$\bm{S}$};
\node[N] (R) [below left of=S] {$\bm{R}$};
\node[N] (B) [below right of=S] {$\bm{B}$};
%
\path   (S) edge [loop above,"0.4" ']   ()
            edge [bend right,"0.2"]     (R)
            edge ["0.4"]                (B) 
        (R) edge [loop left,"0.5" ']    ()
            edge ["0.15"]               (S)
            edge ["0.35"]               (B)
        (B) edge [loop right,"0.45" ']  ()
            edge [bend right,"0.2"]     (S)
            edge [bend left,".35"]      (R)
            ;
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容