0

When using the initial state with the automata drawing library for the tikzpicture environment, I have been unable to find a way to change the color of the initial state on a per-node basis.

That is, I want to be able to have different colors for initial states in the same instance of a tikzpicture environment.

I would also be fine with the creation of a style or workaround that doesn't directly use the initial state but functionally and visually looks the same.

For example, in the LaTeX below, I am able to do this on a tikzpicture environment basis, but I would like to be able to modify it on a per-node basis:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{arrows, automata}

% The pgfkey for creating graphs used in tikzpicture
\tikzset{
    discrete graph/.style={
        ->,
        >=stealth',
        auto,
        thick,
        main node/.style={circle, draw, font={\sffamily\bfseries\Large#1}},
        nolooparrow/.style={-, every loop/.append style={-}},
        double arrows/.style args={##1, ##2, ##3}{
            decorate,
            decoration={
                markings,
                mark=at position 0 with {
                    \coordinate (ta-base-1) at (0, ##3pt);
                    \coordinate (ta-base-2) at (0, -##3pt);
                },
                mark=at position 1 with {
                    \draw[##1] (ta-base-1) -- (0,##3pt); \draw[##2] (ta-base-2) -- (0,-##3pt);
                }
            }
        },
        every node/.style={font=\sffamily\small},
    },
}

\begin{document}
    
    \begin{tikzpicture}[discrete graph=\normalsize, node distance=2.5cm]
        \node[main node, state, initial] (a) {$q_0$};
    \end{tikzpicture}
    
    \begin{tikzpicture}[discrete graph=\normalsize, node distance=2.5cm, every initial by arrow/.style={red}]
        \node[main node, state, initial, red] (a) {$q_0$};
    \end{tikzpicture}
    
\end{document}
2
  • I'm not really sure I understand your question. Normally an automaton (either deterministic or nondeterministic) only has one initial state. Do you want to draw automata with two or more initial states? And is the color issue related to the arrow color, or the node color, or both? For nodes it seems you can just add the color as an optional parameter, as you do currently in your second example.
    – Marijn
    Feb 20, 2021 at 19:45
  • @Marijn Yes, usually an automaton only has one initial state. However, when building NFAs from regex, there are some cases in which I would like the temporary usage of multiple initial states. Additionally, there are some rarer cases in which I use NFAs with multiple starting points. The issue corresponds to the arrow and arrow text color associated with the initial state. Feb 20, 2021 at 20:49

1 Answer 1

2

Perhaps the easiest is to draw the arrows for the additional initial nodes manually, and set them to the desired color.

The distance between the text and the node in the code below is set to 3ex, which is the default in the automata library.

MWE:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{arrows, automata, positioning, decorations.markings, calc}

% The pgfkey for creating graphs used in tikzpicture
\tikzset{
    discrete graph/.style={
        ->,
        >=stealth',
        auto,
        thick,
        main node/.style={circle, draw, font={\sffamily\bfseries\Large#1}},
        nolooparrow/.style={-, every loop/.append style={-}},
        double arrows/.style args={##1, ##2, ##3}{
            decorate,
            decoration={
                markings,
                mark=at position 0 with {
                    \coordinate (ta-base-1) at (0, ##3pt);
                    \coordinate (ta-base-2) at (0, -##3pt);
                },
                mark=at position 1 with {
                    \draw[##1] (ta-base-1) -- (0,##3pt); \draw[##2] (ta-base-2) -- (0,-##3pt);
                }
            }
        },
        every node/.style={font=\sffamily\small},
    },
}

\begin{document}
    
\begin{tikzpicture}[discrete graph=\normalsize, node distance=2.5cm, every initial by arrow/.style={red}]
\node[state,initial,red] (q_0) {$q_0$};
\node[state,blue]             (q_1) [above right=of q_0] {$q_1$};
\node (start2) [left=3ex of q_1,blue] {start}; % extra start label
\draw[->,>=stealth',blue] (start2) -- (q_1); % extra start arrow
\node[state]             (q_2) [below right=of q_0] {$q_2$};
\node[state,accepting]   (q_3) [below right=of q_1] {$q_3$};

\path[->] (q_0) edge node {0} (q_1)
                edge node [swap] {1} (q_2)
          (q_1) edge node {1} (q_3)
                edge [loop above] node {0} ()
          (q_2) edge node [swap] {0} (q_3)
                edge [loop below] node {1} ();
\end{tikzpicture}    
\end{document}

Result:

enter image description here

1
  • Exactly what I needed. Thank you! Feb 21, 2021 at 21:55

You must log in to answer this question.

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