如何使用 TikZ 绘制自动机?

如何使用 TikZ 绘制自动机?

在此处输入图片描述

我是 TeX 新手,不熟悉 TikZ 包。

答案1

嗯,你的草图很具体,所以即使没有 TikZ 库也可以绘制automata

1. 解决方案

借助库arrows.meta(用于箭头)、库decorations.markings(用于将箭头放在边的中间)和quotes库(用于将节点放在边上),您可以获得:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{arrows.meta,decorations.markings,quotes}

    \begin{document}
\begin{tikzpicture}[
    auto,
    decoration={markings,
                mark=at position .66 with {\arrow{Stealth[]}}}
                    ]
\coordinate (A) at (0,0);
\coordinate (B) at (5,0);
    \fill[black] (A) circle  (1mm) (B) circle (1mm);
\draw[postaction=decorate]  (A) to [out=45,in=135,"DOT"] (B);
\draw[postaction=decorate]  (A) to [out=60,in=120,looseness=2,"DASH"] (B);
\draw[postaction=decorate]  (B) to [out=-135,in=-45,"LETTER SPACE"] (A);
\draw[postaction=decorate]  (B) to [out=-120,in=-60,looseness=2,"WORD SPACE"] (A);
\draw[postaction=decorate]  (B) to [out= 60,in= 30,distance=12mm,"DOT"] (B);
\draw[postaction=decorate]  (B) to [out=-30,in=-60,distance=12mm,"DASH"] (B);
\end{tikzpicture}
    \end{document}

选项auto用于自动放置边缘标签在边缘上(上方/下方)。

2. 解决方案

out=<angle>, in=<anfle>正如 Herr K- 在他的评论中所述,如果将 替换为 , 则上述代码可以稍微简化。这样,连接坐标和的bend left=<angle>路径就变成:AB

\draw[postaction=decorate]  (A) to [bend left=45,"DOT"] (B);
...
\draw[postaction=decorate]  (B) to [out= 60,in= 30,distance=12mm,"DOT"] (B);
\draw[postaction=decorate]  (B) to [out=-30,in=-60,distance=12mm,"DASH"] (B);

3.解决方法:

通过以下定义可获得进一步的代码优化:

every edge/.style = {draw, postaction=decorate}

考虑到这两种优化,所需图像的代码变为:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{arrows.meta,decorations.markings,quotes}

    \begin{document}
\begin{tikzpicture}[
    auto,
    decoration = {markings,
                  mark=at position .7 with {\arrow{Stealth[length=2mm]}}},
every edge/.style = {draw, postaction=decorate}
                    ]
\coordinate (A) at (0,0);
\coordinate (B) at (5,0);
    \fill[black] (A) circle  (1mm) (B) circle (1mm);
\path   (A) edge [bend left=15,"DOT"]                         (B)
        (A) edge [bend left=15,"DOT"]                         (B)
        (A) edge [bend left=45,looseness=1.5,"DASH"]          (B)
        (B) edge [bend left=15,"LETTER SPACE"]                (A)
        (B) edge [bend left=45,looseness=1.5,"WORD SPACE"]    (A)
        (B) edge [out= 75,in= 15,distance=12mm,"DOT"]         (B)
        (B) edge [out=-75,in=-15,distance=12mm,swap,"DASH"]   (B);
\end{tikzpicture}
    \end{document}

与 1. 解决方案相比,此处的图像外观略有改变(弯曲角度和松动度较小)。

在此处输入图片描述

答案2

这是另一个简单的版本元帖子,仅供比较。

在此处输入图片描述

prologues:=3;outputtemplate:="%j%c.eps";
beginfig(1);

path c, e, a, b; 
c = fullcircle scaled 89; 
e = c yscaled 7/16;
a = point 0 of c {dir 80} 
 .. point 0 of c shifted (21,13) {down}
 .. {dir -170} cycle;
b = a reflectedabout(left,right);

forsuffixes $=e,c:
  drawarrow subpath(6,2) of $; 
  drawarrow subpath(2,-2) of $;
endfor

forsuffixes $=a,b:
  draw $;
  drawarrow subpath (1,1.414) of $;
endfor 

forsuffixes $=0,4:
  fill fullcircle scaled 4 shifted point $ of c;
endfor

defaultfont := "ptmr8r";
defaultscale := 0.7;

label.top("DASH", point 2 of c);
label.top("DOT",  point 2 of e);
label.urt("DOT",  point 1 of a);
label.lrt("DASH", point 1 of b);
label.bot("LETTER SPACE", point 6 of e);
label.bot("WORD SPACE",   point 6 of c);

endfig;
end

相关内容