Home > tikz pgf > Tikz - How to Draw Boxes Around Set of Nodes

# Tikz - How to Draw Boxes Around Set of Nodes

January 13Hits:4

How to draw the boxes as shown in the attached image? I could only manage to draw the chart but could not highlight the desired node / nodes with a rectangle box. Preferably dotted. I could not draw dotted in MS paint.

\documentclass{article} \usepackage{tikz} \begin{document} \begin{figure}[!htb] \begin{center} \begin{tikzpicture} [auto,  block/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},  block1/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},  line/.style ={draw, thick, -latex',shorten >=2pt},  cloud/.style ={draw=red, thick, ellipse,fill=red!20,  minimum height=1em}] \draw (2.5,-2) node[block] (C) {Subanta}; \path (0,-3) node[block] (G){ Pumlinga}       (0,-4) node[block] (H){ Strilinga}       (0,-5) node[block] (I){ Napumsaka}       (5,-3) node[block] (J){ Pumlinga}       (5,-4) node[block] (K){ Strilinga}       (5,-5) node[block] (L){ Napumsaka}; \draw (C.south) -- ++(0,-0.25) coordinate (linga); \draw (linga) -- ++(-1,0) coordinate (ling); \draw[-latex] (ling) |- (G.east); \draw[-latex] (ling) |- (H.east); \draw[-latex] (ling) |- (I.east); \draw (linga) -- ++(1,0) coordinate (hling); \draw[-latex] (hling) |- (J.west); \draw[-latex] (hling) |- (K.west); \draw[-latex] (hling) |- (L.west); \end{tikzpicture} \end{center} \caption{Summary of Linga } \label{linga} \end{figure} \end{document} 


With the help of the »calc« library you can get it right.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[%
auto,
block/.style={
rectangle,
draw=blue,
thick,
fill=blue!20,
text width=5em,
align=center,
rounded corners,
minimum height=2em
},
block1/.style={
rectangle,
draw=blue,
thick,
fill=blue!20,
text width=5em,
align=center,
rounded corners,
minimum height=2em
},
line/.style={
draw,thick,
-latex',
shorten >=2pt
},
cloud/.style={
draw=red,
thick,
ellipse,
fill=red!20,
minimum height=1em
}
]
\draw (2.5,-2) node[block] (C) {Subanta};
\path (0,-3) node[block] (G) {Pumlinga}
(0,-4) node[block] (H) {Strilinga}
(0,-5) node[block] (I) {Napumsaka}
(5,-3) node[block] (J) {Pumlinga}
(5,-4) node[block] (K) {Strilinga}
(5,-5) node[block] (L) {Napumsaka};
\draw (C.south) -- ++(0,-0.25) coordinate (linga);
\draw (linga) -- ++(-1,0) coordinate (ling);
\draw[-latex] (ling) |- (G.east);
\draw[-latex] (ling) |- (H.east);
\draw[-latex] (ling) |- (I.east);
\draw (linga) -- ++(1,0) coordinate (hling);
\draw[-latex] (hling) |- (J.west);
\draw[-latex] (hling) |- (K.west);
\draw[-latex] (hling) |- (L.west);
\draw[red,thick,dotted] ($(J.north west)+(-0.3,0.6)$)  rectangle ($(L.south east)+(0.3,-0.6)$);
\draw[thick,dotted]     ($(I.north west)+(-0.5,0.15)$) rectangle ($(L.south east)+(0.5,-0.15)$);
\end{tikzpicture}
\end{document}



\node[draw,dotted] {A};



or

\draw[dotted] (0,0) rectangle (2,2);



Other options would be densely dotted, loosely dotted, dashed, densely dashed, and loosely dashed.

If you want to fit stuff, use the fit-library (example in Plain):

\input tikz
\usetikzlibrary{fit}
\tikzpicture
\foreach \x/\z in {A/1,B/2,C/3,D/4,E/5,F/6,G/7} {
\node at (\z,0) (\x) {\x};
}
\node[draw,dotted,fit=(A) (B) (C)] {};
\endtikzpicture
\bye



Following Caramdir's suggestion on inner sep, I composed a LaTeX example with some additional tikz libraries which you might be interested in:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{ matrix,      % For easy node positioning
fit,         % For easily fitting nodes inside another one
positioning, % For easy node-relative placements
}
\begin{document}
\begin{tikzpicture}[ % Dims on their own lines for easy tweakage
myscope/.style={node distance=1em and 0em},
mymatrix/.style={matrix of nodes, nodes=block,
column sep=2em,
row sep=1em},
block/.style={draw=blue, thick, fill=blue!20, rounded corners,
minimum width=6em,
minimum height=2em},
vhilit/.style={draw=red, thick, dotted,
inner sep=1em},
hhilit/.style={draw=black, thick, densely dotted,
inner xsep=2em,
inner ysep=.5em},
line/.style={thick, -latex, shorten >= 2pt}
]

\matrix[mymatrix] (mx) { % Usage of matrix-lib
& Subanta \\
Pumlinga  & & Pumlinga \\
Strilinga & & Strilinga \\
Napumsaka & & Napumsaka \\
}; % Now you can refer to nodes as "matrixname-rownumber-columnnumber"

\begin{scope}[myscope]
\coordinate[below left=of mx-1-2] (leftBranch); % Usage of positioning-lib
\coordinate[below right=of mx-1-2] (rightBranch);
\end{scope}

\draw[thick] (mx-1-2) |- (leftBranch) -- (rightBranch);

\foreach \x in {2,3,4} {
\draw[line] (leftBranch)  |- (mx-\x-1);
\draw[line] (rightBranch) |- (mx-\x-3);
}

\node[vhilit, fit=(mx-2-3) (mx-3-3) (mx-4-3)] {}; % Usage of fit-lib
\node[hhilit, fit=(mx-4-1) (mx-4-3)] {};

\end{tikzpicture}
\end{document}



You can use the fitting library from tikz. Here you have your modified example.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,fit} %use shapes library if you need ellipse
\begin{document}
\begin{figure}[!htb]
\begin{center}
\begin{tikzpicture}
[auto,
block/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},
block1/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},
line/.style ={draw, thick, -latex',shorten >=2pt},
cloud/.style ={draw=red, thick, ellipse,fill=red!20,
minimum height=1em}]
\draw (2.5,-2) node[block] (C) {Subanta};
\path (0,-3) node[block] (G){ Pumlinga}
(0,-4) node[block] (H){ Strilinga}
(0,-5) node[block] (I){ Napumsaka}
(5,-3) node[block] (J){ Pumlinga}
(5,-4) node[block] (K){ Strilinga}
(5,-5) node[block] (L){ Napumsaka};
\draw (C.south) -- ++(0,-0.25) coordinate (linga);
\draw (linga) -- ++(-1,0) coordinate (ling);
\draw[-latex] (ling) |- (G.east);
\draw[-latex] (ling) |- (H.east);
\draw[-latex] (ling) |- (I.east);
\draw (linga) -- ++(1,0) coordinate (hling);
\draw[-latex] (hling) |- (J.west);
\draw[-latex] (hling) |- (K.west);
\draw[-latex] (hling) |- (L.west);

\node[draw=red, fit=(I) (L)](FIt1) {};
\node[ellipse,draw=green, fit=(J) (L),inner sep=3mm](FIt2) {};

\end{tikzpicture}
\end{center}
\caption{Summary of Linga } \label{linga}
\end{figure}
\end{document}\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,fit}
\begin{document}
\begin{figure}[!htb]
\begin{center}
\begin{tikzpicture}
[auto,
block/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},
block1/.style ={rectangle, draw=blue, thick, fill=blue!20, text width=5em,align=center, rounded corners, minimum height=2em},
line/.style ={draw, thick, -latex',shorten >=2pt},
cloud/.style ={draw=red, thick, ellipse,fill=red!20,
minimum height=1em}]
\draw (2.5,-2) node[block] (C) {Subanta};
\path (0,-3) node[block] (G){ Pumlinga}
(0,-4) node[block] (H){ Strilinga}
(0,-5) node[block] (I){ Napumsaka}
(5,-3) node[block] (J){ Pumlinga}
(5,-4) node[block] (K){ Strilinga}
(5,-5) node[block] (L){ Napumsaka};
\draw (C.south) -- ++(0,-0.25) coordinate (linga);
\draw (linga) -- ++(-1,0) coordinate (ling);
\draw[-latex] (ling) |- (G.east);
\draw[-latex] (ling) |- (H.east);
\draw[-latex] (ling) |- (I.east);
\draw (linga) -- ++(1,0) coordinate (hling);
\draw[-latex] (hling) |- (J.west);
\draw[-latex] (hling) |- (K.west);
\draw[-latex] (hling) |- (L.west);

%Two nodes fitting your selected ones
\node[draw=red, fit=(I) (L)](FIt1) {};
\node[ellipse,draw=green, fit=(J) (L)](FIt2) {};

\end{tikzpicture}
\end{center}
\caption{Summary of Linga } \label{linga}
\end{figure}
\end{document}

`

and the result

Hope it helps.

## Related Articles

• ### Tikz - How to Draw Boxes Around Set of NodesJanuary 13

How to draw the boxes as shown in the attached image? I could only manage to draw the chart but could not highlight the desired node / nodes with a rectangle box. Preferably dotted. I could not draw dotted in MS paint. \documentclass{article} \usepac

• ### How to put part of a TikZ graphic into a box to measure its sizeMay 19

After adding support for tooltips today in pdfcomment (\pdftooltip) I wanted to make a TikZ example with tooltip annotations. Within \pdftooltip the annotated element is set into a box to measure the size, which does not work for parts of a TikZ grap

• ### Drawing box in matrix in beamer January 4

I have a matrix and need to highlight some elements in the matrix. I have found how to highlight a single element (I want to put the cell in the square):  A = \begin{bmatrix} \fbox{0} & \fbox{0} & 0 \\ \fbox{0} & \fbox{0} & 0 \\ 1 &

• ### Tikz: how to draw a horizontal line between 2 nodesSeptember 15

Code: \documentclass[preview]{standalone} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{tikz} \usetikzlibrary{positioning} \begin{document} \begin{tikzpicture} \node [draw] (1) {Node}; \node [draw, below = of 1] (1) {Long text};

• ### How can I remove hardcoded numbers from this TikZ algorithm for drawing a circle of circles?October 6

I have some TikZ code for drawing a circle of little circle images (for showing pictures of a team of people). The number of people shown can vary, so I'm trying to remove hardcoded numbers to make it easier to change the code for different numbers o

• ### A Program that can draw boxes on the Desktop?February 4

I'm looking for a program that can draw boxes around groups of icons on my desktop. Kind of a way to group similar items. Something that persists through a reboot as well. Has anyone seen anything like this? --------------Solutions------------- Stard

• ### Draw an arc between 2 nodes and label it in TikZSeptember 2

I have already drawn two ellipses l1 and l2, l2 is on the left below l1: \begin{tikzpicture} \node (l1) [ellipse, draw=black, fill=white!20, text=black, scale=0.8, text centered]{ $l_1$}; \node (l2) [ellipse, draw=black, fill=white!20, text=black, sc

• ### Using TikZ, how to draw an elliptical arc starting from point A to point B with the origin as its center where both radii are given?March 9

I am new to TikZ and reading its manual now. For the sake of exercises, I want to draw Batman's logo as follows: Using TikZ, how to draw an elliptical arc starting from point A to point B with the origin as its center? \documentclass{article} \usepac

• ### TikZ: how to draw chained tabular materialJune 10

I am new to TikZ, and this is probably regarded as do-my-homework-for-me question. I'd like to draw a diagram like this: First I need the very basic help on the segments themselves. I have tried nodes with a tabular environment: \documentclass{standa

• ### How to draw saturation symbol inside a node in TikZAugust 1

I'm working on a block diagram using TikZ. I'd like to create a symbol like below, which means I have to draw 5 lines inside a node and write a letter in the upper-right corner. What's the best way to achieve what I need? Any hints appreciated. -----

• ### Tikz library : command \draw - to- with out and in specificationNovember 29

I would like to ask how the following command works: \draw [] (0,0) to [out=20,in=120] (50,20) to [out=-60, in=110] (90,20); I wonder what part .. to [out=20,in=120] .. makes. The result is a curve, but I don't know how to properly understand these p

• ### TikZ how to draw lines to/from nodes correctly? December 18

I have the following tikz code % compilation command: pdflatex --jobname=Diag-f1 Diag.tex \documentclass{book} \usepackage{tikz} \pgfrealjobname{Diag} \begin{document} \beginpgfgraphicnamed{Diag-f1} \begin{tikzpicture}[node distance=3em] \node(G)[rec

• ### Drawing a polyline between two nodes in tikzJune 2

I am a beginner with tikz and finding the following really difficult to do. I have defined and drawn node1 and node2 in tikz. You can figure it out easily from the picture. What I intend to do is Draw a dashed line from (node1.south) to P1. Draw a da

• ### TikZ - Recursive Arc DrawingMarch 16

I'm trying to make Farey diagrams similar to those seen here (click) or the screenshot as follows, Namely, I'd like an example of the one seen on the top of page 6. From there I hope I can figure out how to do the rest. Anyway, I was wanting to do th

• ### Showing weights on Tikz graph using "draw edge" commandsMarch 17

I would like to show some numbers as weights of the edges of my Tikz graph. My graph without weights is the following: \begin{figure}[!ht] \begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick] \tikzstyle{node_style} = [circle,draw=

• ### TikZ code for drawing an icosahedron and labelingJune 4

I found TikZ code for drawing an icosahedron here: \NeedsTeXFormat{LaTeX2e}[1995/06/01] \documentclass[a4paper]{amsart} \usepackage{graphics, tikz, tkz-berge, tkz-graph} \begin{document} \begin{figure} \begin{tikzpicture} \begin{scope}[rotate=90] \gr

• ### Draw Tikz Tree where level distance between multi-line nodes is constantMarch 26

I am drawing a tree with tikz, namely, using tikz-qtree-compat for simpler syntax. On the tree there can be nodes of different height but then branches become messy. I want to add an instruction where distance between the south point of a mother node

• ### Draw ellipsis (three dots) between nodes in tikzJune 1

I am beginning to learn tikz to draw a simple graph. I would like to represent the idea that there are N nodes by drawing something like this: How can I draw an ellipsis: ... between the two nodes? Here is the code I used for the above: \documentclas

• ### TikZ: how to draw a diamond childAugust 27

I neew to draw an "easy" flowchart. I would do that, using che child statement. I've used (as example) an example found here. I've m odified it, that is the code: \documentclass{article} \usepackage{tikz} \usetikzlibrary{trees} \usepackage[activ

• ### Drawing Boxes in PDF::ReuseFebruary 14

I am looking for more examples or documentation for drawing boxes using PDF::Reuse. I would like to be able to: 1. Draw a box with no fill so that text can be printed inside the box. (Is this possible?) 2. Draw a shaded box with no border and text. 3