This is my tikz code :
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.geometric,arrows,fit,calc,positioning,automata,}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,auto,node distance=5 cm, scale = 0.6, transform shape]
\node[initial,state] (A) {$q_0$};
\node[state] (B) [right of=A,node distance=3 cm] {$q_1$};
\node[state] (C) [right of=B,node distance = 6.5 cm] {$q_2$};
\node[state] (D) [below right of=B] {$q_3$};
\node[state] (E) [below right of=D] {$q_5$};
\node[state] (F) [below left of=D] {$q_4$};
\path[->] (A) edge [above] node [align=center] {} (B)
(B) edge [loop above] node [align=center] {} (B)
edge [bend right,below] node [align=center] {} (C)
edge [left] node [align=center] {} (D)
(C) edge [loop above] node [align=center] {} (C)
edge [bend right,right] node [above,align=center] {} (B)
edge [right] node [align=center] {} (D)
(D) edge [bend left,right] node [align=center] {} (E)
edge [bend right,left] node [pos=0.4,align=center] {} (F)
(E) edge [bend left] node [pos=0.2,align=center] {} (D)
(F) edge [bend right,above] node [pos=0.6,align=center] {} (D)
edge [bend left] node [pos=0.7,align=center] {} (A);
\end{tikzpicture}
\end{document}
And this is the output:
I want to draw container boxes (possibly with dashed border) around {q1,q2,q3} and {q3,q4,q5} states and label them. How to do that in tikz?
I want the following output:
Best Answer
This is a job for the
fit
library.1. Initial Guess:
Using the
fit=
and specifying the nodes around which you want the box fit:gets you pretty close to what you want:
Now it is just a matter of adjusting the boxes.
2. Add
inner sep=
Note that the boxes are tightly fit around the specified nodes. So, that makes me think that we should increase the
inner sep
. So if you addinner sep=0.75cm
to the first rectangle, andinner sep=1.00cm
to the second rectangle:you get:
3. Tweaking: Line Styles:
You also asked for a dashed box, so adding
dashed
to the list options, and making the lines thicker withultra thick
we get:Perhaps using different colors for the lines and adding some fill color with an opacity will make the group easier to see:
4. Adding a Label
You can add a label is to name the box produced by the
fit=
option andanchor
the label to one of the points along the boundary:5. Tidying Up:
Now it looks like we are done. But wait, the overlap of the blue box with the red box is not quite symmetrical. Seems as if we could just stretch the blue box on to the left slightly things would look much better. So if somehow we could convince the
fit
library that the node(F)
(corresponding toq4
) was a bit to the left, then things would have worked. Basically we need to Make some extra space when using TikZ fit package. So, lets define a new point a bit to the left of the west side of(F)
:then we just give
(F')
to thefit
library instead of(F)
.6. Use layers
Using
layers
allows you to place the fit in thebackground
layer so that the drawing elements are on top and do not have any opacity effects.Note:
Q0
,Q1
, ... would have been ideal. Updated solutionwith layers
has these.layers
solution required additional tweaks in the placement of the bounding box -- not sure why.The complete code is below, but basically all I did was add these lines:
Code: Without
layers
:Code: with
layers
: