Tikz - 如何在节点集周围绘制框

Tikz - 如何在节点集周围绘制框

如何绘制如附图所示的框?我只能绘制图表,但无法用矩形框突出显示所需的节点。最好是虚线。我无法在 MS 画图中绘制虚线。

替代文本

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

感谢您的帮助。

答案1

在»calc«库的帮助下,您可以得到正确的结果。

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

在此处输入图片描述

答案2

\node[draw,dotted] {A};

或者

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

其他选项包括densely dotted、、、和。loosely dotteddasheddensely dashedloosely dashed

如果您想要适合的东西,请使用-library fit(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

根据 Caramdir 的建议inner sep,我编写了一个 LaTeX 示例,其中包含一些您可能会感兴趣的其他 tikz 库:

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

答案3

您可以使用 tikz 的拟合库。这里有您修改后的示例。

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

结果

替代文本

希望能帮助到你。

答案4

用于练习:-)。

使用\arrows.meta箭头库chainspositioning节点的相对定位:

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

在此处输入图片描述

相关内容