我需要绘制一种自动机,每条圆弧都具有:
- 圆弧中点处的“字母标签”,
并且(有些)有
弧线开头的“数字标签”,
弧末端的“数字标签”。
我认为,与使用普通的 tikz 相比,使用一些现有的库或包(例如 tikzcd、automata、tkzgraph……)会更舒服。但在每种情况下,我都会发现一些无法正确解决的问题。
例如,使用 tikzcd 我获得了以下(相当令人满意的)结果
\documentclass{article}
\usepackage{tikz,tikz-cd}
\usetikzlibrary{arrows.meta,bending}
\tikzcdset{
arrow style=tikz, % Latin Modern, math font, tikz
diagrams={>=stealth}, % to, stealth, latex, space
diagrams={nodes={inner sep=1pt}},
}
\begin{document}
\begin{tikzcd}[column sep = 50,row sep = 40]
\odot \arrow[loop left,distance=3em,
out=145,in=215,
"y" description,
blue]
\arrow[r,bend left=90,red,"x" description]
& \bullet
\arrow[l,red, "x" description,
"\scriptscriptstyle{(0,2)}"' very near start,
"\scriptscriptstyle{(1,2)}"' very near end]
\arrow[loop right,distance=3em,
out=35,in=-35,
"y" description,
"\scriptscriptstyle{(0,2)}"' very near start,
"\scriptscriptstyle{(1,2)}"' very near end,
blue]\\
&\bullet \arrow[lu,red,"x" description,
"\scriptscriptstyle{(1,0)}" very near end]
\arrow[u,blue,"y" description]
\end{tikzcd}
\end{document}
当然,问题在于循环的“数字标签”。有没有办法正确定位这些标签(出现在边缘循环的开始和结束处)?
我也尝试使用自动机库(我认为它更适合绘制自动机,对吗?),结果如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows.meta,bending}
\tikzset{every state/.style={
inner sep=2pt,
minimum size=4pt,
%fill=black
}}
\tikzset{every edge/.append style={
font=\tiny %\scriptsize \tiny
}}
\tikzset{>=stealth} %latex, to, stealth
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
\node[state,accepting] (1) {1};
\node[state] (4) [right=of 1] {4};
\node[state] (3) [below=of 4] {3};
\path[->]
(1) edge[red,bend left,in=90,out=90]
node {$a$}(4)
edge[loop left,blue,min distance=10mm]
node {$b$}(1)
(3) edge[red]
node {$a$}
node[very near end] {${(1,0)}$}(1)
edge[blue]
node {$b$}(4)
(4) edge[red]
node {$a$}
node[near end,swap] {${(1,2)}$}
node[very near start,swap] {${(0,2)}$} (1)
edge[loop right,blue,min distance=10mm]
node {$b$}
node[pos=0] {${(0,2)}$}
node[pos=1] {${(1,2)}$}(4);
\end{tikzpicture}
\end{document}
这里的问题更多。至少有:
在循环中定位“数字标签”(如前所述),
模拟“描述”效果来定位“字母标签”,就像在 tikcd 示例中一样,
“数字标签”中的间距不好(可能是由 \tiny 字体大小导致的)。
你知道如何解决这些问题吗?你认为哪种方法更适合绘制这种自动机?
当然,我不是专家。如果我问的是一些琐碎或容易找到的问题,请见谅。
答案1
相反,auto
您可以固定您想要标签的位置。默认情况下,auto
放置标签above
或below
路径。如果没有,auto
它们将放置在on
路径上,如果您希望它们剪切路径,请使用fill=white
。
举个例子:
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows.meta,bending}
\tikzset{every state/.style={
inner sep=2pt,
minimum size=4pt,
%fill=black
}}
\tikzset{every edge/.append style={
font=\tiny
}}
\tikzset{>=stealth} %latex, to, stealth
\begin{document}
\begin{tikzpicture}[shorten >=1pt, node distance=2cm, on grid]
\node[state,accepting] (1) {1};
\node[state] (4) [right=of 1] {4};
\node[state] (3) [below=of 4] {3};
\path[->]
(1) edge[red,bend left,in=90,out=90]
node[fill=white] {$a$}(4)
edge[loop left,blue,min distance=10mm]
node[fill=white] {$b$}(1)
(3) edge[red]
node[fill=white] {$a$}
node[near end, below left=-1mm] {(1,0)}(1)
edge[blue]
node[fill=white] {$b$}(4)
(4) edge[red]
node[fill=white] {$a$}
node[pos=.8,above=-1mm] {(1,2)}
node[pos=.2,above=-1mm] {(0,2)} (1)
edge[loop right,blue,min distance=10mm]
node[fill=white] {$b$}
node[pos=0.1, above] {(0,2)}
node[pos=0.9, below] {(1,2)}(4);
\end{tikzpicture}
\end{document}