数字序列中的曲线

数字序列中的曲线

我想用曲线写出数字序列,就像这张图片上那样。该怎么做?

在此处输入图片描述

答案1

这是一种方法。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[font=\sffamily,arr/.style={-{Latex[bend]},shorten <=2pt}]
 \def\Lst{5,6,7,8,10,11,14,15,19,20}
 \path[node distance=1ex] 
  foreach \X [count=\Y,remember=\X as \LastX] in \Lst
  {
  \ifnum\Y>1
   node[base right=of \LastX] (\X) {\ifnum\X>15 \dots\else\X\fi}
  \else
   node(\X) {\X} 
  \fi};
 \foreach \X [count=\Y] in \Lst
 {\ifnum\Y<3
  \else
   \pgfmathsetmacro{\myX}{{\Lst}[\Y-3]}
   \ifodd\Y
    \draw[arr] (\myX.south) to [bend right=70] 
    node[below]{\the\numexpr\X-\myX} (\X);
   \else
    \draw[arr] (\myX.north) to [bend left=70] 
    node[above]{\the\numexpr\X-\myX} (\X);
   \fi 
 \fi}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者仅一个循环。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning,quotes,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[font=\sffamily,arr/.style={thick,-{Latex[bend]},shorten <=2pt}]
 \def\Lst{5,6,7,8,10,11,14,15,19,20}
 \path[node distance=1ex] 
  foreach \X [count=\Y,remember=\X as \LastX] in \Lst
  {
  \ifnum\Y>1
   node[base right=of \LastX] (\X) {\ifnum\X>15 \dots\else\X\fi}
  \else
   node(\X) {\X} 
  \fi
  \ifnum\Y>2
   [/utils/exec=\pgfmathsetmacro{\myX}{{\Lst}[\Y-3]}]
   \ifodd\Y
     (\myX.south) edge[arr,bend right=70,"\the\numexpr\X-\myX"']  (\X)
   \else
    (\myX.north) edge[arr,bend left=70,"\the\numexpr\X-\myX"]  (\X)
   \fi 
 \fi
  };
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这里添加另一种方法:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                bending,
                chains,
                positioning,
                quotes}
\begin{document}
    \begin{tikzpicture}[auto,
node distance = 1em,
  start chain = going right,
   arr/.style = {-{Stealth[bend]}, shorten <=1pt, shorten >=1pt},
   bend angle = 60
                        ]
\foreach \i [count=\j] in {5,6,7,8,10,11,14,15,\dots,\dots}
    \node (n\j) [on chain] {\i};
\foreach \i [count=\j from 2,
             evaluate=\i as \k using int(\i+2)] in {2,4,6,8}
    \draw[arr] (n\i.north) to [bend left, "\j"] (n\k.north);
\foreach \i [count=\j from 2,
             evaluate=\i as \k using int(\i+2)] in {1,3,5,7}
    \draw[arr] (n\i.south) to [bend right, "\j" '] (n\k.south);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容