96

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.

alt text

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

Thanks for your help.

4
  • In Tikz 3 this is now really easy. It would be great to see a tikz 3.0 implementation of this whole thing. I may create one later if no one else does, Apr 27, 2015 at 5:14
  • @LyndonWhite what kind of command were you thinking of ?
    – Thomas
    Jan 24, 2017 at 10:36
  • Using subgraphs, and the graph drawing library I think. But I am no longer a fan of that way -- Abusing Graphs to make diagrams is useful for something quick, but aweful for something precise. Jan 24, 2017 at 12:43
  • 1
    @LyndonWhite I cannot state enough how much your comment has helped me. All I ever saw was the manual positioning of nodes, and I never had an Idea features like described here even existed. This is a gamechanger for me. Mar 30, 2020 at 20:25

4 Answers 4

46

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}

enter image description here

1
  • 1
    How would you insert text around the dotted squares?
    – Hel
    Dec 29, 2017 at 14:42
84
\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}

4
  • 1
    I guess the question from the OP is how to draw boxes around sets of named nodes, without having to guess the appropriate coordinates. Jan 13, 2011 at 17:42
  • @juan - you are correct, I would like to draw around set of nodes to take the guess work out.
    – Aku
    Jan 13, 2011 at 17:47
  • 1
    @Aku: I edited my answer accordingly.
    – morbusg
    Jan 13, 2011 at 17:50
  • 4
    You might also want to specify inner sep to change how loosely the rectangle fits around the nodes.
    – Caramdir
    Jan 13, 2011 at 18:10
34

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}

and the result

alt text

Hope it helps.

1
  • This should be the correct answer, solved me so many headaches! Apr 22, 2022 at 9:15
0

For exercises :-).

With use of the \arrows.meta library for arrows heads, chains and positioning for relative positioning of nodes:

\begin{document}
    \begin{figure}[htb]
\centering
    \begin{tikzpicture}[
node distance = 3mm and 6mm,
  start chain = going below,
 block/.style = {draw=blue, rounded corners, thick, fill=blue!20, 
                text width=5em, align=center, minimum height=2em,
                on chain},
  arr/.style = {draw, thick, -{Latex[length=2mm]}, shorten >=1pt},
  FIT/.style args = {#1/#2}{draw=#1, thick, fit=#2,
                            inner xsep=3mm, inner ysep=1.5mm}
                        ]
\node (A) [block]  {Subanta};
    \begin{scope}[nodes=block]
\node (B) [below  left=of A]    {Pumlinga};
\node (C) {Strilinga};
\node (D) {Napumsaka};
\node (E) [below right=of A]    {Pumlinga};
\node (F) {Strilinga};
\node (G) {Napumsaka};
    \end{scope}
\node[FIT=red/(E) (G),dotted,inner ysep=3mm] {};
\node[FIT=   /(D) (G),densely dotted,inner xsep=5mm] {};
%
\coordinate[below=of A.south west] (auxL);
\coordinate[below=of A.south east] (auxR);
%
\draw[arr]  (A) |- (auxL) |- (B);
\draw[arr]  (auxL |- B) |- (C);
\draw[arr]  (auxL |- C) |- (D);
%
\draw[arr]  (A) |- (auxR) |- (E);
\draw[arr]  (auxR |- E) |- (F);
\draw[arr]  (auxR |- F) |- (G);
   \end{tikzpicture}
\caption{Summary of Linga} 
\label{linga}
    \end{figure}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.