每个节点顶部都有弯曲的箭头

每个节点顶部都有弯曲的箭头

我是 latex 和 tikz 的新手。我想在每个节点顶部添加弯曲的箭头,就像tikx 示例

以下是我的代码。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
  [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]


  \node (n1) at (9,8) {1};
  \node (n2) at (6,7) {2};
  \node (n3) at (5,5)  {3};
  \node (n4) at (6,3) {4};
  \node (n5) at (9,2)  {5};
  \node (n6) at (12,3)  {6};
  \node (n7) at (13,5)  {7};
  \node (n8) at (12,7)  {8};


  \foreach \from/\to in {n1/n2,n2/n3,n3/n4,n4/n5,n5/n6,n6/n7,n7/n8,n8/n1,n1/n3,n1/n4,n1/n5,n1/n6,n1/n7,n2/n4,n2/n5,n2/n6,n2/n7,n2/n8,n3/n5,n3/n6,n3/n7,n3/n8,n4/n6,n4/n7,n4/n8,n5/n7,n5/n8,n6/n8}
    \draw (\from) -- (\to);

\end{tikzpicture}
\end{figure}

\end{document}

如何在每个节点上方绘制弯曲的箭头?

答案1

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
  [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]


  \node (n1) at (9,8) {1} edge [in=100,out=80,loop] ();
  \node (n2) at (6,7) {2} edge [in=150,out=130,loop] ();
  \node (n3) at (5,5)  {3} edge [in=190,out=170,loop] ();
  \node (n4) at (6,3) {4} edge [in=240,out=220,loop] ();
  \node (n5) at (9,2)  {5} edge [in=280,out=260,loop] ();
  \node (n6) at (12,3)  {6} edge [in=330,out=310,loop] ();
  \node (n7) at (13,5)  {7} edge [in=10,out=350,loop] ();
  \node (n8) at (12,7)  {8} edge [in=60,out=40,loop] ();


  \foreach \from/\to in {n1/n2,n2/n3,n3/n4,n4/n5,n5/n6,n6/n7,n7/n8,n8/n1,n1/n3,n1/n4,n1/n5,n1/n6,n1/n7,n2/n4,n2/n5,n2/n6,n2/n7,n2/n8,n3/n5,n3/n6,n3/n7,n3/n8,n4/n6,n4/n7,n4/n8,n5/n7,n5/n8,n6/n8}
    \draw (\from) -- (\to);

\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案2

如果您对圆形布局感到满意,那么使用n>4节点的 foreach 循环可以简化很多事情。然后您需要调整循环绘制,这也是可行的,但我有点无聊 :P

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}\def\myn{9}
\foreach \x in {1,...,\myn}{\node[circle,fill=blue!20,inner sep=1pt] (n\x) at ({(\x-1)*360/\myn+90}:2cm) {\x};}
\foreach \x[evaluate={\xi=(\x-1)*360/\myn+90+360/\myn;\xo=\xi-360/(0.5*\myn)}] in {1,...,\myn}{
\foreach \y in {\x,...,\myn}{\ifnum\x=\y\path (n\x)edge [in=\xi,out=\xo,loop] ();\else\draw[-](n\x) -- (n\y);\fi}}
\end{tikzpicture}
\end{document}

为了n=9

在此处输入图片描述

对于n=40......(我真的不知道为什么)

在此处输入图片描述

答案3

也许是这样的?

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}
  [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]


  \node (n1) at (9,8) {1};
  \node (n2) at (6,7) {2};
  \node (n3) at (5,5)  {3};
  \node (n4) at (6,3) {4};
  \node (n5) at (9,2)  {5};
  \node (n6) at (12,3)  {6};
  \node (n7) at (13,5)  {7};
  \node (n8) at (12,7)  {8};


  \foreach \from/\to in {n1/n2,n2/n3,n3/n4,n4/n5,n5/n6,n6/n7,n7/n8,n8/n1,n1/n3,n1/n4,n1/n5,n1/n6,n1/n7,n2/n4,n2/n5,n2/n6,n2/n7,n2/n8,n3/n5,n3/n6,n3/n7,n3/n8,n4/n6,n4/n7,n4/n8,n5/n7,n5/n8,n6/n8}
    \draw (\from) -- (\to);

  \foreach \i in {1,...,8}
      \draw [->] (n\i.north west) [out=45, in=135] to (n\i.north east);

\end{tikzpicture}

\end{document}

带弯曲箭头的网络

相关内容