我正在尝试在 tikz 中绘制下图。
我正在处理的代码是:
\documentclass {article}
% example taken from
% http://www.guitex.org/home/images/doc/GuideGuIT/introingtikz.pdf
\usepackage {tikz}
\usetikzlibrary {positioning}
%\usepackage {xcolor}
\definecolor {processblue}{cmyk}{0.96,0,0,0}
\begin {document}
\begin {center}
\begin {tikzpicture}[-latex ,auto ,node distance =4 cm and 5cm ,on grid ,
semithick ,
state/.style ={ very thick, circle ,top color =white , ,
draw , text=blue , minimum width =2 cm}]
%\node[state] (C)
%{$1$};
\draw[very thick] (0,0) circle (.8cm);
\node[state] (A) {$q_0$};
\draw[very thick] (0,0) circle (.8cm);
\node[state] (B) [ right =of A] {$q_1$};
\path (A) edge [loop above] node[above] {$b$} (A);
\path (B) edge [loop above] node[above] {$b$} (B);
%\path (C) edge [bend left =25] node[below =0.15 cm] {$1/2$} (A);
%\path (A) edge [bend right = -15] node[below =0.15 cm] {$1/2$} (C);
\path (A) edge [bend left =25] node[above] {$a$} (B);
\path (B) edge [bend left =15] node[below =0.15 cm] {$a$} (A);
%\path (C) edge [bend left =15] node[below =0.15 cm] {$1/2$} (B);
%\path (B) edge [bend right = -25] node[below =0.15 cm] {$1/2$} (C);
\end{tikzpicture}
\end{center}
\end{document}
如何让左边的箭头弯曲,并绘制右边的箭头q_0
?
答案1
这是我的尝试:
\documentclass[border=0.2cm]{standalone}
% required packages and libraries
\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture} [draw=cyan!70!black,
node distance = 3cm,
on grid,
auto,
every initial by arrow/.style = {thick}]
% State q0
\node (q0) [state,
initial,
accepting,
initial text = {}] {$q_0$};
% State q1
\node (q1) [state,
right = of q0] {$q_1$};
% Arrows
\path [-stealth, thick]
(q0) edge[bend left] node {$a$} (q1)
(q1) edge[bend left] node {$b$} (q0)
(q0) edge [loop above] node {b}()
(q1) edge [loop above] node {b}();
\end{tikzpicture}
\end{document}
得出以下结果: 有关更多详细信息,您可以查看此分步教程:LaTeX 中的自动机图
答案2
使用arrows.meta
、automata
和positioning
库quotes
:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[auto,
> = Stealth,
every edge quotes/.style = {font=\footnotesize}, % if you like to have smaller edge labels
every edge/.append style = {->, draw=cyan, thick},
every loop/.append style = {<-, looseness = 12},
node distance = 22mm,
state/.style = {circle, semithick, draw=cyan, text=cyan, minimum size=1.2em},
initial text = ,
]
\node (A) [state, initial, accepting] {$q_0$};
\node (B) [state, right=of A] {$q_1$};
\path (A) edge [loop above, "$b$"] ()
(A) edge [bend left,"$a$"] (B)
(B) edge [bend left,"$a$"] (A)
(B) edge [loop above, "$b$"] ();
\end{tikzpicture}
\end{document}
答案3
由于您已标记了您的问题tikz-cd,这是一个通过 TikZ-CD 包解决的解决方案。
此外,它还使用它automata
提供的各种样式和键的库以及bending
使弯曲的箭头看起来更好的库。
我定义了Loop …
大写字母的样式L
,其角度与普通loop …
样式不同。该initial by arrow
键被替换为initial by arrow wo text
TikZ-CD 中的键,automata
选项为 as,这样就设置好了一切。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{cd, automata, bending}
\definecolor{processblue}{cmyk}{0.96,0,0,0}
\makeatletter
\tikzset{
Loop/.style={anchor={180+#1}, out={#1-15}, in={#1+15}, loop},
/utils/temp/.style args={#1=#2}{Loop #1/.style={Loop={#2}}, commutative diagrams/Loop #1/.style={/tikz/Loop #1, labels={auto=false}}},
/utils/temp/.list={above=90, right=0, left=180, below=270, above right=45, above left=135, below left=235, below right=315},
initial by arrow wo text/.code={\arrow[double=none, every initial by arrow, to path={([shift=(\tikz@initial@angle:\tikz@initial@distance)]\tikztostart.\tikz@initial@angle) -- (\tikztostart)}]}}
\makeatother
\tikzcdset{
automata/.style={
/tikz/initial/.style={initial by arrow wo text},
/tikz/accepting by double/.style={double,outer sep=\pgflinewidth+.3pt},
cells={nodes={state, draw=processblue}},
labels={math mode=false, node font=\footnotesize\ttfamily},
arrows={thick, color=processblue, text=black, /tikz/>={Latex[angle'=25]}, ->}}}
\begin{document}
\begin{tikzcd}[automata]
|[initial, accepting]|
q_0 \ar[Loop above, "b"]
\ar[r, bend left, "a"]
& q_1 \ar[Loop above, "b"]
\ar[l, bend left, "a"]
\end{tikzcd}
\begin{tikzcd}[automata, bend angle=10, text depth=+0pt, /utils/exec=\def\c{{\normalfont, }}]
& |[accepting, initial]| 1, 2 \ar[r, "a"] \ar[dr, "b"]
& 2 \ar[d, "b"] \ar[ Loop right, "a"] \\
& 4 \ar[u, "a\c b"']
& |[accepting]| 3 \ar[l, "a"] \ar[d, "b"] \\
2, 4 \ar[uur, "a"] \ar[dr, bend left, "b"]
& & |[accepting]| 2, 3 \ar[ll, "a"] \ar[ Loop right, "b"] \\
& |[accepting]| 1, 2, 3 \ar[ul, bend left, "a"] \ar[ur, "b"']
\end{tikzcd}
\end{document}