在tikz中用水平 - 垂直 - 水平线连接两个节点

在tikz中用水平 - 垂直 - 水平线连接两个节点

我正在研究这个例子:简单流程图

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\begin{document}
\pagestyle{empty}
% Define block styles
\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 = 2cm, auto]
    % Place nodes
    \node [block] (init) {initialize model};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [block, below of=init] (identify) {identify candidate models};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {yes} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    \path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}
\end{document}

我想删除“更新模型”节点,并用水平-垂直-水平线直接连接决策节点和“识别...”节点。我天真地尝试使用

 \path [line] (decide) -|- node [near start] {yes} (identify)

但它不起作用。

有什么简单的方法可以做到这一点吗?

答案1

语法-|-默认没有定义,所以如果你想使用它,你需要加载Qrrbrbirlbel 的paths.ortho图书馆

另一种可能性是使用|-(or -|) 语法。它表示垂直线和水平线之间的交点。您可以在以下位置找到更多信息流程图 - 从节点到边缘画线。 或者如何绘制从节点 3 到节点 1 的返回箭头

使用它,很容易在(decide)和之间建立联系(identify)

    \path [line] (decide) --(decide-|expert) |- (identify);

(decide)--(decide-|expert)在由一条水平线和一条垂直线decide.center定义的交点之间画一条线。这条线使用作为原点,但它将从边界开始绘制。decide.centerexpert.centerdecide.centerdecide

(decide-|expert) |- (identify)将绘制一条以 y 坐标为原点的垂直线,(decide-|expert)该线终止于identify.centery 坐标。从那里将绘制一条水平线到边界(identify)

\documentclass[tikz]{standalone}
\usepackage[latin1]{inputenc}
\usetikzlibrary{shapes,arrows}
\usepackage{verbatim}
\begin{document}
% Define block styles
\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 = 2cm, auto]
    % Place nodes
    \node [block] (init) {initialize model};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [block, below of=init] (identify) {identify candidate models};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
%    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) --(decide-|expert) |- (identify);
%    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    \path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容