0

Using the automata package I have the following code:

\begin{tikzpicture}
        \node[state, initial] (p) {p};
        \node[state,accepting, right of=p] (q) {q};
        \draw (q) edge[loop above] node{a}  (q);
        \draw (p) edge[above] node{b} (q);
        % \draw (p) edge[below] node{c} (q);
        
\end{tikzpicture}

The output looks like this:

enter image description here

However, I would like to add a label c below arrow b. How to do this?

3
  • 2
    \path (p) edge node[above] {b} node[below] {c} (q); should do it. Commented Oct 16, 2022 at 22:00
  • Yes, it does it! But what is the difference between edge[above] and edge node[above] ? From where can I read more about this?
    – A J
    Commented Oct 16, 2022 at 22:11
  • edge[above] makes all following nodes use above (i.e. anchor=south), though it should be possible to do edge[above] node{b} node[below]{c}. Commented Oct 16, 2022 at 22:20

1 Answer 1

1
  • in on arrow can be placed several nose.With their option(s) can be set above or below arrow, or on any other position on arrow,
  • labels on arrows you can write with help of quotes library
  • option auto positioned labels on arrows depending on arrow direction. See @Qrrbrbirlbel comment below()thank you for pointing this)
\documentclass[margin=3mm, preview]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
                quotes}
                
\begin{document}
\begin{tikzpicture}[auto,
node distance = 22mm,
            > = Stealth
                    ]
    \node[state, initial] (p) {p};
    \node[state,accepting, right of=p] (q) {q};
    %
    \draw[->] (q) edge[loop above] node{a}  (q);
    \draw[->] (p) edge node[above] {b} node[below] {c} (q);
\end{tikzpicture}

\bigskip
\begin{tikzpicture}[auto,
node distance = 22mm,
            > = Stealth
                    ]
    \node[state, initial] (p) {p};
    \node[state,accepting, right of=p] (q) {q};

    \draw[->] (q) edge[loop above, "a"] (q);
    \draw[->] (p) edge["b", "c" '] (q);     % or "swap" instead of '
\end{tikzpicture}
\end{document}

enter image description here

2
  • Just to clarify things: auto makes it so that the nodes on edges automatically use the appropriate above, above left, left, etc. depending on the direction of the connection. swap (shortcut: ') swaps the side of auto. The quotes library just implements the "text" syntax. Commented Oct 16, 2022 at 23:41
  • @Qrrbrbirlbel, you are right. I should this mentioned in answer ...
    – Zarko
    Commented Oct 16, 2022 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .