我有一个马尔可夫链,我想在两个状态之间画两条平行边。如果我向左/向右弯曲边缘,我就可以做到这一点,但我希望它们是直的和平行的。我在网站上看到的所有解决方案要么与弯曲有关,要么代码太复杂(对于看似太简单的东西)。我有很多过渡,所以我需要一些简单而有效的东西。提前谢谢。
下面是边缘重叠的 mwe:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,positioning}
\begin{document}
\begin{tikzpicture}
\node[state](a) {$a$};
\node[state,right=of a] (b) {$b$};
\draw[every loop]
(a) edge[auto=right] node {$\lambda$} (b)
(b) edge[auto=right] node {$\mu$} (a);
\end{tikzpicture}
\end{document}
答案1
有几种可能。手动或采用新 (?) 样式。
\documentclass{article}
\usepackage{multicol}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,positioning,calc}
\begin{document}
\begin{multicols}{2}
\section*{Manual adjustment}
\subsection*{Adjust angles}
\begin{tikzpicture}
\node[state](a) {$a$};
\node[state,right=of a] (b) {$b$};
\draw
(a.-10) edge[auto=right,->] node {$\lambda$} (b.190)
(b.170) edge[auto=right,->] node {$\mu$} (a.10);
\end{tikzpicture}
\subsection*{Bend}
\begin{tikzpicture}
\node[state](a) {$a$};
\node[state,right=of a] (b) {$b$};
\draw
(a) edge[bend right=10,auto=right,->] node {$\lambda$} (b)
(b) edge[bend right=10,auto=right,->] node {$\mu$} (a);
\end{tikzpicture}
\vfill\null\columnbreak
\section*{Automatic options}
\subsection*{Auto rotate}
\tikzset{auto rotate/.style={auto=right,->,
to path={let \p1=(\tikztostart),\p2=(\tikztotarget),
\n1={atan2(\y2-\y1,\x2-\x1)},\n2={\n1-10},\n3={\n1+190}
in (\tikztostart.\n2) -- (\tikztotarget.\n3) \tikztonodes}}}
\begin{tikzpicture}
\node[state](a) {$a$};
\node[state,right=of a] (b) {$b$};
\node[state,above=of b] (c) {$c$};
\draw
(a) edge[auto rotate] node {$\lambda$} (b)
(b) edge[auto rotate] node {$\mu$} (a);
\draw
(b) edge[auto rotate] node {$\lambda$} (c)
(c) edge[auto rotate] node {$\mu$} (b);
\end{tikzpicture}
\subsection*{Auto shift}
\tikzset{auto shift/.style={auto=right,->,
to path={ let \p1=(\tikztostart),\p2=(\tikztotarget),
\n1={atan2(\y2-\y1,\x2-\x1)},\n2={\n1+180}
in ($(\tikztostart.{\n1})!1mm!270:(\tikztotarget.{\n2})$) --
($(\tikztotarget.{\n2})!1mm!90:(\tikztostart.{\n1})$) \tikztonodes}}}
\begin{tikzpicture}
\node[state](a) {$a$};
\node[state,right=of a] (b) {$b$};
\node[state,above=of b] (c) {$c$};
\draw
(a) edge[auto shift] node {$\lambda$} (b)
(b) edge[auto shift] node {$\mu$} (a);
\draw
(b) edge[auto shift] node {$\lambda$} (c)
(c) edge[auto shift] node {$\mu$} (b);
\end{tikzpicture}
\end{multicols}
\end{document}
答案2
可以使用tikz-cd
包简单地绘制马尔可夫链。例如:
\documentclass[border=3mm]{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[cells={nodes={circle, draw=gray,
minimum size=3ex, inner sep=1pt, anchor=center}}
]
a \ar[r, bend left, "\lambda"]
&
b \ar[l, bend left, "\mu"]
\end{tikzcd}
\begin{tikzcd}[cells={nodes={circle, draw=gray,
minimum size=3ex, inner sep=1pt, anchor=center}}
]
a \ar[r, shift left, "\lambda"]
&
b \ar[l, shift left, "\mu"]
\end{tikzcd}
\end{document}