但是我在箭头方面遇到了困难。我想知道是否有一种方法可以做到这一点,而无需对每个坐标进行硬编码。
以下是多部分矩形的代码:
\documentclass[]{article}
\usepackage{tikz}
\usepackage[skip=2pt]{caption}
\usetikzlibrary{positioning,decorations.pathreplacing, shapes, arrows}
\settowidth{\textwidth}{Multipart rectangles with no line shortening}
\begin{document}
\begin{tikzpicture}[auto, node distance=1cm,
rect/.style={
rectangle split,
rectangle split parts=3,
rectangle split horizontal,
rectangle split part fill={green!50,green!50,gray!50},
draw=black,
rounded corners,
text width = 3cm
}]
% Split Rectangle
\node[rect] {
\centerline{ether}
\nodepart {two} \centerline{ip}
\nodepart[text width=5cm]{three}
};
\end{tikzpicture}
\end{document}
答案1
如手册第 70.6 节所述具有多个文本部分的形状,节点部分之间的边界处有预定义的锚点。如果你将节点命名为foo
,那么foo.two split north
就是箭头指向的点,因此你可以这样做
\draw [<-] (foo.two split north) -- +(0,1cm) node[above]{bar};
要得到
\documentclass[]{article}
\usepackage{tikz}
\usepackage[skip=2pt]{caption}
\usetikzlibrary{positioning,decorations.pathreplacing, shapes, arrows}
\settowidth{\textwidth}{Multipart rectangles with no line shortening}
\begin{document}
\begin{tikzpicture}[auto, node distance=1cm,
rect/.style={
rectangle split,
rectangle split parts=3,
rectangle split horizontal,
rectangle split part fill={green!50,green!50,gray!50},
draw=black,
rounded corners,
text width = 3cm,
align=center % <- alternative to \centerline everywhere
}]
% Split Rectangle
\node[rect] (foo) {
ether
\nodepart {two} ip
\nodepart[text width=5cm]{three}
};
\draw [<-] (foo.two split north) -- +(0,1cm) node[above]{bar};
\end{tikzpicture}
\end{document}