在 LaTeX 中创建图表,可能还使用 TikZ?

在 LaTeX 中创建图表,可能还使用 TikZ?

我对绘制一些带箭头的流程图很感兴趣,如图所示,以表示递归流程。我希望有人能给我指出正确的方向,告诉我如何创建这样的图表。到目前为止,我对 circuitikz 很熟悉,所以也许可以使用 tikz 的方法? 在此处输入图片描述

答案1

以牺牲清晰度和/或普遍性(如果这实际上是一个词的话)为代价的简洁性:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth,every node/.style={text depth=0cm},
  every label/.style={label distance=0.75cm, text=red, inner sep=1cm/16}]
\foreach \i [count=\j, count=\k from 0] in {5,7,9,11}{
  \node [label={[name=p-\j]180:$+$}, label={[name=q-\j]0:$\i$}] 
    (n-\j) at (\j*1.5,-\j/1.5) {Stuff (\i)};
  \draw [<-] (n-\j.base west) -| ++(-.5,.5);
  \ifnum\j>1
    \draw [blue, ->] (p-\k) to [out=250, in=200] (p-\j);
    \draw [blue, ->] (q-\j) to [out=110, in=20]  (q-\k);
  \fi
} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我会选择matrix

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
table/.style={
  matrix of nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,text width=4.5em,align=center,inner sep=0pt},
  text depth=1.25ex,
  text height=2.5ex,
  nodes in empty cells
},
}

\begin{document}

\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
&  &  &  &  &  \\
|[text=red]|$+$ & Stuff (5)  & |[text=red]|5 & & &  \\
   &  |[text=red]|$+$  & Stuff (7) & |[text=red]|7 & &  \\
   &    & |[text=red]|$+$ & Stuff (9) & |[text=red]|9 &  \\
   &    &  &|[text=red]|$+$ & Stuff (11) & |[text=red]| 11   \\
};
% the matrix rules

% the arrows
\begin{scope}
\draw[->]  ([xshift=8pt]mat-1-1.south) |- ([yshift=4pt]mat-2-2.south west);
\draw[->]  ([xshift=8pt]mat-2-2.south) |- ([yshift=4pt]mat-3-3.south west);
\draw[->]  ([xshift=8pt]mat-3-3.south) |- ([yshift=4pt]mat-4-4.south west);
\draw[->]  ([xshift=8pt]mat-4-4.south) |- ([yshift=4pt]mat-5-5.south west);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-2-1.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-3-2.center);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-3-2.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-4-3.center);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-4-3.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-5-4.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-5-6.center) to[out=90, in=65]  (mat-4-5.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-4-5.center) to[out=90, in=65]  (mat-3-4.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-3-4.center) to[out=90, in=65]  (mat-2-3.center);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

这个需要macro5 个参数,特别是 L 形可以有不同的垂直和水平长度,由 #2(向下为负)和 #3 控制。文本由stuff具有固定长度的样式定义。

#1=starting point, #2=vertically down  length,  #3=length of L, #4=end point, #5=stuff label.

在此处输入图片描述

代码

\documentclass[border=10pt]{standalone}%[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{stuff/.style={rectangle, text width=2cm, inner sep=0pt, outer sep=0pt}
}
\newcommand\myL[5]{
% draw L shape line
\draw[line width=1pt,->] ($(#1)+(1cm,0)$) node[below left=0.3cm and 0.2cm](a){\color{red}$+$} |- +(0,#2)
node[](b){} -- +(#3,#2)node[](#4){};
\node[stuff,anchor=south west] at (#4){#5};
% curve arrow on the left
\draw[blue,->] (a) to[out=-90,in=-180, looseness=2] ([shift={(0,-0.5)}]#4);
% curve line on the right, 3cm and 2cm can be changed
\draw[green,->] ($(#4)+(2cm,0.5cm)$) to[out=90,in=0] ($(b.east)+(1cm,-#2)$);
}
\begin{document}

\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt] at (0,0) (A){};
\myL{A}{-1}{1}{B}{Long Stuff(5)\hfill \color{red}5}
\draw[line width=5pt,white,->] ($(B)+(2cm,0.5cm)$)to[out=90,in=0] ($(b.east)+(1cm,1cm)$);   % to erase the unneeded curve arrow that was drawn automatically.
\myL{B}{-1}{1}{C}{Stuff(7)\hfill \color{red}  7}
\myL{C}{-1}{1}{D}{Stuff(9)\hfill \color{red}  9}
\myL{D}{-1}{1}{E}{Stuff(11)\hfill \color{red} 11}
\draw[line width=5pt,white,->, looseness=2] (a) to[out=-90,in=-180] ([shift={(0,-0.5)}]E); % to erase the unneeded curve arrow that was dran automatically.
\end{tikzpicture}
\end{document}

相关内容