我需要绘制几个图形(图论类型),并在边上画上箭头。下面是我所指的那种东西的粗略草图:
我知道tkz-graph
并且如果可能的话会使用它 - 我已经看过它的手动的(我不懂法语,但我能看懂代码示例)但我没有看到任何类似的东西。
或者,我很乐意使用简单的方法tikz
来实现这一点,但我不知道如何用这种方式绘制与边缘平行的东西。(我想自动完成,而不是手动定位箭头。)
有没有一种直接的方法可以做到这一点,要么在tkz-graph
,要么就在tikz
?
答案1
借助 mathtools˙package 的纯 TikZ 解决方案:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{chains,positioning}
\usepackage{mathtools}
\begin{document}
\begin{tikzpicture}[
start chain = A going right,
]
\begin{scope}[every node/.style={circle, draw, minimum size=1.5em,
inner sep=2pt, on chain}]
\foreach \i in {1,...,5} \node {};
%
\node (B-1) [below=of A-2] {A};
\node (B-2) [below=of A-4] {B};
\end{scope}
\begin{scope}[every node/.style={above, sloped}]
\draw[densely dotted, thick]
(A-1) -- (A-2)
(A-2) -- node {$\xleftarrow{1}$} (A-3)
(A-3) -- (A-4)
(A-4) -- node {$\xleftarrow{1}$} (A-5);
\draw[thick] (B-1) -- node {$\xleftarrow{1}$} (A-2)
(B-1) -- node {$\xrightarrow{1}$}(A-3)
%
(B-2) -- node[below] {$\xleftarrow[1]{}$} (A-3)
(B-2) -- node {$\xleftarrow{1}$} (A-4)
;
%
\draw[double] (A-1) -- (B-1)
(A-5) -- node[below] {$\xleftarrow[2]{}$} (B-2);
\end{scope}
\end{tikzpicture}
\end{document}
附录:
另一种解决方案是pic
:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,chains,positioning}
\begin{document}
\begin{tikzpicture}[
start chain = A going right,
pics/AB/.style args = {#1/#2/#3}{code={
\draw[solid,-{Straight Barb[length=1mm]},very thick]
(-3mm,#1 2mm) -- node [#2,font=\footnotesize,inner sep=2pt] {#3} ++ (6mm,0mm);}
},
pics/BA/.style args = {#1/#2/#3}{code={
\draw[solid,{Straight Barb[length=1mm]}-,very thick]
(-3mm,#1 2mm) -- node [#2,font=\footnotesize,inner sep=2pt] {#3} ++ (6mm,0mm);}
}
]
\begin{scope}[every node/.style={circle, draw, minimum size=1.5em,
inner sep=2pt, on chain}]
\foreach \i in {1,...,5} \node {};
%
\node (B-1) [below=of A-2] {A};
\node (B-2) [below=of A-4] {B};
\end{scope}
\begin{scope}[sloped]
\draw[densely dotted, thick]
(A-1) -- (A-2)
(A-2) -- pic {BA=/above/1} (A-3)%node {$\xleftarrow{1}$} (A-3)
(A-3) -- (A-4)
(A-4) -- pic {AB=/above/1} (A-5);
\draw[thick] (B-1) -- pic {AB=/left/1} (A-2)
(B-1) -- pic {AB=-/above left/1} (A-3)
%
(A-3) -- pic {BA=-/below left/1} (B-2)
(B-2) -- pic {AB=/left/1} (A-4);
%
\draw[double] (A-1) -- (B-1)
(A-5) -- pic {BA=-/below right/2} (B-2);
\end{scope}
\end{tikzpicture}
\end{document}
编辑:在附录的第一个版本中,我弄乱了箭头方向。现在已更正。