以下 MWE 生成了我想要改进的流程图:
- 向左离开的箭头应该向上弯曲,大约 5 毫米。
- 向左离开的箭头应该正好位于箭头距离的一半距离处
(1)--(2)
- 离开右侧决策点的箭头应该做同样的事情,但是
(6)
从北方“曲折”地进入(意思是:向下后,箭头应该向左弯曲,然后向下/或从东边进入框。 - 是否可以根据“对象尺寸”设置节点距离?我希望每个对象之间的距离恒定,无论它们有多“长”。
先感谢您。
\documentclass{scrbook}
\usepackage[fleqn]{amsmath}
\usepackage{tikz,tikz-3dplot}
\usetikzlibrary{
intersections,
arrows,
patterns,
shapes,
positioning,
calc,
}
\begin{document}
\tikzstyle{block1} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw, fill=white!20, text width=6cm]
\tikzstyle{block2} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, draw, fill=white!20, text width=6cm]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw, fill=white!20, text width=2cm]
\tikzstyle{line} = [draw, ->,>=stealth]
\begin{tikzpicture}[node distance = 3cm, auto]
% Place nodes
\node [block1] (1) {%
\textbf{Initialisation}\\%
$\vec{P} \left[ 1 \right]$
};
\node [block1, below of=1,] (2) {%
\textbf{Fitness evaluation} \\ %
$\vec{x}^{i} = \text{d}(\Theta^{i})$,\\ %
$f^{i}=f{\vec{x}^{i}}$};
\node [decision, below of=2,] (3) {%
\textbf{Stop crit. met?}};
\node [block1, below of=3,] (4) {%
\textbf{Selection}};
\node [block2, below of=4,] (5) {%
\centerline{\textbf{Mating}} \\
\textbullet~Chromosome crossover \\
\textbullet~Segregation\\
\textbullet~Mutation};
\node [block1, below of=5,] (6) {%
\textbf{Select solution}\\
$i = \text{argmax}(f^{i})$,\\
$\vec{x}^{\ast} = \vec{x}^{i}$};
% % Draw edges
\path [line] (1) -- node [] {$\vec{P}[k]$}(2);
\path [line] (2) -- (3);
\path [line] (3) -- node [] {No}(4);
\path [line] (3.east) |- node [above] {Yes}(6);
\path [line] (4) -- node [] {}(5);
\path [line] (5.west) |- node [] {$\vec{P}[k+1]=\vec{C}[k]$}(0,-1);
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{shapes, calc, arrows.meta}
\begin{document}
\tikzset{
block1/.style ={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw, fill=white!20, text width=6cm},
block2/.style ={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, draw, fill=white!20, text width=6cm},
decision/.style ={diamond, minimum width=3cm, minimum height=1cm, text centered, draw, fill=white!20, text width=2cm},
line/.style ={-Stealth},
}
\begin{tikzpicture}[node distance=3cm, auto]
\node[block1] (1) {\textbf{Initialisation}\\$\vec{P} \left[ 1 \right]$};
\node[block1, below of=1] (2) {\textbf{Fitness evaluation}\\$\vec{x}^{i}=\text{d}(\Theta^{i})$,\\$f^{i}=f{\vec{x}^{i}}$};
\node[decision, below of=2] (3) {\textbf{Stop crit. met?}};
\node[block1, below of=3] (4) {\textbf{Selection}};
\node[block2, below of=4] (5) {\centerline{\textbf{Mating}}\\\textbullet~Chromosome crossover\\\textbullet~Segregation\\\textbullet~Mutation};
\node[block1, below of=5] (6) {\textbf{Select solution}\\$i=\text{argmax}(f^{i})$,\\$\vec{x}^{\ast}=\vec{x}^{i}$};
\draw[line] (1) --node{$\vec{P}[k]$} (2);
\draw[line] (2) -- (3);
\draw[line] (3) --node{No} (4);
\draw[line] (3.east) --node[above]{Yes} +(2.5,0) |- ($ (5.south)!0.5!(6.north) $) --(6.north);
\draw[line] (4) -- (5);
\draw[line] (5.west) -- +(-0.5,0) |-node[pos=0.75]{$\vec{P}[k+1]=\vec{C}[k]$} ($ (1.south)!0.5!(2.north) $);
\end{tikzpicture}
\end{document}
答案2
为了更好地理解这一点,请在非常专业的 Hpekristiansen 的答案中添加 (prrovvisory)。在后面添加两行代码\begin{tikzpicture}
:
\draw[gray!15] (-4,-16) grid (4,2);
\foreach \x/\t in {0/(1),-3/(2),-6/(3),-9/(4),-12/(5),-15/(6)} \node at (-4.2,\x) {\small $\t$};
您有此输出,可以很好地显示所有图片块:
block1
其中我还为、block2
和的样式定义添加了一些装饰颜色decision
。在完全理解 Hpekristiansen 的所有代码后,您可以删除或注释我的两行。
答案3
chains
我将使用包及其宏绘制流程图join
:
\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\argmax}{{arg\,max}}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
calc, chains,
quotes,
positioning,
shapes.geometric}
\begin{document}
\begin{tikzpicture}[
node distance = 8mm,
start chain = A going below,
%
arr/.style = {-Stealth},
base/.style = {draw,
text width=4cm, minimum height=1cm, align=center,
font=\bfseries},
box/.style = {base, rounded corners},
decision/.style = {diamond, base, aspect=1.5, inner sep=-5pt},
every edge quotes/.style = {auto=left, font=\small},
every join/.style = {arr}
]
\begin{scope}[nodes={on chain=A}]
\node[box] {Initialisation\\
$\vec{P}[1]$};
\coordinate (aux);
\node[box, join] {Fitness evaluation\\[1ex]
$\begin{aligned}
\vec{x}^{i} & = \text{d}(\Theta^{i}),\\
f^{i} & = f{\vec{x}^{i}}
\end{aligned}$};
\node[decision, join] {Stop\\ crit. met?};
\node[box, join] {Selection};
\node[box, join] {Mating\normalfont
\begin{itemize}[nosep, leftmargin=*]
\item Chromosome crossover
\item Segregation
\item Mutation
\end{itemize}};
\node[box,
] {Select solution\\[1ex]
$\begin{aligned}
i & = \argmax(f^{i})\\
\vec{x}^{\ast} & = \vec{x}^{i}
\end{aligned}$};
\end{scope}
\draw[arr] (A-1) to ["{$\vec{P}[k]$}"] (A-3) to ["No"] (A-4);
\draw[arr] (A-4.east) to ["Yes"] ++ (8mm,0) |- ($(A-6.south)!0.5!(A-7.north) $) -- (A-7.north);
\draw[arr] (A-6.west) -- ++(-8mm,0) |- (aux) node[near end,above] {$\vec{P}[k+1]=\vec{C}[k]\quad$};
\end{tikzpicture}
\end{document}
顺便说一句,流程图可以用不同的方式绘制。例如,在这问题。