tikz 自动机:使转换的标签定位与状态是否接受无关

tikz 自动机:使转换的标签定位与状态是否接受无关

在编写一些生成用于显示自动机的 TikZ 代码的代码时,我遇到了以下问题:使用相同的标签注释,即“edge[bend angle=18,bend right] node[below=0,sloped]”,标签的位置取决于某些源/目标状态是否接受:在下图中,情况 b 定位错误:有没有办法有一些统一的命令来正确定位标签,无论源/目标状态是否接受?这是否可能是 TikZ 中的一个错误(我尝试过的其他定位对源/目标状态是否接受这一事实不敏感)。

\documentclass{article}
\usepackage{tikz} \usetikzlibrary{arrows,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,semithick]

\begin{scope}
\node[state] (1a) at (1cm,1cm) {1a};
\node[state] (2a) at (1cm,3cm) {2a};
\path
(1a) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2a);
\end{scope}

\begin{scope}[xshift=2cm]
\node[state,accepting] (1b) at (1cm,1cm) {1b};
\node[state]           (2b) at (1cm,3cm) {2b};
\path
(1b) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2b);
\end{scope}

\begin{scope}[xshift=4cm]
\node[state]           (1c) at (1cm,1cm) {1c};
\node[state,accepting] (2c) at (1cm,3cm) {2c};
\path
(1c) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2c);
\end{scope}

\begin{scope}[xshift=6cm]
\node[state,accepting] (1d) at (1cm,1cm) {1d};
\node[state,accepting] (2d) at (1cm,3cm) {2d};
\path
(1d) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2d);
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

发生这种情况的原因是,对于倾斜路径 TiZ 会尝试智能地解释诸如above等按键。要关闭此功能,您可以使用allow upside down键。

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{arrows,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,semithick,
    allow upside down]

\begin{scope}
\node[state] (1a) at (1cm,1cm) {1a};
\node[state] (2a) at (1cm,3cm) {2a};
\path
(1a) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2a);
\end{scope}

\begin{scope}[xshift=2cm]
\node[state,accepting] (1b) at (1cm,1cm) {1b};
\node[state]           (2b) at (1cm,3cm) {2b};
\path
(1b) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2b);
\end{scope}

\begin{scope}[xshift=4cm]
\node[state]           (1c) at (1cm,1cm) {1c};
\node[state,accepting] (2c) at (1cm,3cm) {2c};
\path
(1c) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2c);
\end{scope}

\begin{scope}[xshift=6cm]
\node[state,accepting] (1d) at (1cm,1cm) {1d};
\node[state,accepting] (2d) at (1cm,3cm) {2d};
\path
(1d) edge[bend angle=18,bend right] node[below=0,sloped] {$=$} (2d);
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

这与主题无关,但为了缩短代码,您可以使用循环。

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{arrows.meta,automata}

\begin{document}
\begin{tikzpicture}[->,>=Stealth,shorten >=1pt,auto,semithick]
\foreach \X [count=\Y] in {a,...,d}
{
 \ifodd\Y
  \path  (2*\Y,1)  node[state]  (1\X) {1\X};
 \else 
  \path  (2*\Y,1)  node[state,accepting]  (1\X) {1\X};
 \fi
 \ifnum\Y<3
  \path   (2*\Y,3) node[state](2\X) {2\X};
 \else
  \path   (2*\Y,3) node[state,accepting](2\X) {2\X};
 \fi
 \path
 (1\X) edge[bend angle=18,bend right] node[below=0,sloped,
 allow upside down] {$=$} (2\X);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这些缩短代码的方式是否有意义,总是取决于人们最终的想法。

答案2

我不知道为什么会发生这种情况,但是使用quotes库、设置auto=right和写入edge ["$=$"]可以解决您的问题。当然,您需要相应地定义箭头和边引号(请参阅下面的 MWE)。使用 TikZ 库chains可以使代码更短:

    \begin{scope}[nodes={state, on chain}]
\node (s11)                 {1a};
\node (s12)                 {1b};
\node (s13) [accepting]     {1c};
\node (s14) [accepting]     {1d};
%
\node (s21) [above=of s11]  {2a};
\node (s22) [accepting]     {2b};
\node (s23)                 {1c};
\node (s24) [accepting]     {1d};
    \end{scope}
    \foreach \i in {1,...,4}
{
\path   (s1\i) edge["$=$"] (s2\i);
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容