Graphics, Figures & Tablestikz fit around everything

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
CoolnessItself
Posts: 47
Joined: Wed Nov 11, 2009 9:30 pm

tikz fit around everything

Post by CoolnessItself »

Hi all,
I'm trying to reproduce the state machine example with some minor changes. In particular, I'd like to draw a background circle encapsulating all elements of the picture. Here is a start:

Code: Select all

\usepackage{tikz}
\usetikzlibrary{arrows,automata,backgrounds,fit,decorations.pathreplacing}

%....

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                    semithick]
  \tikzstyle{every state}=[fill=white,draw=black,text=black]
  \tikzstyle{matrix}=[circle,thick,draw=gray!80,fill=gray!20]
    \tikzstyle{surround} = [circle,fill=blue!10,thick,draw=black,rounded corners=2mm]


  \node[state] (A)                    {$q_a$}; 
  \node[state]         (B) [above right of=A] {$q_b$}; 
  \node[state]         (D) [below right of=A] {$q_d$}; 
  \node[state]         (C) [below right of=B] {$q_c$}; 
  \node[state]         (E) [below of=D]       {$q_e$}; 

  \path (A) edge              node {0,1,L} (B)
            edge              node {1,1,R} (C)
        (B) edge [loop above] node {1,1,L} (B)
            edge              node {0,1,L} (C)
        (C) edge              node {0,1,L} (D)
            edge [bend left]  node {1,0,R} (E)
        (D) edge [loop below] node {1,1,R} (D)
            edge              node {0,1,R} (A)
        (E) edge [bend left]  node {1,0,R} (A);

\begin{pgfonlayer}{background} 
    \node[surround] (background) [fit = (A) (B)(C)(D)(E)] {};
    \end{pgfonlayer}
    %

\end{tikzpicture}
Later on, the number of states will be dynamic, and so I can't explicitly enumerate the fit=(A)(B) etc. Is there a way to simply grab everything in a particular scope? Also, the current background circle is missing the label "(B) edge [loop above] node {1,1,L} (B)"...

Suggestions?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikz fit around everything

Post by localghost »

Please get use to providing always full examples. Code snippets are not very helpful.

Regarding your problem you could shift the background node vertically. I consider this solution not very proper, but it does the job.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,backgrounds,fit,decorations.pathreplacing}

\begin{document}
  \begin{tikzpicture}[%
    ->,
    >=stealth',
    shorten >=1pt,
    auto,
    node distance=2.8cm,
    semithick
  ]
    \tikzstyle{every state}=[fill=white,draw=black,text=black]
    \tikzstyle{matrix}=[circle,thick,draw=gray!80,fill=gray!20]
    \tikzstyle{surround} = [circle,fill=blue!10,thick,draw=black,rounded corners=2mm]

    \node[state] (A)                    {$q_a$};
    \node[state] (B) [above right of=A] {$q_b$};
    \node[state] (D) [below right of=A] {$q_d$};
    \node[state] (C) [below right of=B] {$q_c$};
    \node[state] (E) [below of=D]       {$q_e$};

    \path (A) edge              node {0,1,L} (B)
              edge              node {1,1,R} (C)
          (B) edge [loop above] node {1,1,L} (B)
              edge              node {0,1,L} (C)
          (C) edge              node {0,1,L} (D)
              edge [bend left]  node {1,0,R} (E)
          (D) edge [loop below] node {1,1,R} (D)
              edge              node {0,1,R} (A)
          (E) edge [bend left]  node {1,0,R} (A);

    \begin{pgfonlayer}{background}
      \node[surround,yshift=15pt] (background) [fit=(A)(B)(C)(D)(E)] {};
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}
Perhaps there is a direct solution regarding the fit around the nodes (A)-(E). At the moment I can't figure out.


Thorsten
Post Reply