[Tex/LaTex] How to zoom a portion of TiKZ picture

tikz-pgf

I want to draw a small rectangle within a tikz picture, then zoom it out somewhere. I skim through the PGF manual but I couldn't find a way to put the logic together. So far I've just drawn the automaton diagram, I had to hack the blue rectangle with "paint" though :(. Anyone could help me out with this? Thank you.

enter image description here

Minimal Working Example

\documentclass[10pt,letterpaper]{article}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}

\usepackage[version=0.96]{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,automata,backgrounds,petri,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.fractals}
\usetikzlibrary{decorations.footprints}
\usetikzlibrary{shadows}

\begin{document}
    \begin{center}
        \begin{tikzpicture}[shorten >=1pt, node distance=3cm, auto, on grid, initial text=,font=\small,>=stealth',
            every state/.style={minimum size=7mm,draw=black!50,very thick,fill=red!50}]
        \node[state,initial]        (S)                 {S};
        \node[state]                (A) [right=of S]    {A};
        \node[state]                (B) [below=of S]    {B};
        \node[state,accepting]      (C) [right=of B]    {C};
        \path[->]
        (S) edge[bend right]    node{a} (A)
        (S) edge[bend right]    node[xshift=-4mm]{$\lambda$} (B)
        (A) edge[bend right]    node[yshift=5mm]{a,b} (S)
        (A) edge[]              node{$\lambda$} (C)
        (A) edge[]              node[yshift=7mm]{b} (B)
        (B) edge[bend left]     node{b, $\lambda$} (C)
        (B) edge[bend right]    node[xshift=4mm]{a} (S)
        (C) edge[bend left]     node{a,b} (B)

        ; 
        \end{tikzpicture}
    \end{center}
\end{document}

Best Answer

The spy TikZ-library was created exactly for this. Here an example. See the PGF/TikZ manual in section 49 Spy Library: Magnifying Parts of Pictures, page 462 for more details.

\documentclass[10pt,letterpaper]{article}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}

\usepackage[version=0.96]{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,automata,backgrounds,petri,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.fractals}
\usetikzlibrary{decorations.footprints}
\usetikzlibrary{shadows}
\usetikzlibrary{calc}
\usetikzlibrary{spy}

\begin{document}
    \begin{center}
        \begin{tikzpicture}[shorten >=1pt, node distance=3cm, auto, on grid, initial text=,font=\small,>=stealth',
            every state/.style={minimum size=7mm,draw=black!50,very thick,fill=red!50},spy using outlines]
        \node[state,initial]        (S)                 {S};
        \node[state]                (A) [right=of S]    {A};
        \node[state]                (B) [below=of S]    {B};
        \node[state,accepting]      (C) [right=of B]    {C};
        \path[->]
        (S) edge[bend right]    node{a} (A)
        (S) edge[bend right]    node[xshift=-4mm]{$\lambda$} (B)
        (A) edge[bend right]    node[yshift=5mm]{a,b} (S)
        (A) edge[]              node{$\lambda$} (C)
        (A) edge[]              node[yshift=7mm]{b} (B)
        (B) edge[bend left]     node{b, $\lambda$} (C)
        (B) edge[bend right]    node[xshift=4mm]{a} (S)
        (C) edge[bend left]     node{a,b} (B)

        ; 
        \spy [blue,draw,height=5cm,width=10cm,magnification=2,connect spies] on ($(B)!.5!(C)$) in node at ($(B)!.5!(C) + (0,-5) $);
        \end{tikzpicture}
    \end{center}
\end{document}

Result