我正在尝试在链接列表中创建一个节点并让它指向自身。
\begin{tikzpicture}[list/.style={rectangle split, rectangle split parts=2,
draw, rectangle split horizontal}, >=stealth, start chain]
\node[list,on chain] (A) {1};
\coordinate[above = 0.3 of A] (aa);
\draw[*-] let \p1 = (A.two), \p2 = (A.center) in (\x1,\y2) -- (A.east);
\draw [-] (A.east) to[bend right=90] node[auto] {} (aa);
\draw [->] (aa) to[bend right=90] node[auto] {} (A.west);
\end{tikzpicture}
结果如下图所示。
但我希望它能像下面这样更漂亮。单箭头就能直击目标。
答案1
通过路径,您可以直接用和curve to
传递绝对角度。in
out
\draw (A) to[out=0, in=0] ([shift=(up:.3)] A.north) to[out=180, in=180] (A);
虽然我会使用不同的路径,并*
在其中添加箭头。在to path
样式中使用它可以轻松重复使用并用作边缘。
需要用到材料\pgfextra
来将边缘上的节点运送到节点上方的水平部分。
代码
\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{arrows,shapes.multipart}
\makeatletter
\tikzset{
dotloop above/.style={
*->,
looseness=#1,
to path={
\pgfextra{%
\let\myTikZtostart\tikztostart
\let\myTikZ@tonodes\tikz@tonodes
}
(\myTikZtostart.two |- \myTikZtostart.east) -- (\myTikZtostart.east)
to[out=0,in=0] ([shift=(up:+.3cm)] \myTikZtostart.north east) -- ([shift=(up:+.3cm)] \myTikZtostart.north west)
{\pgfextra{\tikz@node@is@a@labeltrue}\myTikZ@tonodes} to[out=180, in=180] (\myTikZtostart.west)
}
},
dotloop above/.default=1.5,
}
\makeatother
\begin{document}
\begin{tikzpicture}[
list/.style={
rectangle split,
rectangle split parts=2,
draw,
rectangle split horizontal},
>=stealth,
auto=right]
\node[list] (A) {1};
\path (A) edge[dotloop above] node {node} (A);
\end{tikzpicture}
\end{document}
输出
答案2
两个选项:第一个使用语法controls
,另一个使用修饰ellipse
(我抑制了它,chain
因为它对于示例来说不是必需的):
\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,arrows,decorations.markings}
\begin{document}
\begin{tikzpicture}[
list/.style={
rectangle split,
rectangle split parts=2,
draw,
rectangle split horizontal},
>=stealth
]
\node[list] (A) {1};
\draw[*-] (A.two|-A.center) -- (A.east);
\draw [->] (A.east) -- +(5pt,0) .. controls +(70:1) and +(100:1) ..
([xshift=-5pt]A.west) -- (A.west);
\end{tikzpicture}
\vspace{1cm}
\begin{tikzpicture}[
list/.style={
rectangle split,
rectangle split parts=2,
draw,
rectangle split horizontal},
>=stealth
]
\draw[decoration={
markings,
mark=at position 0.62 with {\arrow{>}}},postaction=decorate]
([yshift=8pt]A.center) ellipse [x radius=19pt,y radius=10pt];
\node[list,fill=white] (A) {1};
\draw[*-] (A.two|-A.center) -- (A.east);
\end{tikzpicture}
\end{document}