TikZ:流程图错误

TikZ:流程图错误

我修改了给出的流程图代码这里稍微处理一下,得到如下结果:

在此处输入图片描述

这是修改后的代码:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}

% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=yellow!20, 
    text width=6em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!20, 
    text width=12em, text centered, rounded corners, minimum height=4em]
\tikzstyle{small_block} = [circle, draw, fill=white!20, 
    text width=0em, text centered, rounded corners, minimum height=0em]    
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [block] (init) {\textbf{START} \\ Randomly generate $n$ groups of $p$ strings.};
    \node [block, below of=init] (identify) {Choose the leader string for each group.};
    \node [block, below of=identify] (mutate) {Mutate each string according to the given rule.};
    \node [block, below of=mutate] (transfer) {Carry out parameter transfer for each group, from other groups.};
    \node [block, below of=transfer] (evaluate) {Calculate the fidelity for all the strings in all the groups.};
    \node [small_block, left of=evaluate, node distance=5cm] (update) {};
    \node [decision, below of=evaluate] (decide) {Termination condition satisfied ?};
    \node [block, below of=decide, node distance=3cm] (stop) {\textbf{STOP}};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (mutate);
    \path [line] (mutate) -- (transfer);
    \path [line] (transfer) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {No} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {Yes}(stop);
\end{tikzpicture}
\end{document}

问题 1:

现在,我想删除位于“否”箭头路径上的小白圆圈。但是,要做到这一点,如果我删除\node [small_block, left of=evaluate, node distance=5cm] (update) {};,就会出现错误:即“否”箭头直接穿过其他块,看起来很丑陋。解决这个问题的正确方法是什么?

问题2:

如果我进一步修改代码,在流程图顶部绘制一个单独的“START”云,而不是将其与“随机生成......”块分组,例如:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}

% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=yellow!20, 
    text width=6em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!20, 
    text width=12em, text centered, rounded corners, minimum height=4em]
\tikzstyle{small_block} = [circle, draw, fill=white!20, 
    text width=0em, text centered, rounded corners, minimum height=0em]    
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [cloud] (start) {\textbf{START}};
    \node [block] (init) {Randomly generate $n$ groups of $p$ strings.};
    \node [block, below of=init] (identify) {Choose the leader string for each group.};
    \node [block, below of=identify] (mutate) {Mutate each string according to the given rule.};
    \node [block, below of=mutate] (transfer) {Carry out parameter transfer for each group, from other groups.};
    \node [block, below of=transfer] (evaluate) {Calculate the fidelity for all the strings in all the groups.};
    \node [small_block, left of=evaluate, node distance=5cm] (update) {};
    \node [decision, below of=evaluate] (decide) {Termination condition satisfied ?};
    \node [block, below of=decide, node distance=3cm] (stop) {\textbf{STOP}};
    % Draw edges
    \path [line] (start) -- (init);
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (mutate);
    \path [line] (mutate) -- (transfer);
    \path [line] (transfer) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {No} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {Yes}(stop);
\end{tikzpicture}
\end{document}

我得到:

在此处输入图片描述

显然,“开始“云没有出现!我哪里做错了?”

答案1

一些评论:

  1. \tikzstyle已被弃用,请改用\tikset{.../.style={...}}
  2. 使用定位库可以使事情简化很多。
  3. update如果您删除节点,但稍后在路径中使用它,则会出现错误,因为节点未定义。您只需插入一个坐标即可。
  4. 如果您使用,添加云就没有问题positioning

以下是代码:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}
\pagestyle{empty}

% Define block styles
\tikzset{decision/.style={diamond, draw, fill=yellow!20, 
    text width=6em, text badly centered, node distance=3cm, inner sep=0pt},
block/.style={rectangle, draw, fill=gray!20, 
    text width=12em, text centered, rounded corners, minimum height=4em},
small_block/.style={circle, draw, fill=white!20, 
    text width=0em, text centered, rounded corners, minimum height=0em},    
line/.style={draw, -latex'},
cloud/.style={draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em},}

\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [block] (init) {Randomly generate $n$ groups of $p$ strings.};
    \node [cloud,font=\bfseries,above=1cm of init] (start) {START};
    \node [block, below=of init] (identify) {Choose the leader string for each group.};
    \node [block, below=of identify] (mutate) {Mutate each string according to the given rule.};
    \node [block, below=of mutate] (transfer) {Carry out parameter transfer for each group, from other groups.};
    \node [block, below=of transfer] (evaluate) {Calculate the fidelity for all the strings in all the groups.};
    % changed to coordinate
    \coordinate [left of=evaluate, node distance=5cm] (update) {};
    \node [decision, below of=evaluate] (decide) {Termination condition satisfied ?};
    \node [block, below of=decide, node distance=3cm] (stop) {\textbf{STOP}};
    % Draw edges
    \path [line] (start) -- (init); 
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (mutate);
    \path [line] (mutate) -- (transfer);
    \path [line] (transfer) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    %\path [line] (decide) -| node [near start] {No} (update);
    %\path [line] (update) |- (identify);
    \path [line] (decide) -| node [near start] {No} (update) |- (identify);
    \path [line] (decide) -- node {Yes}(stop);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

tikz借助库和chains,您可以轻松绘制简单的流程图。考虑到它们,流程图的代码组织如下:positioningquotes

  • 节点以链的形式连接,开头为“START”,结尾为“END”,
  • join链中节点之间的线由库提供的宏绘制chains
  • 使用第一条路径的阶段的相对坐标绘制反馈回路,
  • 用于标记路径的quotes

最终的代码更加简短和简单:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,       % <---
                positioning,  % <---
                quotes,       % <---
                shapes.geometric}
\usepackage{siunitx}

\tikzset{FlowChart/.style={   % <--- named tikzset
    block/.style = {rectangle, rounded corners, draw, fill=gray!20,
                    text width=12em, minimum height=1cm, align=flush center,
                    on chain, join=by line},
    cloud/.style = {draw, ellipse, fill=red!20, minimum height=1cm,
                    on chain, join=by line},
 decision/.style = {diamond, aspect=1.3, draw, fill=green!30,
                    text width=6em, minimum height=1cm, align=flush center,
                    on chain, join=by line},
     line/.style = {semithick,-Triangle}
                            }% end of styles
     }% end of tikzset
\begin{document}
\pagestyle{empty}
    \begin{tikzpicture}[FlowChart,
node distance = 8mm,
  start chain = going below
                    ]
% Place nodes
\node [cloud,font=\bfseries] (start) {START};
\node [block] (init)        {Randomly generate $n$ groups of $p$ strings.};
\node [block] (identify)    {Choose the leader string for each group.};
\node [block] (mutate)      {Mutate each string according to the given rule.};
\node [block] (transfer)    {Carry out parameter transfer for each group, from other groups.};
\node [block] (evaluate)    {Calculate the fidelity for all the strings in all the groups.};
\node [decision] (decide)   {Termination condition satisfied ?};
\node [block] (stop)        {\textbf{STOP}};
% Draw edges o included with "join" macro
\draw[line] (decide.west) to ["No"] ++ (-1.5,0) |- (identify);
\path       (decide) to ["Yes"] (stop);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容