我正在尝试使用 tikz 复制自动机,想知道是否有一个关键字允许在没有“虚拟”节点的情况下模拟具有相对定位的状态的位置。下面是我当前的解决方案,其中包含一个空白节点和一张我试图复制状态位置的图像:
\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr, float}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{cleveref}
\usepackage{algorithm2e}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\renewcommand{\a}{\mathtt{a}}
\renewcommand{\b}{\mathtt{b}}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
\node[state,initial] (A_2) {$A_2$};
\node (Z) [right=of A_2] {};
\node[state,accepting] (B_2) [right=of Z] {$B_2$};
\node[state] (C_2) [below=of Z] {$C_2$};
\path[->]
(A_2) edge [bend left=15] node {$\varepsilon$} (B_2)
edge [swap] node {$\a$} (C_2)
(B_2) edge [bend left=15] node {$\a$} (A_2)
(C_2) edge [loop right] node {$\b$} ()
edge [swap] node {$\a,\b$} (B_2);
\end{tikzpicture}
\end{document}
答案1
我将\a,\b
其改为\tta,\ttb
,因为覆盖现有宏不是一个好主意。它们的原始定义(在article
)是
> \a=macro:
#1->\expandafter \@changed@cmd \csname \string #1\endcsname \relax .
> \b=macro:
->\OT1-cmd \b \OT1\b .
要获得灰色背景,请使用state/.append style={fill=black!10}
。箭头可以通过 来模仿>=stealth
。
\documentclass[tikz]{standalone}
\usetikzlibrary{automata,positioning}
\newcommand{\tta}{\mathtt{a}}
\newcommand{\ttb}{\mathtt{b}}
\begin{document}
\begin{tikzpicture}[>=stealth,auto,state/.append style={fill=black!10}]
\node[state,initial] (A2) {$A_2$};
\node[state,below right=of A2] (C2) {$C_2$};
\node[state,accepting,above right=of C2] (B2) {$B_2$};
\draw (A2) edge[->,bend left=15] node {$\varepsilon$} (B2);
\draw (B2) edge[->,bend left=15] node {$\tta$} (A2);
\draw (C2) edge[<-] node {$\tta$} (A2);
\draw (B2) edge[<-] node {$\tta,\ttb$} (C2);
\draw (C2) edge [loop right] node {$\ttb$} (C2);
\end{tikzpicture}
\end{document}
答案2
先放置节点C_2
,然后right/left of=C_2,above of=C_2
一起使用来对角放置A_2
和B_2
。以下代码的结果与 OP 中的完全相同。
\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr, float}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{cleveref}
\usepackage{algorithm2e}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\renewcommand{\a}{\mathtt{a}}
\renewcommand{\b}{\mathtt{b}}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
\node[state] (C_2) {$C_2$};
\node[state,initial] (A_2) [left of=C_2,above of=C_2] {$A_2$};
\node[state,accepting] (B_2) [right of=C_2,above of=C_2] {$B_2$};
\path[->]
(A_2) edge [bend left=15] node {$\varepsilon$} (B_2)
edge [swap] node {$\a$} (C_2)
(B_2) edge [bend left=15] node {$\a$} (A_2)
(C_2) edge [loop right] node {$\b$} ()
edge [swap] node {$\a,\b$} (B_2);
\end{tikzpicture}
\end{document}