如何使用节点移动文本?

如何使用节点移动文本?

我有一个由箭头连接的节点链。我使用角度将箭头精确地定位到我想要的位置。但是,链两端的箭头指向包含一些文本的节点。我似乎无法让这些节点的文本与箭头对齐。参见下图:

在此处输入图片描述

文本“NULL”应与末尾的箭头对齐。我该如何实现这一点?我尝试使用yshift节点定义的位置,但被忽略了。这是一个 MWE:

\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows.meta,bending,calc,shapes.multipart,chains,arrows,decorations.markings}

\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \node [on chain] (X) {NULL};
    \begin{scope}[nodes={list}]
      \node   (A) {\nodepart{second}$A_1$};
      \node   (B) {\nodepart{second}$A_2$};
      \node   (C) {\nodepart{second}$A_3$};
      \node   (D) {\nodepart{second}$A_4$};
      \node   (E) {\nodepart{second}$A_5$};
    \end{scope}
    \node [on chain] (Y) {NULL};
    %
    \draw   
    (A.170) edge (X.12)
    (A.10) edge (B.170)
    (B.195) edge (A.-15)
    (B.10) edge (C.170)
    (C.195) edge (B.-15)
    (C.10) edge (D.170)
    (D.195) edge (C.-15)
    (D.10) edge (E.170)
    (E.195) edge (D.-15)
    (E.10) edge (Y.168);
  \end{tikzpicture}
  \end{document}

答案1

您不需要XY节点on chain

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,chains,shapes.multipart}
\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \begin{scope}[nodes={list}]
      \node   (A) {\nodepart{second}$A_1$};
      \node   (B) {\nodepart{second}$A_2$};
      \node   (C) {\nodepart{second}$A_3$};
      \node   (D) {\nodepart{second}$A_4$};
      \node   (E) {\nodepart{second}$A_5$};
    \end{scope}
    %
    \draw   
    (A.170) ++ (-1,0) coordinate[label=left:NULL] (X)
    (A.170) edge (X)
    (A.10) edge (B.170)
    (B.195) edge (A.-15)
    (B.10) edge (C.170)
    (C.195) edge (B.-15)
    (C.10) edge (D.170)
    (D.195) edge (C.-15)
    (D.10) edge (E.170)
    (E.195) edge (D.-15)
    (E.10)  ++ (1,0) coordinate[label=right:NULL] (Y) 
    (E.10) edge (Y)
    ;
  \end{tikzpicture}
\end{document}

在此处输入图片描述

顺便说一句,你可以缩短代码。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,chains,shapes.multipart}
\begin{document}
 \begin{tikzpicture}[
      every edge/.style = {draw, -{Stealth[angle=32:10pt, bend]}},
      start chain = going right,
      list/.style = {rectangle split, rectangle split parts=3,
      rectangle split horizontal,
      draw, thick, minimum height=1cm,
      on chain},
    ]
    \begin{scope}[nodes={list}]
     \path foreach \X [count=\Y,remember=\X as \LastX] in {A,...,E}
      {node   (\X) {\nodepart{second}$A_{\Y}$}
      \ifnum\Y>1
       ([yshift=1.4mm]\LastX.east) edge ([yshift=1.4mm]\X.west)
       ([yshift=-1.4mm]\X.west) edge ([yshift=-1.4mm]\LastX.east)
      \fi};
    \end{scope}
    %
    \path ([yshift=1.4mm]A.west) ++ (-1,0) coordinate[label=left:NULL] (X) 
      ([yshift=1.4mm]A.west) edge (X)
      ([yshift=-1.4mm]E.east)  ++ (1,0) coordinate[label=right:NULL] (Y)  
      ([yshift=-1.4mm]E.east) edge (Y);
  \end{tikzpicture}
\end{document}

相关内容