如何用 tikz 绘制下图?

如何用 tikz 绘制下图?

我想用 tikz 绘制下图。有乳胶代码吗,请分享一下。

在此处输入图片描述

答案1

实现此目的的一个非常简单的方法是

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[marrow/.style={double,double distance=#1,
    {Straight Barb[width=#1,length=-#1/3].}-{.Straight
    Barb[width=#1,length=#1/3]}},
    marrow/.default=20pt,shorten >=5pt,shorten <=5pt]
  \draw[marrow] (0,0) to[out=10,in=170] node {$a_1$} ++ (2,0) coordinate (aux);
  \draw[marrow] (aux) to[out=-10,in=170] node[pos=0.2] {$a_2$}   ++ (3,-2)  coordinate (aux);
  \draw[marrow] (aux) to[out=-10,in=-90] node {$a_2$}   ++ (2,1) coordinate (aux);
  \draw[marrow] (aux) to[out=90,in=10] node{$a_4$}  ++ (-2,1) coordinate (aux);
  \draw[marrow] (aux) to[out=-170,in=10] node[pos=0.8] {$a_5$}    ++ (-3,-2) coordinate (aux);
  \draw[marrow] (aux) to[out=-170,in=-170,looseness=3] node[pos=0.3] {$a_6$} (0,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

不幸的是,有一些文物。

因此,您可能想要使用装饰。这是这个答案

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations,decorations.text} 
\pgfkeys{/tikz/.cd,
    outlined arrow width/.store in=\OutlinedArrowWidth,
    outlined arrow width=15pt,
    outlined arrow step/.store in=\OutlinedArrowStep,
    outlined arrow step=1pt,
    outlined arrow length/.store in=\OutlinedArrowLength,
    outlined arrow length=10pt,
}

\pgfdeclaredecoration{outlined arrow}{initial}
{% initial arrow butt
\state{initial}[width=\OutlinedArrowStep,next state=cont] {
    \pgfmoveto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{\OutlinedArrowLength}{0pt}}
    \pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{-\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
    \pgfcoordinate{lastup}{\pgfpoint{1pt}{\OutlinedArrowWidth/2}}
    \pgfcoordinate{lastdown}{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
    \xdef\marmotarrowstart{0}
  }
  \state{cont}[width=\OutlinedArrowStep]{
    \ifdim\pgfdecoratedremainingdistance>\OutlinedArrowLength% continue the outlined path
     \pgfmoveto{\pgfpointanchor{lastup}{center}}
     \pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
     \pgfcoordinate{lastup}{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
     \pgfmoveto{\pgfpointanchor{lastdown}{center}}
     \pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
     \pgfcoordinate{lastdown}{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
    \else
     \ifnum\marmotarrowstart=0% draw the arrow head
     \pgfmoveto{\pgfpointadd{\pgfpointanchor{lastup}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \pgflineto{\pgfpoint{-0.5\pgflinewidth}{\OutlinedArrowWidth/2}}
     \pgflineto{\pgfpointadd{\pgfpointdecoratedpathlast}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \pgflineto{\pgfpoint{-0.5\pgflinewidth}{-\OutlinedArrowWidth/2}}
     \pgflineto{\pgfpointadd{\pgfpointanchor{lastdown}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \xdef\marmotarrowstart{1}
     \else
     \fi
    \fi%
  }
  \state{final}[width=5pt]
  { % perhaps unnecessary but doesn't hurt either
    \pgfmoveto{\pgfpointdecoratedpathlast}
  }
}
\begin{document}


\begin{tikzpicture}[decoration=outlined arrow,outlined arrow length=10pt,
 ctext/.style={decoration={text along path, text={#1},
  raise=-2.5pt,text align=center},decorate},
  pics/tarrow/.style={code={
  \draw[decorate] #1;
  \draw[ctext=\pgfkeysvalueof{/tikz/tarrow/text}] #1;}},
  tarrow/.cd,text/.initial={}]
  \path pic[tarrow/text={${a_1}$}] {tarrow={(-6,1.5) to[bend left=20] (150:2)}}
  pic[tarrow/text={${a_2}$}] {tarrow={(150:2) to[bend left=10] (-30:2)}}
  pic[tarrow/text={${a_3}$}] {tarrow={(-30:2) to[bend right=20] (6,-1.5)}}
  pic[tarrow/text={${a_4}$}] {tarrow={(6,-1.5) arc[start angle=-90,end
  angle=90,radius=1.5cm]}}
  ;
\end{tikzpicture}  
\end{document}

在此处输入图片描述

也许这可以为你提出一个更容易被接受的问题提供依据。

如果您只想要直立的文本,则不需要decorations.text

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations} 
\pgfkeys{/tikz/.cd,
    outlined arrow width/.store in=\OutlinedArrowWidth,
    outlined arrow width=15pt,
    outlined arrow step/.store in=\OutlinedArrowStep,
    outlined arrow step=1pt,
    outlined arrow length/.store in=\OutlinedArrowLength,
    outlined arrow length=10pt,
}

\pgfdeclaredecoration{outlined arrow}{initial}
{% initial arrow butt
\state{initial}[width=\OutlinedArrowStep,next state=cont] {
    \pgfmoveto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{\OutlinedArrowLength}{0pt}}
    \pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{-\OutlinedArrowWidth/2}}
    \pgfpathlineto{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
    \pgfcoordinate{lastup}{\pgfpoint{1pt}{\OutlinedArrowWidth/2}}
    \pgfcoordinate{lastdown}{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
    \xdef\marmotarrowstart{0}
  }
  \state{cont}[width=\OutlinedArrowStep]{
    \ifdim\pgfdecoratedremainingdistance>\OutlinedArrowLength% continue the outlined path
     \pgfmoveto{\pgfpointanchor{lastup}{center}}
     \pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
     \pgfcoordinate{lastup}{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
     \pgfmoveto{\pgfpointanchor{lastdown}{center}}
     \pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
     \pgfcoordinate{lastdown}{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
    \else
     \ifnum\marmotarrowstart=0% draw the arrow head
     \pgfmoveto{\pgfpointadd{\pgfpointanchor{lastup}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \pgflineto{\pgfpoint{-0.5\pgflinewidth}{\OutlinedArrowWidth/2}}
     \pgflineto{\pgfpointadd{\pgfpointdecoratedpathlast}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \pgflineto{\pgfpoint{-0.5\pgflinewidth}{-\OutlinedArrowWidth/2}}
     \pgflineto{\pgfpointadd{\pgfpointanchor{lastdown}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
     \xdef\marmotarrowstart{1}
     \else
     \fi
    \fi%
  }
  \state{final}[width=5pt]
  { % perhaps unnecessary but doesn't hurt either
    \pgfmoveto{\pgfpointdecoratedpathlast}
  }
}
\begin{document}


\begin{tikzpicture}[decoration=outlined arrow,outlined arrow length=10pt,
    phantom/.style={circle,inner sep=1ex,alias=aux,node contents={}}]
%
  \draw[decorate] (0,0) 
    to[out=10,in=170] node {$a_1$} ++ (2,0) node[phantom];
  \draw[decorate] (aux) to[out=-10,in=170] node[pos=0.2] {$a_2$}   ++ (3,-2)  node[phantom];
  \draw[decorate] (aux) to[out=-10,in=-90] node {$a_2$}   ++ (2,1) node[phantom];
  \draw[decorate] (aux) to[out=90,in=10] node{$a_4$}  ++ (-2,1) node[phantom];
  \draw[decorate] (aux) to[out=-170,in=10] node[pos=0.8] {$a_5$}    ++ (-3,-2) node[phantom];
  \draw[decorate] (aux) to[out=-170,in=-170,looseness=3] node[pos=0.3] {$a_6$} (0,0);
%
  \path[font=\sffamily] (1,-1) node{from}  (5.5,-1) node{to} ; 
\end{tikzpicture}  
\end{document}

在此处输入图片描述

相关内容