在下图中,我希望箭头起源于框内而不是边框内,可能与文本对齐并位于文本旁边(例如,紧接着单词“Pointer:”。
这是我使用的代码:
\usetikzlibrary{shapes.multipart,arrows}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}
\tikzstyle{fnode}=[rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners]
\begin{tikzpicture}
\node[fnode] (r1) at (0,0) {Pointer: \npd Key \npt Text text \npq Pointers \npc text};
\draw [*->] (r1.one east) to (-2,2);
\draw [*->] (r1.four east) to (3,0);
\draw [*->] (r1.four west) to (-3,0);
\end{tikzpicture}
我还想(如果可能的话)使用类似于锚点的语法。手册中没有预定义的锚点来执行此操作。实现此结果的最简单方法是什么?
答案1
这是一个可能的解决方案。
您应该指定anchormark
您应该在左侧或右侧指针通过\anchormark
;其定义是:
\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
\tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}
每个都anchormark
应该被唯一地标识(命令的强制参数),并且可以被移动(命令的第一个参数,可选),或者圆可以缩放(命令的第三个参数,再次是可选的)。
请注意,使用这种方法时,您应该从逻辑上划分部署fnode
标记的时刻和创建连接的时刻。您还应该编译两次。
完整示例:
\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,arrows}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}
\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
\tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}
\tikzset{fnode/.style={rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners}}
\tikzset{label style/.style={draw, rounded corners}}
\begin{document}
% node deployment
\begin{tikzpicture}
\node[fnode] (r1) at (0,0) {Pointer:\anchormark{first pointer}[0.075] \npd Key \npt Text text \npq \anchormark[-0.15cm]{pointer left}Pointers \anchormark{pointer right}\npc text};
\end{tikzpicture}
% connection deployment
\begin{tikzpicture}[remember picture,overlay,-stealth]
\draw (first pointer.center) to (2,2.75) node[right,label style]{label};
\draw (pointer right.center) to (2,1) node[right,label style]{label};
\draw (pointer left.center) to (-5,1)node[left,label style]{label};
\end{tikzpicture}
\end{document}
结果:
这个高级示例展示了如何处理更多模块。正如评论中所说,通过在部署节点的位置添加[remember picture]
,tikzpicture
可以在连接阶段访问它们的锚点。该示例的另一个特点是使用库来calc
避免在连接阶段进行绝对定位。
\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,arrows,calc}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}
\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
\tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}
\tikzset{fnode/.style={rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners}}
\tikzset{label style/.style={draw, rounded corners}}
\begin{document}
\begin{tikzpicture}[remember picture]
\node[fnode] (r1) at (0,0) {Pointer:\anchormark{first pointer}[0.075] \npd Key \npt Text text \npq \anchormark[-0.15cm]{pointer left}Pointers \anchormark{pointer right}\npc text};
\node[fnode] (r2) at (5,0) {Pointer:\anchormark{second pointer}[0.075] \npd Key \npt Text text \npq \anchormark[-0.05cm]{second pointer left}Second pointer\npc text};
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay,-stealth]
\draw (first pointer.center) to ($(first pointer.center)+(2,1)$) node[right,label style] (mylabel){label};
\draw (pointer right.center) to (second pointer left);
\draw (pointer left.center) to ($(pointer left.center)+(-1,1)$)node[left,label style]{label};
\draw (second pointer.center) |-(mylabel);
% just to connect r1 and r2
\draw(r1.south)|- ($(r1.south)!0.5!(r2.south)-(0,1)$)-|(r2.south);
\end{tikzpicture}
\end{document}
结果: