LaTeX 流程图路径问题

LaTeX 流程图路径问题

我有一个流程图,其中包含我想要的所有节点,但只有一条路径我无法获得。

    \begin{figure}[H]
\begin{center}

\tikzstyle{decision} = [diamond, draw, fill=blue!20, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 3cm, auto]
    % Place nodes
    \node [block] (init) {Set Quotient to 0};
    \node [block, below of=init] (identify) {Subtract Divisor from Dividend};
    \node [block, below of=identify] (evaluate) {Add 1 to Quotient};
    \node [decision, below of=evaluate] (decide) {is Result Register Greater than 0?};
    \node [block, below of=decide, node distance=3cm] (stop) {Stop - Quotient is Correct};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -- node {no}(stop);
    \end{tikzpicture}

% use \figcap to build the caption. DO NOT use the default \caption command
\figcap{Control scheme for Repeated Subtraction Division} % put the caption of your figure here
\end{center}
\end{figure}

我想添加一条从最后一个块(停止)循环到块(识别)的路径。每当我尝试这样做时,我得到的路径都不会绕过块,而是穿过它们。

有任何想法吗?

答案1

您可以使用相对坐标(++(2cm,0))然后垂直水平路径(|-):

\path[line] (stop) -- ++(2cm,0) |- (identify);

\tikzset使用而不是完成的代码\tikzstyle(参见应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,arrows}
\tikzset{%
  decision/.style={
    diamond, draw, fill=blue!20, text width=4.5em, text badly centered,
    node distance=3cm, inner sep=0pt,
  },
  block/.style={
    rectangle, draw, fill=blue!20, text width=5em, text centered,
    rounded corners, minimum height=4em,
  },
  line/.style={
    draw, -latex',
  },
}
\begin{document}
\begin{tikzpicture}[node distance = 3cm, auto]
  % Place nodes
  \node [block] (init) {Set Quotient to 0};
  \node [block, below of=init] (identify) {Subtract Divisor from Dividend};
  \node [block, below of=identify] (evaluate) {Add 1 to Quotient};
  \node [decision, below of=evaluate] (decide) {is Result Register Greater than 0?};
  \node [block, below of=decide, node distance=3cm] (stop) {Stop - Quotient is Correct};
  % Draw edges
  \path [line] (init) -- (identify);
  \path [line] (identify) -- (evaluate);
  \path [line] (evaluate) -- (decide);
  \path [line] (decide) -- node {no}(stop);
  % Loop
  \path[line] (stop) -- ++(-2cm,0) |- (identify);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我建议通过 TikZ 的矩阵注释使用第二种替代方案。这样,垂直距离由rowsep或(colsep对于 2D 方向)设置,而无需使用node distance命令,而且使用量below= of ...也很少。(同样的想法适用于 2D 方向,尤其是流程图,IHMO)

在此处输入图片描述

代码

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{matrix, shapes, arrows, positioning}
\begin{document}    
\begin{figure}
\begin{center}
\tikzstyle{decision} = [diamond, draw, fill=blue!20, 
    text width=4.5em, text badly centered, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, fill=red!20, minimum height=2em]

\begin{tikzpicture}[auto]
% Place nodes with matrix nodes
 \matrix[matrix of nodes, column sep=1cm, row sep=1cm]{%
    \node [block] (init) {Set Quotient to 0};\\
    \node [block] (identify) {Subtract Divisor from Dividend};\\
    \node [block] (evaluate) {Add 1 to Quotient};\\
    \node [decision] (decide) {is Result Register Greater than 0?};\\
    \node [block] (stop) {Stop - Quotient is Correct};\\};
% Draw edges
    \path [line] (init) -- (identify);  
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -- node[right]{No} (stop);
    \path [line] (stop) -- ++(2,0) |- (identify);
    \end{tikzpicture}
\end{center}
\end{figure}
\end{document}

相关内容