TikZ、自动机的文本位置不正确

TikZ、自动机的文本位置不正确

以下是生成状态机的代码:

\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.5sloped但似乎不正确。

在此处输入图片描述

然后我找到了一个解决方法,就是将有问题的两行改为:

...
(B) edge node [red, sloped, anchor=center, below] {c2} (F)
...
(A) edge node [red, sloped, anchor=center, above]{timeout} (U)
...

请问为什么这两行代码需要与其他代码不同? 在此处输入图片描述

答案1

您的代码有两个问题,一个是auto,另一个是您混合使用edgenode选项,例如

(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不使用northsouth锚点来将边标签放置在边的中点上。它会影响timeoutc2,但也会影响c1。我不知道为什么,但您可以避免这个问题,auto从您的tikzpicture声明中隐藏每个标签的定位abovebelow根据需要。在这种情况下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}

在此处输入图片描述

相关内容