tikz 自动机循环边缘上的多行标签

tikz 自动机循环边缘上的多行标签

在以下图灵机图中,转换 $b \to a, R$ 和 $a \to a, R$ 相互遮挡。我怎样才能将它们写成两条线,以便两者都可见?

最小 MWE:

\documentclass[convert={density=300,size=1080x800,outext=.png}]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows}
\tikzset{
    ->, % makes the edges directed
    >=stealth', % makes the arrow heads bold
    node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
    every state/.style={thick}, % sets the properties for each ’state’ node
    initial text=$ $, % sets the text that appears on the start arrow
}

\begin{document}


    \begin{tikzpicture}[shorten >=1pt,on grid,auto]
        \node[state, initial] (1) {$q_0$};
        \node[state, right of=1, accepting] (2) {$q_1$};
        
    
        \draw   (1) edge[above] node{$b$} (2)
                (1) edge[loop above] node[above]{$b \to a,R$} node{$a \to a, R$} (1)
        ;
    \end{tikzpicture}
\end{document}

答案1

您可以使用\node[above, align=left]{$b \to a,R$\\$a \to a, R$},但为了更好地对齐,我建议使用aligned环境:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{automata, positioning, arrows}
\tikzset{
  ->, % makes the edges directed
  >=stealth', % makes the arrow heads bold
  node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
  every state/.style={thick}, % sets the properties for each ’state’ node
  initial text=, % sets the text that appears on the start arrow
}

\begin{document}

\begin{tikzpicture}[shorten >=1pt, on grid, auto]
  \node[state, initial] (1) {$q_0$};
  \node[state, right of=1, accepting] (2) {$q_1$};

  \draw (1) edge[above] node{$b$} (2)
        (1) edge[loop above] node[above]{$
    \begin{aligned}
      b &\to a, R\\[-0.7ex]
      a &\to a, R
    \end{aligned}$} (1);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容