如何绘制两个独立的有向边

如何绘制两个独立的有向边

我正在用 tikz 绘制一个简单的图形。这可以正常工作,但是当我在一对节点之间有两条朝相反方向延伸的边时,我会得到一个有两个头的箭头(例如在下面的节点 B 和 D 之间)。我怎样才能得到两个箭头?这是一个 MWE:

\documentclass[]{article}
\usepackage{tikz}  
\usetikzlibrary{arrows.meta,positioning}
\usepackage{tkz-graph}
\usepackage{graphicx}  
\begin{document}  
\section{}
\begin{center}
    \begin{tikzpicture}[auto,node distance=2cm,thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}, scale=2]
    \node[main node, label=$0$] (A) at (0,5) {A};
    \node[main node, label=$\infty$] (B) at (2,6.5) {B};
    \node[main node, label=$\infty$] (E) at (4,5) {E};
    \node[main node, label=$\infty$] (C) at (1,3) {C};
    \node[main node, label=$\infty$] (D) at (3,3) {D};

    \path [->] (A) edge node {$-1$} (B);
    \path [->] (B) edge node {$2$} (E);
    \path [->] (A) edge node {$4$} (C);
    \path [->] (B) edge node {$3$} (C);
    \path [->] (B) edge node {$2$} (D);
    \path [->] (D) edge node {$1$} (B);
    \path [->] (D) edge node {$5$} (C);
    \path [->] (E) edge node {$-3$} (D);
    \end{tikzpicture}
\end{center}
\end{document}

答案1

这里有一个建议,使用calc库将路径从中心移开一定距离。当然,如果你经常这样做,你可以为此编写一个宏。或者你只需​​弯曲路径,如 B 和 D 节点之间的箭头所示。

\documentclass[]{article}
\usepackage{tikz}  
\usetikzlibrary{arrows.meta,positioning,calc}
\usepackage{tkz-graph}
\usepackage{graphicx}  
\begin{document}  
\section{}
\begin{center}
    \begin{tikzpicture}[auto,node distance=2cm,thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}, scale=2]
    \node[main node, label=$0$] (A) at (0,5) {A};
    \node[main node, label=$\infty$] (B) at (2,6.5) {B};
    \node[main node, label=$\infty$] (E) at (4,5) {E};
    \node[main node, label=below:$\infty$] (C) at (1,3) {C};
    \node[main node, label=below:$\infty$] (D) at (3,3) {D};

    \path [->] (A) edge node {$-1$} (B);
    \path [->] (B) edge node {$2$} (E);
    \path [->] (A) edge node {$4$} (C);
    \path [->] (B) edge node {$3$} (C);
    \path [->] let \p1=($(D)-(B)$),\n1={atan2(\y1,\x1)},\n2={180+\n1} in
     ($ (B.\n1)!2pt!90:(D.\n2) $) edge node {$2$} ($ (D.\n2)!2pt!-90:(B.\n1) $);
    \path [->] let \p1=($(B)-(D)$),\n1={atan2(\y1,\x1)},\n2={180+\n1} in
     ($ (D.\n1)!2pt!90:(B.\n2) $) edge node {$-2$} ($ (B.\n2)!2pt!-90:(D.\n1) $);
    \path [->,bend left=10] (D) edge node {$5$} (C);
    \path [->,bend left=10] (C) edge node {$-5$} (D);
    \path [->] (E) edge node {$-3$} (D);
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

离题了,但我刚刚看到了 AndréC 的回答,尽管我大体上同意他的观点,但我不会为此使用图表。在我看来,一个非常简单的循环更简单,也更短。

\documentclass[]{article}
\usepackage{tikz}  
\usetikzlibrary{calc}
\begin{document}  
\begin{center}
    \begin{tikzpicture}[auto,node distance=2cm,thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}, scale=2]
    \foreach \X/\Z [count=\Y,evaluate=\Y as \angle using {90-72+72*\Y}] in {B/\infty,A/0,C/\infty,D/\infty,E/\infty}
    {\node[main node,label={\angle:$\Z$}] (\X) at (\angle:2) {\X};}

    \path [->] (A) edge node {$-1$} (B);
    \path [->] (B) edge node {$2$} (E);
    \path [->] (A) edge node {$4$} (C);
    \path [->] (B) edge node {$3$} (C);
    \path [->] let \p1=($(D)-(B)$),\n1={atan2(\y1,\x1)},\n2={180+\n1} in
     ($ (B.\n1)!2pt!90:(D.\n2) $) edge node {$2$} ($ (D.\n2)!2pt!-90:(B.\n1) $);
    \path [->] let \p1=($(B)-(D)$),\n1={atan2(\y1,\x1)},\n2={180+\n1} in
     ($ (D.\n1)!2pt!90:(B.\n2) $) edge node {$-2$} ($ (B.\n2)!2pt!-90:(D.\n1) $);
    \path [->,bend left=10] (D) edge node {$5$} (C);
    \path [->,bend left=10] (C) edge node {$-5$} (D);
    \path [->] (E) edge node {$-3$} (D);
    \end{tikzpicture}
\end{center}
\end{document}

答案2

要构建图表,可以使用graphs库。

这里有两种略有不同的语法,但产生相同的结果。

circular placement选项允许您将节点放置在圆上。由于有 5 个节点,因此要绘制正五边形,角度等于 就足够了360/5=72°,其半径由 给出radius=40mm

clockwise=5选项以缩写形式给出相同的结果。

要放置箭头,使用引号库,只需将文本放在引号中{B->["3"]C}

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{graphs}
\usetikzlibrary{quotes}
\begin{document}  

   \tikz[>={Straight Barb[length=6pt,width=8pt]},thick] 
   \graph [circular placement, group polar shift=(-72:0), radius=40mm,nodes={draw,circle,font=\sffamily\Large\bfseries,fill=blue!20}]
   {B[label=$\infty$],E[label={[inner sep=0pt]60:$\infty$}],D[label=-90:$\infty$],C[label=-90:$\infty$],A[label={[inner sep=0pt]120:$0$}],
   {A->["-1"]B->["2"] E->["-3"] D->["5"] C},
   {B->["3"]C},
   {B->[bend left=10,"2"]D},
   {D->[bend left=10,"1"]B},
   {A->["4"]C}
   };


\tikz[>={Straight Barb[length=6pt,width=8pt]},thick] 
\graph [clockwise=5,radius=40mm,nodes={draw,circle,font=\sffamily\Large\bfseries,fill=blue!20}] 
   {B[label=$\infty$],E[label={[inner sep=0pt]60:$\infty$}],D[label=-90:$\infty$],C[label=-90:$\infty$],A[label={[inner sep=0pt]120:$0$}],
   {A->["-1"]B->["2"] E->["-3"] D->["5"] C},
   {B->["3"]C},
   {B->[bend left=10,"2"]D},
   {D->[bend left=10,"1"]B},
   {A->["4"]C}
   }; 

\end{document}

石墨烯

使用 www.DeepL.com/Translator 翻译

相关内容