如何在 tikz 中绘制弯曲的箭头

如何在 tikz 中绘制弯曲的箭头

我需要使用 tikz 绘制这幅图。

在此处输入图片描述

我很少使用 tikz,因此我发现完成这项任务很困难。我尝试使用来自互联网的一些代码,但我找不到如何绘制弯曲的箭头。

\begin{tikzpicture}
\foreach \i in {0,...,4}
{
    \draw (-1+5*\i,0) -- (0+5*\i,1) -- (1+5*\i,0) -- (0+5*\i,-1) --cycle;
\node  at (5*\i,0) {\huge$\i$};
}
\end{tikzpicture}

答案1

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,4} \node[draw,diamond] (\i) at (2*\i,0) {$\i$};
\foreach \i [count=\j] in {0,...,3} {
    \draw[->] (\i) to[bend left] (\j);
    \draw[->] (\i) to[bend right] (\j);
}
\path[->] (1) to[bend left] node[midway,above,inner sep=2pt] {1/4} (2);
\path[->] (0) to[bend right] node[midway,below,inner sep=2pt] {1/4} (1);
\draw[->] (0) to[out=-150,in=150,loop] node[midway,left,inner sep=2pt] {1} ();
\draw[->] (1) to[out=-60,in=-120,loop] node[midway,below,inner sep=2pt] {1/2} ();
\draw[->] (2) to[out=-60,in=-120,loop] ();
\draw[->] (3) to[out=-60,in=-120,loop] ();
\draw[->] (4) to[out=30,in=-30,loop] node[midway,right,inner sep=2pt] {1} ();
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用 的版本tikz-cd

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\begin{document}
\[\tikzset{loop below/.style={out=-60,in=-120,loop},
loop left/.style={out=210,in=150,loop},
loop right/.style={out=30,in=-30,loop},
loop above/.style={out=60,in=120,loop}}
\begin{tikzcd}[cells={nodes={shape=diamond,draw}}]
 0 \ar[r,bend left] \ar[loop left,"1" ] & 
 1 \ar[r,bend left,"1/4"] \ar[l,bend left,"1/4"] \ar[loop below,"1/2"] & 
 2 \ar[r,bend left] \ar[l,bend left] \ar[loop below] &
 3 \ar[r,bend left] \ar[l,bend left] \ar[loop below] & 
 4 \ar[l,bend left] \ar[loop right]
\end{tikzcd}\]
\end{document}

在此处输入图片描述

这与焦耳 V 的答案类似,但所有内容都在一个循环中,并用 放置标签auto

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\edef\lstLoop{"1","1/2","","","1"}
\edef\lstAbove{"","","1/4","",""}
\edef\lstBelow{"","1/4","","",""}
\foreach \X [remember=\X as \LastX] in {0,...,4}
 {\node[diamond,draw] (d\X) at (2*\X,0) {\X};
 \ifnum\X=0
  \draw[-latex] (d\X) to[out=150,in=210,loop] 
    node[auto,swap]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X) ;
 \else
  \draw[-latex] (d\LastX) to[bend left] 
        node[auto]{\pgfmathparse{{\lstAbove}[\X]}$\pgfmathresult$}  (d\X);
  \draw[-latex] (d\X) to[bend left] 
        node[auto]{\pgfmathparse{{\lstBelow}[\X]}$\pgfmathresult$} 
        (d\LastX);
  \ifnum\X<4
   \draw[-latex] (d\X) to[out=-60,in=-120,loop] 
    node[auto]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X);
  \else
   \draw[-latex] (d\X) to[out=30,in=-30,loop] 
   node[auto]{\pgfmathparse{{\lstLoop}[\X]}$\pgfmathresult$} (d\X) ;
  \fi
 \fi
 }
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容