更改顶点和边的样式

更改顶点和边的样式

我希望较大的圆是椭圆,而右边的圆环较小(如下图所示)。有人知道怎么做吗?

在此处输入图片描述

这是我的.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}

在此处输入图片描述

对于循环,我通过手动选择贝塞尔控制点获得了最佳结果,如上面的代码所示。

其余部分是我最初的想法,使用\scaleboxxscale变换来绘制椭圆状态。这有点复杂,但可以很好地控制椭圆的大小和偏心率。

为了保持(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);

在此处输入图片描述

这是一个更简单的循环方式,但与您的绘图有点距离(inout角度明显不同;如果更改它们,箭头将不会从节点边界上的相同点开始和结束):

\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}

在此处输入图片描述

相关内容