![在 TikZ 中重现 TCP/IP 协议栈](https://linux22.com/image/456839/%E5%9C%A8%20TikZ%20%E4%B8%AD%E9%87%8D%E7%8E%B0%20TCP%2FIP%20%E5%8D%8F%E8%AE%AE%E6%A0%88.png)
我一直在尝试在 TikZ 中重现以下 TCP/IP 协议栈。
我已经能够使用以下 MWE 来绘制它:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{lipsum}
\title{Figure of TCP-IP Stack}
\author{}
\date{October 2022}
\begin{document}
\maketitle
\section{Introduction}
\par In this section, an attempt will be made to draw the infamous TCP/IP Protocol stack using \LaTeX's \verb|tikz| package. It will be so much fun!
\begin{figure}
\centering
\begin{tikzpicture}
\draw(0,0) rectangle (4,6);
\node [] (l5) at (2,1) {Layer 5};
\draw (0,0) rectangle (4,1.5);
\node [] (l4) at (2,2) {Layer 4};
\draw (0,1.5) rectangle (4,2.5);
\node [] (l3) at (2,3) {Layer 3};
\draw (0,2.5) rectangle (4,3.5);
\node [] (l2) at (2,4) {Layer 2};
\draw (0,3.5) rectangle (4,4.5);
\node [] (l1) at (2,5) {Layer 1};
\node [] (t5) at (7,1) {Physical layer};
\draw[->] (l5) -- (t5);
\node [] (t4) at (7,2) {Data Link Layer};
\draw[->] (l4) -- (t4) ;
\node [] (t3) at (7,3) {Network layer};
\draw [->] (l3) --(t3);
\node [] (t2) at (7,4) {Transport layer};
\draw [->] (l2)--(t2);
\node [] (t1) at (7,5) {Application layer};
\draw [->] (l1)--(t1);
\end{tikzpicture}
\caption{My caption}
\label{fig:tcp stack}
\end{figure}
\lipsum
\end{document}
tikz 中的图像有些粗糙,因为可以通过每个已定义节点的定义来判断。我想知道如何重新绘制它,使其看起来更美观,适合期刊出版物,就像上面给出的 powerpoint 图像一样。
答案1
这是一个使用chains
库的解决方案(它跟踪所使用的前一个节点on chain
并以正确的方式自动引用它above=of <last chain node>
)和pin
自动添加另一个节点并通过边自动将其连接到其父节点的键。
我不喜欢负量shorten <
,但这是将引脚边缘延伸到父节点的最简单方法。
我使用循环count
中的变量来对节点进行编号。\foreach
\tikzchaincount
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,arrows.meta}
\begin{document}
\tikz[
start chain=going above,
node distance=+0pt,
every pin edge/.style={-{Triangle[angle'=45]}, shorten <=+-.5cm},
pin position=right,
pin distance=+1.5cm,
every on chain/.style={% not nodes!
minimum width=+3cm,
minimum height=+1cm,
draw, outer sep=+0pt}]
\foreach \t in {Physical layer,
Data Link layer,
Network Layer,
Transport layer,
Application layer}
\node[on chain, pin=\t] {Layer \tikzchaincount};
\end{document}
输出
答案2
也许有了split rectangle
和\foreach
循环事情会变得更加优雅?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usepackage{lipsum}
\title{Figure of TCP-IP Stack}
\author{}
\date{October 2022}
\begin{document}
\maketitle
\section{Introduction}
In this section, an attempt will be made to draw the infamous TCP/IP Protocol stack using \LaTeX's \verb|tikz| package. It will be so much fun!
\begin{figure}
\centering
\begin{tikzpicture}
\node[rectangle split, rectangle split parts=5, draw, minimum width=4cm, inner sep=.33cm] (box) at (2,0) {
Layer 5
\nodepart{two}
Layer 4
\nodepart{three}
Layer 3
\nodepart{four}
Layer 2
\nodepart{five}
Layer 1
};
\foreach \n/\l in {
text/Application Layer,
two/Transport Layer,
three/Network Layer,
four/Data Link Layer,
five/Physical Layer
} {
\draw[->] ([xshift=-1cm]box.\n\space east) -- ++(2,0) node[right] {\l};
}
\end{tikzpicture}
\caption{My caption}
\label{fig:tcp stack}
\end{figure}
\lipsum
\end{document}
答案3
另一种选择是使用两列matrix
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{lipsum}
\title{Figure of TCP-IP Stack}
\author{}
\date{October 2022}
\begin{document}
\maketitle
\section{Introduction}
In this section, an attempt will be made to draw the infamous TCP/IP Protocol stack using \LaTeX's \verb|tikz| package. It will be so much fun!
\begin{figure}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes, nodes={minimum width=4cm, minimum height=1cm},
column 1/.style={nodes=draw}, row sep=-\pgflinewidth] (Layers) {
Layer 5 & Application Layer\\
Layer 4 & Transport Layer \\
Layer 3 & Network Layer \\
Layer 2 & Data Link Layer \\
Layer 1 & Physical Layer \\
};
\foreach \i in {1,...,5}
\draw[->] ([xshift=-5mm]Layers-\i-1.east)--([xshift=5mm]Layers-\i-2.west);
\end{tikzpicture}
\caption{My caption}
\label{fig:tcp stack}
\end{figure}
\end{document}
更新:左对齐标签
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes, nodes={anchor=center, minimum height=1cm},
column 1/.style={nodes={draw, minimum width=3.5cm}},
column 2/.style={nodes={align=left, text width=3cm}},
row sep=-\pgflinewidth,
column sep=5mm] (Layers) {
Layer 5 & Application Layer\\
Layer 4 & Transport Layer \\
Layer 3 & Network Layer \\
Layer 2 & Data Link Layer \\
Layer 1 & Physical Layer \\
};
\foreach \i in {1,...,5}
\draw[->] ([xshift=-5mm]Layers-\i-1.east)--(Layers-\i-2.west);
\end{tikzpicture}
\caption{My caption}
\label{fig:tcp stack}
\end{figure}
\end{document}