[Tex/LaTex] How to avoid superfluous space in the initial state of a tikz automaton

automatatikz-pgf

When drawing tikz automata, I always omit the inital text and only use an arrow to indicate that a state is initial. To this end, I add

\tiksset[initial text={}]

to the preamble of my tex file.

This works fine so far, but when I was positioning several automata horizontally in a figure, I noticed that the initial arrow occupies to much space. You can see this phenomenon in the following picture. The red rectangle shows the bounding box.

tikz picture

I suspect that this happens because the node for the text is still there and has a nonnegative size. However, I would like to have the bounding box start where the arrow starts. Does anyone know how to achieve this without changing the bounding box by hand? Tanks!

The picture above results from the following code.

    \documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata}
\tikzset{initial text={}}

\begin{document}

\begin{tikzpicture}
    \node[state, initial] {};
    \draw[red, dashed] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

\end{document}

Best Answer

It's because when you clear the text of the initial node you don't actually decrease the node size. I've added a draw to the node options to make it obvious that your guess is correct:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata}
\tikzset{initial text={}}
\makeatletter
\tikzstyle{initial by arrow}=   [after node path=
{
  {
    [to path=
    {
      [->,double=none,every initial by arrow]
      ([shift=(\tikz@initial@angle:\tikz@initial@distance)]\tikztostart.\tikz@initial@angle)
          node [shape=rectangle,anchor=\tikz@initial@anchor,draw] {\tikz@initial@text}
        -- (\tikztostart)}]
    edge ()
  }
}]
\makeatother

\begin{document}

\begin{tikzpicture}
    \node[state, initial] {};
    \draw[red, dashed] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

\end{document}

enter image description here

Instead of draw you can put an inner sep=0 which is a good approximation, or delete the node or add overlay to keep the node but not include it in the bounding box.