TikZ 拆分部分中的锚点

TikZ 拆分部分中的锚点

我想使用 TikZ 绘制一个单链表。我的 MWE

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{arrows}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{chains}

\tikzset{
 anode/.style={rectangle, draw, minimum size = 12pt, inner sep = 4pt},
 snode/.style={rectangle split, rectangle split parts=2, draw, rectangle split horizontal, minimum size=18pt, inner sep=4pt, text=black},
>=Stealth}

\begin{document}

\begin{tikzpicture}[start chain]
\node[snode, on chain] (1) {25};
\node[snode, on chain] (2) {19};
\node[snode, on chain] (3) {41};
\node[snode, on chain] (4) {13};
\node[anode] [above of=1, yshift=4ex] (f) {};
\node[anode] [above of=4, yshift=4ex] (l) {};
\node [left of=f] (fl) {first};
\node [right of=l] (ll) {last};
\draw[*->] (1.two) -- (2);
\draw[*->] (2.two) -- (3);
\draw[*->] (3.two) -- (4);
\draw[*->] (f.mid) -- (1);
\draw[*->] (l.mid) -- (4);
\draw[fill=black] (4.two) circle (2.3pt);
\end{tikzpicture}

\end{document}

输出结果如下:

MWE 的输出

如何让所有实心圆位于矩形的中心?

答案1

您可以通过以下方式获取拆分节点的第二部分的中心

(<node>.two west-|<node>.two south)

如果您画箭头,箭头头将在起点或目标处结束,因此您可能需要将圆形箭头缩短相应的负量。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{chains}

\tikzset{
 anode/.style={rectangle, draw, minimum size = 12pt, inner sep = 4pt},
 snode/.style={rectangle split, rectangle split parts=2, draw, 
 rectangle split horizontal, minimum size=18pt, inner sep=4pt, text=black},
>=Stealth,
carr/.style={{Circle[width=4.6pt,length=4.6pt]}->,shorten <=-2.3pt}}

\begin{document}

\begin{tikzpicture}[start chain]
\node[snode, on chain] (1) {25};
\node[snode, on chain] (2) {19};
\node[snode, on chain] (3) {41};
\node[snode, on chain] (4) {13};
\node[anode] [above of=1, yshift=4ex] (f) {};
\node[anode] [above of=4, yshift=4ex] (l) {};
\node [left of=f] (fl) {first};
\node [right of=l] (ll) {last};
\foreach \X [count=\Y] in {2,3,4}
\draw[carr] (\Y.two west-|\Y.two south) -- (\X);
\draw[carr] (f.center) -- (1);
\draw[carr] (l.center) -- (4);
\draw[fill=black] (4.two west-|4.two south) circle[radius=2.3pt];
\end{tikzpicture}

\end{document}

在此处输入图片描述

您可以使用相同的锚点来添加一些圆形节点。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{chains}


\begin{document}

\begin{tikzpicture}[anode/.style={rectangle, draw, minimum size = 12pt, inner sep = 4pt},
 snode/.style={on chain,rectangle split, rectangle split parts=2, draw, 
 rectangle split horizontal, minimum size=18pt, inner sep=4pt, text=black},
>=Stealth,start chain=going right]
\node[snode] (1) {25};
\node[snode] (2) {19};
\node[snode] (3) {41};
\node[snode] (4) {13};
\node[anode] [above of=1, yshift=4ex] (f) {};
\node[anode] [above of=4, yshift=4ex] (l) {};
\node [left of=f] (fl) {first};
\node [right of=l] (ll) {last};
\path [nodes={circle,fill,inner sep=0pt,minimum size=4.6pt}] 
foreach \Y in {1,...,4}
{ (\Y.two west-|\Y.two south) node(c-\Y){}}
(f.center) node(c-0){} (l.center) node(c-5){};
\draw[->] foreach \X [count=\Y] in {0,...,3}
{(c-\X) edge (\Y) } (c-5) -- (4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

最后,你可以使用以下方法将点作为节点定义的一部分

 append after command={(\tikzlastnode.two west-|\tikzlastnode.two south)
 node[dot] (c-chain-\tikzchaincount) {}}

并通过 、 添加箭头joinjoin=by harr其中

 harr/.style={->,to path={(c-\tikztostart) -- (\tikztotarget)}

完整示例:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{chains}


\begin{document}

\begin{tikzpicture}[anode/.style={rectangle, draw, minimum size = 12pt, inner sep = 4pt},
 dot/.style={circle,fill,inner sep=0pt,minimum size=4.6pt},
 snode/.style={on chain,rectangle split, rectangle split parts=2, draw, 
 rectangle split horizontal, minimum size=18pt, inner sep=4pt, text=black,
 append after command={(\tikzlastnode.two west-|\tikzlastnode.two south)
 node[dot] (c-chain-\tikzchaincount) {}},join=by harr},
 harr/.style={->,to path={(c-\tikztostart) -- (\tikztotarget)}},
    >=Stealth,start chain=going right]
\node[snode] (1) {25};
\node[snode] (2) {19};
\node[snode] (3) {41};
\node[snode] (4) {13};
\node[anode] [above of=1, yshift=4ex,label=left:first,
    label={[anchor=center,dot,name=c-f]center:}] (f) {};
\node[anode] [above of=4, yshift=4ex,label=right:last,
    label={[anchor=center,dot,name=c-l]center:}] (l) {};
\foreach \X/\Y in {f/1,l/4} 
{\draw[->] (c-\X) -- (\Y);}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容