我正在尝试绘制马尔可夫链。由于我有 11 个状态,因此我尝试在 for 循环中自动声明状态以及边缘。这是我的 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,arrows.meta}
\begin{document}
\begin{tikzpicture}[->, >=stealth', auto, semithick]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black]
\foreach \i in {0,...,10}
{
\node[state] (\i) at (\i*2,0) {\i};
\path (\i) edge[loop above] node{0.1} (\i);
}
\foreach \i in {0,...,9}
{
\pgfmathparse{\i+1}; % result goes into --> \pgfmathresult
\path (\i) edge node{0.3} (\pgfmathresult);
}
\end{tikzpicture}
\end{document}
其结果如下:
如您所见,水平边的端点位于目标状态的“东”。理想情况下,我希望它们位于“西”。另一方面,如果我手动逐个写入边,我会得到更好的结果:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,arrows.meta}
\begin{document}
\begin{tikzpicture}[->, >=stealth', auto, semithick]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black]
\foreach \i in {0,...,10}
{
\node[state] (\i) at (\i*2,0) {\i};
\path (\i) edge[loop above] node{0.1} (\i);
}
\path (0) edge node{0.3} (1);
\path (1) edge node{0.3} (2);
\path (2) edge node{0.3} (3);
\path (3) edge node{0.3} (4);
\path (4) edge node{0.3} (5);
\path (5) edge node{0.3} (6);
\path (6) edge node{0.3} (7);
\path (7) edge node{0.3} (8);
\path (8) edge node{0.3} (9);
\path (9) edge node{0.3} (10);
\end{tikzpicture}
\end{document}
这里到底发生了什么?为什么两者表现不同?我做错了什么吗?我该如何解决?
答案1
小数位pgfmathparse
被解释为坐标中的角度。-所以你需要一个像这样的整数:
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{automata, arrows.meta}
\begin{document}
\tikzset{every state/.style={fill=white, thick}}
\begin{tikzpicture}[->, >={Stealth[round]}, auto, semithick]
\foreach \i in {0,...,10}{
\node[state] (\i) at (\i*2,0) {\i};
\draw (\i) edge[loop above] node{0.1} (\i);
}
\foreach \i in {0,...,9}{
\pgfmathparse{int(\i+1)}
\draw (\i) edge node{0.3} (\pgfmathresult);
}
\end{tikzpicture}
\end{document}