嵌套 tikz 图片

嵌套 tikz 图片

我想要以下 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}

其中,我不想在从 q0 到 q1 的路径上标注 $a$,而是希望得到下图这样的图表:

\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}

但是,用一个替换另一个并不一定有效。

我想要的最终结果与此类似

在此处输入图片描述

以下是 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}

答案1

正如您所观察到的,tikzpicture应避免嵌套 s。一种可能性是使用savebox来存储图表。然后您可以使用(q0) edge node {\usebox{\mydiagram}} (q1)来获取所需的输出。

在此处输入图片描述

\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}

答案2

这个怎么样:

\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}

唯一真正的区别是定位q0 below left of = final-zero和 q1 与最后一个的区别。我还用示波器包裹了第二幅图,并将node distance = 2cm, on grid选项移入其中。

相关内容