我正在尝试绘制如图所示的框图。我尝试绘制但不知道如何添加箭头并标记我使用的大框。有人能帮忙绘制这样的框图吗?无论是从现有代码完成还是通过新方法?
我尝试通过以下代码进行绘制:
\begin{figure*}
\begin{center}
\begin{tikzpicture}[shorten >=1pt,node distance=3.7cm,>=stealth',bend angle=45]
\tikzstyle{block} = [rectangle, draw=black, thick, text width=0.5em, text centered, minimum height=1em]
\tikzstyle{block1}=[rectangle, draw=black, thick, text width=2em, text centered,minimum height=3em]
\tikzstyle{block2}=[rectangle, draw=black, thick, text width=4em, text centered,minimum height=2em]
\tikzstyle{block3}=[rectangle, draw=black, thick, text width=8em, text centered,minimum height=5em]
\tikzstyle{bigbox} = [draw=black,thick, rectangle]
\tikzstyle{triple}=[draw, anchor=text, thick, rectangle split,rectangle split parts=3]
\tikzstyle{double}=[draw, anchor=text, thick, rectangle split,rectangle split parts=2]
%\tikzstyle{textbox} = {draw, fill=white, thick,text=black}
\node[block] (A) {};
\node[block] (B) [below=0.2cm of A] {};
\node[block] (C) [below=0.2cm of B] {};
\node[block] (D) [below=0.2cm of C] {};
\node[block] (E) [below=0.2cm of D] {};
\node[block] (F) [below=0.2cm of E] {};
\node[block] (G) [right=0.4cm of C] {};
\node[block] (H) [right=0.1cm of G] {};
\node[block] (I) [right=0.1cm of H] {};
\node[block] (J) [right=0.3cm of I] {};
\node[block] (K) [right=0.1cm of J] {};
\node[block] (L) [right=0.1cm of K] {};
\node[block] (P) [right=2cm of L] {};
\node[block] (Q) [right=0.1cm of P] {};
\node[block] (R) [right=0.1cm of Q] {};
\node[block] (S) [right=0.3cm of R] {};
\node[block] (T) [right=0.1cm of S] {};
\node[block] (U) [right=0.1cm of T] {};
\node[block] (X) [right=0.3cm of U] {};
\node[block] (Y) [above=0.2cm of X] {};
\node[block] (Z) [above=0.2cm of Y] {};
\node[block] (Aa) [below=0.2cm of X] {};
\node[block] (Ab) [below=0.2cm of Aa] {};
\node[block] (Ac) [below=0.2cm of Ab] {};
\begin{pgfonlayer}{background}
\node[bigbox] (M) [fit = (G) (H) (I)] {};
\node[bigbox] (N) [fit = (J) (K) (L)] {};
\node[bigbox] (V) [fit = (P) (Q) (R)] {};
\node[bigbox] (W) [fit = (S) (T) (U)] {};
\node[bigbox] (O) [fit = (A) (B) (C) (D) (E) (F) (G) (H) (I) (J) (K) (L) (N)] {};
\node[bigbox] (Ad) [fit = (P) (Q) (R) (S) (T) (U) (V) (W) (Aa) (Ac) (Z)] {};
\end{pgfonlayer}
\end{tikzpicture}
\caption{Proposed Framework} \label{fig1}
\end{center}
\end{figure*}
答案1
虽然jakun
已经提供了解决方案,但这是一个替代解决方案。
Source
并Destination
可以添加为最大fit
节点的标签:
\node[bigbox, label=source] (O) [fit = (A) (F) (N)] {};
(我简化了fit
参数,仅包含重要节点,而不是全部)
作为替代方案jakun's
transform canvas
(我不知道),您可以使用:
\draw[<-] ([yshift=8mm]O.south east) coordinate (aux) -- node[below] {Ack} (aux-|Ad.west);
或者使用calc
tikzlibrary:
\draw[->] ($(O.north east)!.2!(O.south east)$) -- ($(Ad.north west)!.2!(Ad.south west)$);
完整的代码可能是:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, backgrounds, fit}
\usepackage{lmodern}
\begin{document}
\begin{figure*}
\begin{center}
\begin{tikzpicture}[shorten >=1pt,node distance=3.7cm,>=stealth',bend angle=45, font=\sffamily]
\tikzstyle{block} = [rectangle, draw=black, thick, text width=0.5em, text centered, minimum height=1em]
\tikzstyle{block1}=[rectangle, draw=black, thick, text width=2em, text centered,minimum height=3em]
\tikzstyle{block2}=[rectangle, draw=black, thick, text width=4em, text centered,minimum height=2em]
\tikzstyle{block3}=[rectangle, draw=black, thick, text width=8em, text centered,minimum height=5em]
\tikzstyle{bigbox} = [draw=black,thick, rectangle]
\tikzstyle{triple}=[draw, anchor=text, thick, rectangle split,rectangle split parts=3]
\tikzstyle{double}=[draw, anchor=text, thick, rectangle split,rectangle split parts=2]
%\tikzstyle{textbox} = {draw, fill=white, thick,text=black}
\node[block] (A) {};
\node[block] (B) [below=0.2cm of A] {};
\node[block] (C) [below=0.2cm of B] {};
\node[block] (D) [below=0.2cm of C] {};
\node[block] (E) [below=0.2cm of D] {};
\node[block] (F) [below=0.2cm of E] {};
\node[block] (G) [right=0.4cm of C] {};
\node[block] (H) [right=0.1cm of G] {};
\node[block] (I) [right=0.1cm of H] {};
\node[block] (J) [right=0.3cm of I] {};
\node[block] (K) [right=0.1cm of J] {};
\node[block] (L) [right=0.1cm of K] {};
\node[block] (P) [right=2cm of L] {};
\node[block] (Q) [right=0.1cm of P] {};
\node[block] (R) [right=0.1cm of Q] {};
\node[block] (S) [right=0.3cm of R] {};
\node[block] (T) [right=0.1cm of S] {};
\node[block] (U) [right=0.1cm of T] {};
\node[block] (X) [right=0.3cm of U] {};
\node[block] (Y) [above=0.2cm of X] {};
\node[block] (Z) [above=0.2cm of Y] {};
\node[block] (Aa) [below=0.2cm of X] {};
\node[block] (Ab) [below=0.2cm of Aa] {};
\node[block] (Ac) [below=0.2cm of Ab] {};
\begin{pgfonlayer}{background}
\node[bigbox] (M) [fit = (G) (H) (I)] {};
\node[bigbox] (N) [fit = (J) (K) (L)] {};
\node[bigbox] (V) [fit = (P) (Q) (R)] {};
\node[bigbox] (W) [fit = (S) (T) (U)] {};
\node[bigbox, label=source] (O) [fit = (A) (F) (N)] {};
\node[bigbox, label=destination] (Ad) [fit = (Z) (Ac) (V)] {};
\end{pgfonlayer}
\path (M.south) -- node[below] {Packets} (N.south);
\path (V.south) -- node[below] {Packets} (W.south);
\draw[->] ([yshift=-8mm]O.north east) coordinate (aux) -- (aux-|Ad.west);
\draw[<-] ([yshift=8mm]O.south east) coordinate (aux) -- node[below] {Ack} (aux-|Ad.west);
\end{tikzpicture}
\caption{Proposed Framework} \label{fig1}
\end{center}
\end{figure*}
\end{document}
更新:借助一些matrix
节点可以简化代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, fit, matrix}
\usepackage{lmodern}
\begin{document}
\begin{figure*}
\begin{center}
\begin{tikzpicture}[%
shorten >=1pt,
shorten <=1pt,
>=stealth',
font=\sffamily,
block/.style = {rectangle, black, thick, text width=0.5em, text centered, minimum height=1em},
packet/.style={draw, thick, matrix of nodes, nodes in empty cells, nodes={block, draw}},
bigbox/.style={rectangle, draw, thick}
]
\matrix (A1) [packet, draw=none, row sep=2mm]{ \\ \\ \\ \\ \\ \\ };
\matrix (B1) [packet, column sep=2mm, right=3mm of A1]{ & & \\};
\matrix (C1) [packet, column sep=2mm, right=1mm of B1]{ & & \\};
\node[bigbox, label=Source] (D1) [fit = (A1) (C1)] {};
\matrix (C2) [packet, column sep=2mm, right=2cm of C1]{ & & \\};
\matrix (B2) [packet, column sep=2mm, right=1mm of C2]{ & & \\};
\matrix (A2) [packet, draw=none, row sep=2mm, right=3mm of B2]{ \\ \\ \\ \\ \\ \\ };
\node[bigbox, label=Destination] (D2) [fit = (A2) (C2)] {};
\path (B1.south) -- node[below] {Packets} (C1.south);
\path (B2.south) -- node[below] {Packets} (C2.south);
\draw[->, thick] ([yshift=-8mm]D1.north east) coordinate (aux) -- (aux-|D2.west);
\draw[<-, thick] ([yshift=8mm]D1.south east) coordinate (aux) -- node[below] {Ack} (aux-|D2.west);
\end{tikzpicture}
\caption{Proposed Framework} \label{fig1}
\end{center}
\end{figure*}
\end{document}
答案2
将以下内容添加到您的代码中:
% arrows
\draw[->, transform canvas={yshift=-1cm}] (O.north east) -- (Ad.north west);
\draw[<-, transform canvas={yshift=1cm}] (O.south east) -- node[below]{ACK} (Ad.south west);
% invisible coordinates to position the labels
\node (channel-y-position) [below=0pt of O] {\strut};
\path (O) -- coordinate (channel-x-position) (Ad);
% labels
\node[above=0pt of O] {Source};
\node[above=0pt of Ad] {Destination};
\node at (channel-x-position |- channel-y-position) {Channel};
我会使用循环:
\documentclass[border=.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, calc}
\begin{document}
\begin{tikzpicture}%[shorten >=1pt,node distance=3.7cm,>=stealth',bend angle=45]
\tikzstyle{block} = [rectangle, draw=black, thick, text width=0.5em, text centered, minimum height=1em]
\tikzstyle{block1}=[rectangle, draw=black, thick, text width=2em, text centered,minimum height=3em]
\tikzstyle{block2}=[rectangle, draw=black, thick, text width=4em, text centered,minimum height=2em]
\tikzstyle{block3}=[rectangle, draw=black, thick, text width=8em, text centered,minimum height=5em]
\tikzstyle{bigbox} = [draw=black,thick, rectangle]
\tikzstyle{triple}=[draw, anchor=text, thick, rectangle split,rectangle split parts=3]
\tikzstyle{double}=[draw, anchor=text, thick, rectangle split,rectangle split parts=2]
%\tikzstyle{textbox} = {draw, fill=white, thick,text=black}
\newcommand{\numVer}{6}
\newcommand{\numHor}{3}
\newcommand{\numHorGroups}{2}
% ------ source ------
% vertical
\node[block] (source-ver-1) {};
\foreach \i [remember=\i as \last (initially 1)] in {2,...,\numVer} {
\node[block] (source-ver-\i) [below=0.2cm of source-ver-\last] {};
}
% horizontal
\coordinate[right=.1cm of source-ver-3] (source-hor-group-0);
\foreach \group [remember=\group as \lastGroup (initially 0)] in {1,...,\numHorGroups} {
\node[block] (source-hor-\group-1) [right=0.3cm of source-hor-group-\lastGroup] {};
\foreach \i [remember=\i as \last (initially 1)] in {2,...,\numHor} {
\node[block] (source-hor-\group-\i) [right=.1cm of source-hor-\group-\last] {};
}
\node[bigbox] (source-hor-group-\group) [fit = (source-hor-\group-1) (source-hor-\group-\numHor), label=below:Packet~\group] {};
}
% frame
\node[bigbox, label=above:Source] (source) [fit = (source-ver-1) (source-ver-\numVer) (source-hor-group-\numHorGroups)] {};
% ------ destination ------
% horizontal
\coordinate[right=2cm of source-hor-\numHorGroups-\numHor] (dest-hor-group-0);
\foreach \group [remember=\group as \lastGroup (initially 0)] in {1,...,\numHorGroups} {
\node[block] (dest-hor-\group-1) [right=0.3cm of dest-hor-group-\lastGroup] {};
\foreach \i [remember=\i as \last (initially 1)] in {2,...,\numHor} {
\node[block] (dest-hor-\group-\i) [right=.1cm of dest-hor-\group-\last] {};
}
\node[bigbox] (dest-hor-group-\group) [fit = (dest-hor-\group-1) (dest-hor-\group-\numHor), label=below:Packet~\group] {};
}
% vertical
\coordinate[right=.3cm of dest-hor-group-\numHorGroups] (dest-ver-west);
\node[block] (dest-ver-1) at (dest-ver-west |- source-ver-1) [anchor=west] {};
\foreach \i [remember=\i as \last (initially 1)] in {2,...,\numVer} {
\node[block] (dest-ver-\i) [below=0.2cm of dest-ver-\last] {};
}
% frame
\node[bigbox, label=above:Destination] (dest) [fit = (dest-ver-1) (dest-ver-\numVer) (dest-hor-group-1)] {};
% ------ arrows ------
% arrows
\draw[->, transform canvas={yshift=-1cm}] (source.north east) -- (dest.north west);
\draw[<-, transform canvas={yshift=1cm}] (source.south east) -- node[below]{ACK} (dest.south west);
% invisible coordinates to position the labels
\node (channel-y-position) [below=0pt of source] {\strut};
\coordinate (channel-x-position) at ($(source.east)!.5!(dest.west)$);
% labels
\node at (channel-x-position |- channel-y-position) {Channel};
\end{tikzpicture}
\end{document}
答案3
我修改了代码的某些部分,例如获得更多方形节点,并将输出和输入缓冲区排列在数据包列表的中心。通过使用calc
库以及-|
,您可以将坐标操纵到您想要的位置。我不太关心字体。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,fit,calc}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=3.7cm,>=stealth',bend angle=45]
% \tikzstyle{block} = [rectangle, draw=black, thick, text width=0.5em, text centered, minimum height=1em]
\tikzstyle{block} = [rectangle, draw=black, thick, inner sep=0pt,minimum width=1em, minimum height=1em]
% \tikzstyle{block1}=[rectangle, draw=black, thick, text width=2em, text centered,minimum height=3em]
% \tikzstyle{block2}=[rectangle, draw=black, thick, text width=4em, text centered,minimum height=2em]
% \tikzstyle{block3}=[rectangle, draw=black, thick, text width=8em, text centered,minimum height=5em]
\tikzstyle{bigbox} = [draw=black,thick, rectangle]
% \tikzstyle{triple}=[draw, anchor=text, thick, rectangle split,rectangle split parts=3]
% \tikzstyle{double}=[draw, anchor=text, thick, rectangle split,rectangle split parts=2]
% \tikzstyle{textbox} = {draw, fill=white, thick,text=black}
\node[block] (A) {};
\node[block] (B) [below=0.2cm of A] {};
\node[block] (C) [below=0.2cm of B] {};
\node[block] (D) [below=0.2cm of C] {};
\node[block] (E) [below=0.2cm of D] {};
\node[block] (F) [below=0.2cm of E] {};
\node[block] (G) [right=0.4cm of {$(C.south east)!0.5!(D.north east)$}] {};
\node[block] (H) [right=0.1cm of G] {};
\node[block] (I) [right=0.1cm of H] {};
\node[block] (J) [right=0.3cm of I] {};
\node[block] (K) [right=0.1cm of J] {};
\node[block] (L) [right=0.1cm of K] {};
\node[block] (P) [right=2cm of L] {};
\node[block] (Q) [right=0.1cm of P] {};
\node[block] (R) [right=0.1cm of Q] {};
\node[block] (S) [right=0.3cm of R] {};
\node[block] (T) [right=0.1cm of S] {};
\node[block] (U) [right=0.1cm of T] {};
\node[block] (X) [right=0.3cm of {U.east |- C}] {};
\node[block] (Y) [above=0.2cm of X] {};
\node[block] (Z) [above=0.2cm of Y] {};
\node[block] (Aa) [below=0.2cm of X] {};
\node[block] (Ab) [below=0.2cm of Aa] {};
\node[block] (Ac) [below=0.2cm of Ab] {};
% \begin{pgfonlayer}{background}
\node[bigbox] (M) [fit = (G) (H) (I)] {};
\node[bigbox] (N) [fit = (J) (K) (L)] {};
\node[bigbox] (V) [fit = (P) (Q) (R)] {};
\node[bigbox] (W) [fit = (S) (T) (U)] {};
\node[bigbox,label=above:Source] (O) [fit = (A) (B) (C) (D) (E) (F) (G) (H) (I) (J) (K) (L) (N)] {};
\node[bigbox,label=above:Destination] (Ad) [fit = (P) (Q) (R) (S) (T) (U) (V) (W) (Aa) (Ac) (Z)] {};
% \end{pgfonlayer}
\draw[->](O.30)--(Ad.180 |- O.30);
\draw[<-](O.-30)--(Ad.180 |- O.-30)node[pos=0.5,below]{ACK};
\node[below=1em] at ($(O.south east)!0.5!(Ad.south west)$){CHANNEL};
\node[below] at ($(M.south)!0.5!(N.south)$){Packets};
\node[below] at ($(V.south)!0.5!(W.south)$){Packets};
\end{tikzpicture}
\end{document}