0

I want to have the following tikzpicture

\begin{tikzpicture} [
    node distance = 5cm, 
    on grid, 
    auto,
    every loop/.style={stealth-}]
 
% State q0 
\node (q0) [state, 
    initial, 
    initial text = {}] {$q_0$};
 
% State q1    
\node (q1) [state,
    accepting,
    right = of q0] {$q_1$};
 
% Arrows
\path [-stealth, thick]
    (q0) edge node {$a$}   (q1)
    (q1) edge [loop above]  node {$\top$}();
\end{tikzpicture}

Where instead of label $a$ on the path from q0 to q1 I want to have the following picture as a diagram:

\tikzset{%
  zeroarrow/.style = {-stealth,dashed},
  onearrow/.style = {-stealth,solid},
  c/.style = {circle,draw,solid,minimum width=2em,
        minimum height=2em},
  r/.style = {rectangle,draw,solid,minimum width=2em,
        minimum height=2em}
}

\begin{tikzpicture}[node distance=1cm and 1cm]\footnotesize
   \node[c] (a) {a};
   \node[c] (b) [below right=of a] {b};
   \node[c] (c) [below left=of b] {c};
   \node[c] (d) [below=of c] {d};
   \node[r] (final-one) [below right=of d,xshift=-2pt] {1};
   \node[r] (final-zero) [below left=of d] {0};

   \draw[onearrow] (a) -- (b);
   \draw[onearrow] (b) -- (final-one);
   \draw[onearrow] (c) -- (d); 
   \draw[onearrow] (d) -- (final-one);

   \draw[zeroarrow] (a) -- (c);
   \draw[zeroarrow] (c) -- (final-zero);
   \draw[zeroarrow] (b) -- (c);
   \draw[zeroarrow] (d) -- (final-zero);
\end{tikzpicture}

However, replacing one with the other does not necessarily work.

The end result I wanted is similar to this one

enter image description here

Here is a MWE:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\usepackage{tikz}
\usetikzlibrary{positioning,automata} 

\tikzset{%
  zeroarrow/.style = {-stealth,dashed},
  onearrow/.style = {-stealth,solid},
  c/.style = {circle,draw,solid,minimum width=2em,
        minimum height=2em},
  r/.style = {rectangle,draw,solid,minimum width=2em,
        minimum height=2em}
}

\begin{document}

\begin{tikzpicture} [
    node distance = 5cm, 
    on grid, 
    auto,
    every loop/.style={stealth-}]
 
% State q0 
\node (q0) [state, 
    initial, 
    initial text = {}] {$q_0$};
 
% State q1    
\node (q1) [state,
    accepting,
    right = of q0] {$q_1$};
 
% Arrows
\path [-stealth, thick]
    (q0) edge node {$a$}   (q1)
    (q1) edge [loop above]  node {$\top$}();
\end{tikzpicture}


\begin{tikzpicture}[node distance=1cm and 1cm]\footnotesize
   \node[c] (a) {a};
   \node[c] (b) [below right=of a] {b};
   \node[c] (c) [below left=of b] {c};
   \node[c] (d) [below=of c] {d};
   \node[r] (final-one) [below right=of d,xshift=-2pt] {1};
   \node[r] (final-zero) [below left=of d] {0};

   \draw[onearrow] (a) -- (b);
   \draw[onearrow] (b) -- (final-one);
   \draw[onearrow] (c) -- (d); 
   \draw[onearrow] (d) -- (final-one);

   \draw[zeroarrow] (a) -- (c);
   \draw[zeroarrow] (c) -- (final-zero);
   \draw[zeroarrow] (b) -- (c);
   \draw[zeroarrow] (d) -- (final-zero);
\end{tikzpicture}


\end{document}
2
  • 1
    Hi, can you please provide a MWE that is compilable, not just the snippets.
    – anis
    May 25, 2023 at 13:40
  • @anis I added a MWE May 25, 2023 at 13:43

2 Answers 2

1

As you observed, nesting tikzpictures should be avoided. One possibility is to use a savebox to store the diagram. Then you can use (q0) edge node {\usebox{\mydiagram}} (q1) to get the desired output.

enter image description here

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\usepackage{tikz}
\usetikzlibrary{positioning,automata} 

\tikzset{%
  zeroarrow/.style = {-stealth,dashed},
  onearrow/.style = {-stealth,solid},
  c/.style = {circle,draw,solid,minimum width=2em,
        minimum height=2em},
  r/.style = {rectangle,draw,solid,minimum width=2em,
        minimum height=2em}
}

\newsavebox{\mydiagram}
\sbox{\mydiagram}{
    \begin{tikzpicture}[node distance=1cm and 1cm]\footnotesize
    \node[c] (a) {a};
    \node[c] (b) [below right=of a] {b};
    \node[c] (c) [below left=of b] {c};
    \node[c] (d) [below=of c] {d};
    \node[r] (final-one) [below right=of d,xshift=-2pt] {1};
    \node[r] (final-zero) [below left=of d] {0};
    \draw[onearrow] (a) -- (b);
    \draw[onearrow] (b) -- (final-one);
    \draw[onearrow] (c) -- (d); 
    \draw[onearrow] (d) -- (final-one);
    \draw[zeroarrow] (a) -- (c);
    \draw[zeroarrow] (c) -- (final-zero);
    \draw[zeroarrow] (b) -- (c);
    \draw[zeroarrow] (d) -- (final-zero);
    \end{tikzpicture}
}

\begin{document}

\begin{tikzpicture} [
    node distance = 5cm, 
    on grid, 
    auto,
    every loop/.style={stealth-}]
 
% State q0 
\node (q0) [state, 
    initial, 
    initial text = {}] {$q_0$};
 
% State q1    
\node (q1) [state,
    accepting,
    right = of q0] {$q_1$};
 
% Arrows
\path [-stealth, thick]
    (q0) edge node {\usebox{\mydiagram}}   (q1)
    (q1) edge [loop above]  node {$\top$}();
\end{tikzpicture}

\end{document}
1

How about this:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}

\usetikzlibrary{
    arrows,
    positioning,
    automata
}
\tikzset{%
zeroarrow/.style = {-stealth,dashed},
onearrow/.style = {-stealth,solid},
c/.style = {circle,draw,solid,minimum width=2em,
        minimum height=2em},
    r/.style = {rectangle,draw,solid,minimum width=2em,
        minimum height=2em}
}
\begin{document}
    \begin{tikzpicture} [
    auto,
    every loop/.style={stealth-}
    ]
    
    
    % Arrows
    \begin{scope}[node distance = 2cm, 
    on grid, ]

    \node[c] (a) {a};
    \node[c] (b) [below right=of a] {b};
    \node[c] (c) [below left=of b] {c};
    \node[c] (d) [below=of c] {d};
    \node[r] (final-one) [below right=of d,xshift=-2pt] {1};
    \node[r] (final-zero) [below left=of d] {0};
    
    \draw[onearrow] (a) -- (b);
    \draw[onearrow] (b) -- (final-one);
    \draw[onearrow] (c) -- (d); 
    \draw[onearrow] (d) -- (final-one);
    
    \draw[zeroarrow] (a) -- (c);
    \draw[zeroarrow] (c) -- (final-zero);
    \draw[zeroarrow] (b) -- (c);
    \draw[zeroarrow] (d) -- (final-zero);
    
    \end{scope}
    % State q0 
    \node (q0) [below left  = 0.5 cm of final-zero,
    state, 
    initial, 
    initial text = {}] {$q_0$};
    
    % State q1    
    \node (q1) [state,
    accepting,
    below right =0.5 cm of final-one]{$q_1$}; 
    
    \path [-stealth, thick]
    (q0) edge node {$a$}   (q1)
    (q1) edge [loop above]  node {$\top$}();
    \end{tikzpicture}
\end{document}

The only real difference is positioning q0 below left of = final-zero and q1 with the final one. I also wrapped the second drawing with a scope and move the node distance = 2cm, on grid options into it.

2
  • Is the last tikzpicture necessary? May 25, 2023 at 14:11
  • absolutely not. i just forgot it. i am editing it ty
    – anis
    May 25, 2023 at 14:32

You must log in to answer this question.

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