![TikZ、自动机的文本位置不正确](https://linux22.com/image/337544/TikZ%E3%80%81%E8%87%AA%E5%8A%A8%E6%9C%BA%E7%9A%84%E6%96%87%E6%9C%AC%E4%BD%8D%E7%BD%AE%E4%B8%8D%E6%AD%A3%E7%A1%AE.png)
以下是生成状态机的代码:
\documentclass[xcolor=x11names,compress]{beamer}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{arrows,automata}
\begin{document}
\begin{frame}{Power lost during upgrade}
\begin{figure}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=3.5cm]
\node[initial,state,accepting] (B) {$ON$};
\node[state] (A) [above right of=B] {$ASC0$};
\node[state] (F) [left of=A] {$FOTA$};
\node[state] (U) [below right of=A] {$ACM0$};
\path[->] (B) edge node [pos=0.5, sloped] {c1} (A)
(B) edge node [red, pos=0.5, sloped] {c2} (F)
(A) edge [loop above] node {$10s$} (A)
(A) edge [bend left, pos=0.5, sloped, below] node {reset} (B)
(A) edge node [red, pos=0.5, sloped]{timeout} (U)
(U) edge [loop right] node {forever}
(U) edge [bend left, pos=0.5, sloped, above] node {reset} (B)
(F) edge [bend left, pos=0.5, sloped, above] node {reset} (B);
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
看起来不错,但有两点有问题,我用红色突出显示它们。我已经使用了pos=0.5
,sloped
但似乎不正确。
然后我找到了一个解决方法,就是将有问题的两行改为:
...
(B) edge node [red, sloped, anchor=center, below] {c2} (F)
...
(A) edge node [red, sloped, anchor=center, above]{timeout} (U)
...
答案1
您的代码有两个问题,一个是auto
,另一个是您混合使用edge
和node
选项,例如
(A) edge [bend left, pos=0.5, sloped, below] node {reset} (B)
应该是
(A) edge [bend left] node[pos=0.5, sloped, below] {reset} (B)
似乎auto
不使用north
或south
锚点来将边标签放置在边的中点上。它会影响timeout
和c2
,但也会影响c1
。我不知道为什么,但您可以避免这个问题,auto
从您的tikzpicture
声明中隐藏每个标签的定位above
或below
根据需要。在这种情况下pos=0.5
不需要。
\documentclass[xcolor=x11names,compress]{beamer}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{arrows,automata}
\begin{document}
\begin{frame}{Power lost during upgrade}
\begin{figure}
\begin{tikzpicture}[>=stealth', shorten >=1pt, node distance=3.5cm]
\node[initial,state,accepting] (B) {$ON$};
\node[state] (A) [above right of=B] {$ASC0$};
\node[state] (F) [left of=A] {$FOTA$};
\node[state] (U) [below right of=A] {$ACM0$};
\path[->] (B) edge node [sloped, above] {c1} (A)
(B) edge node [red, sloped, below] {c2} (F)
(A) edge [loop above] node {$10s$} (A)
(A) edge [bend left] node[sloped, below] {reset} (B)
(A) edge node [red, above, sloped]{timeout} (U)
(U) edge [loop right] node {forever}
(U) edge [bend left] node[sloped, below] {reset} (B)
(F) edge [bend left] node[sloped, above] {reset} (B);
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}