![更改顶点和边的样式](https://linux22.com/image/451142/%E6%9B%B4%E6%94%B9%E9%A1%B6%E7%82%B9%E5%92%8C%E8%BE%B9%E7%9A%84%E6%A0%B7%E5%BC%8F.png)
我希望较大的圆是椭圆,而右边的圆环较小(如下图所示)。有人知道怎么做吗?
这是我的.TEX 文档:
\documentclass[12pt]{article}
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz, tkz-euclide}
\usepackage{amssymb}
\usetikzlibrary{automata, positioning, arrows}
\tikzset{->,
>=stealth,
node distance = 3cm,
every state/.style={thick, fill=gray!10},
initial text = $I$}
\begin{document}
\begin{center}
\begin{tikzpicture}
\node[state, initial,accepting] (Q1) at (0,0) {$\{q_1\}$};
\node[state,accepting] (Q2) at (3,0) {$\{q_1,q_2,q_3\}$};
\draw
(Q1) edge [above] node [] {$a$} (Q2)
(Q2) edge [loop right] node [] {$a$} (Q2);
\end{tikzpicture}
\end{center}
\end{document}
这是我得到的输出:
答案1
编辑:感谢Zarko 的回答(+1),我意识到我没有正确尝试明显的ellipse
样式(或shape=ellipse
):我只尝试了\node[ellipse, state, ...
没有给出预期结果的样式,因为很可能state
暗示了circle
形状,因此ellipse
如果按此顺序使用,样式将被覆盖。但是,使用\usetikzlibrary{shapes.geometric}
允许\node[state, ellipse, ...
绘制具有椭圆形状的状态节点。因此,最简单的方法是:
\documentclass[tikz,12pt,border=1mm]{standalone}
\usetikzlibrary{automata, shapes.geometric}
\begin{document}
\begin{tikzpicture}[
->, >=stealth,
node distance=3cm,
every state/.style={thick, fill=gray!10},
initial text={$I$},
]
\node[state, initial, accepting] (Q1) at (0,0) {$\{q_1\}$};
\node[state, ellipse, accepting] (Q2) at (3,0) {$\{q_1, q_2, q_3\}$};
\draw (Q1) edge [above] node {$a$} (Q2);
\draw (Q2.10) .. controls +(40:3mm) and +(90:2mm) .. ([xshift=3.3mm] Q2.east)
node[right] {$a$} .. controls +(-90:2mm) and +(-40:3mm) .. (Q2.-10);
\end{tikzpicture}
\end{document}
对于循环,我通过手动选择贝塞尔控制点获得了最佳结果,如上面的代码所示。
其余部分是我最初的想法,使用\scalebox
和xscale
变换来绘制椭圆状态。这有点复杂,但可以很好地控制椭圆的大小和偏心率。
为了保持(Q2)
节点内的字体形状不变,我首先按缩放整个circle
形节点所用的因子的倒数水平缩放节点内容(0.5 × 2 = 1 → 文本形状不变)。
\documentclass[tikz,12pt,border=1mm]{standalone}
\usepackage{graphicx} % for \scalebox
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[
->, >=stealth,
node distance=3cm,
every state/.style={thick, fill=gray!10},
initial text={$I$},
]
\node[state, initial, accepting] (Q1) at (0,0) {$\{q_1\}$};
\node[xscale=2, transform shape, state, accepting] (Q2) at (3,0) {%
\scalebox{0.5}[1]{$\{q_1, q_2, q_3\}$}};
\draw (Q1) edge [above] node {$a$} (Q2);
\draw (Q2.20) .. controls +(45:4mm) and +(90:3mm) .. ([xshift=5mm] Q2.east)
node[right] {$a$} .. controls +(-90:3mm) and +(-45:4mm) .. (Q2.-20);
\end{tikzpicture}
\end{document}
较小的循环:
\draw (Q2.20) .. controls +(40:3mm) and +(90:2mm) .. ([xshift=3.5mm] Q2.east)
node[right] {$a$} .. controls +(-90:2mm) and +(-40:3mm) .. (Q2.-20);
这是一个更简单的循环方式,但与您的绘图有点距离(in
和out
角度明显不同;如果更改它们,箭头将不会从节点边界上的相同点开始和结束):
\draw (Q1) edge [above] node {$a$} (Q2)
(Q2) edge [loop right, looseness=4] node {$a$} (Q2);
答案2
@frougon 答案第一版的变体。通过将state
样式定义为椭圆,MWE 变得更加简洁明了:
\documentclass[tikz,12pt,border=1mm]{standalone}
\usetikzlibrary{arrows.meta, automata,
positioning,
quotes,
shapes}
\begin{document}
\begin{tikzpicture}[
> = Stealth,
node distance = 7mm,
every edge/.style = {draw, ->},
every state/.style = {ellipse, draw, thick, fill=gray!15,
minimum size=2em, inner xsep=0pt},
initial text = {$I$},
]
\node (Q1) [state, initial, accepting] {$\{q_1\}$};
\node (Q2) [state, accepting,
right=of Q1] {$\{q_1, q_2, q_3\}$};
\draw (Q1) edge ["$a$"] (Q2)
(Q2) edge [loop right, distance=9mm,"$a$"] (Q2);
\end{tikzpicture}
\end{document}