Tikz 线与框相交

Tikz 线与框相交

今天我刚开始学习如何使用这个Tikz包来绘制流程图。我一直在阅读手册和一些在线网站,比如我们喜爱的 TeX.SE,但我似乎无法弄清楚如何阻止线条与框相交。我希望它们绕过框。

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\tikzstyle{block} = [rectangle, draw, text width=2em, text centered, rounded corners]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']
\tikzstyle{cloud} = [rectangle, draw, node distance=1.5cm, minimum height=1em, text centered, rounded corners]
\tikzstyle{no} = [draw, ellipse, node distance=1.5cm, minimum height=1em, text centered, rounded corners]
\tikzstyle{yes} = [draw, ellipse, node distance=1.5cm, minimum height=1em, text centered, rounded corners]

\begin{document}
    \begin{center}    
        \begin{tikzpicture}[auto]
        % Place nodes
        \node [block] (init) {Start};
        \node [cloud, below of=init] (council) {Is it Gerard Hall council related?};
        \node [cloud, right=2.5cm of council] (cpay) {Do you need reimbursement?};
        \node [yes, above right=1.47cm of cpay] (cyes) {Yes};
        \node [no, below right=1.5cm of cpay] (cno) {No};
        \node [cloud, below of=council] (robotics) {Is it Dalhousie Robotics related?};
        \node [cloud, below of=robotics] (tutor) {Is it Engineering/Programming/Mathematics tutoring related?};
        \node [cloud, below of=tutor] (else) {So, it's Something Else?};
        \node [block, below=2cm of else] (contact)  {Contact Me.};
        % Draw edges
        \path [line] (init) -- (council);
        \path [line] (council) -- (robotics);
        \path [line] (council) -- (cpay);
        \path [line] (cpay) -| (cyes);
        \path [line] (cpay) -| (cno);
        \path [line] (robotics) -- (tutor);
        \path [line] (tutor) -- (else);
        \path [line] (robotics) |- (contact);
        \path [line] (tutor) |- (contact);
        \end{tikzpicture}
    \end{center}
\end{document}

目前看起来像这样: 上述代码的渲染图

答案1

您可以使用相对坐标,然后使用垂直线。

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{shapes,arrows,positioning}
\tikzset{
block/.style = {rectangle, draw, text width=6em, text centered, rounded corners},
line/.style = {draw, very thick, color=black!50, -latex'},
cloud/.style = {rectangle, draw, node distance=1.5cm, minimum height=1em, text centered, rounded corners},
no/.style = {draw, ellipse, node distance=1.5cm, minimum height=1em, text centered, rounded corners},
yes/.style = {draw, ellipse, node distance=1.5cm, minimum height=1em, text centered, rounded corners}
}

\begin{document}
%    \begin{center}
 \noindent       \begin{tikzpicture}[auto]
        % Place nodes
        \node [block,text width=2em] (init) {Start};
        \node [cloud, below of=init] (council) {Is it Gerard Hall council related?};
        \node [cloud, right=2.5cm of council] (cpay) {Do you need reimbursement?};
        \node [yes, above right=1.5cm of cpay,minimum width = 3em] (cyes) {Yes};
        \node [no, below right=1.5cm of cpay,minimum width = 3em] (cno) {No};
        \node [cloud, below of=council] (robotics) {Is it Dalhousie Robotics related?};
        \node [cloud, below of=robotics] (tutor) {Is it Engineering/Programming/Mathematics tutoring related?};
        \node [cloud, below of=tutor] (else) {So, it's Something Else?};
        \node [block, below=2cm of else,align=center, text width = 4em] (contact)  {Contact \\ Me.};
        % Draw edges
        \path [line] (init) -- (council);
        \path [line] (council) -- (robotics);
        \path [line] (council) -- (cpay);
        \path [line] (cpay) -| (cyes);
        \path [line] (cpay) -| (cno);
        \path [line] (robotics) -- (tutor);
        \path [line] (tutor) -- (else);
        \path [line] (robotics) -- ++(6,0) |- (contact);
        \path [line] (else) -- (contact);
        \path [line] (tutor) -- ++(-6,0) |- (contact);
        \end{tikzpicture}
%    \end{center}
\end{document}

在此处输入图片描述

我还将所有\tikzstyles 更改为,\tikzset因为前者已被弃用。

答案2

使用chains库和join宏,代码可以更短:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                shapes}
\makeatletter
\tikzset{suspend join/.code={\def\tikz@after@path{}},
%
     base/.style = {rectangle, rounded corners, draw,
                    minimum height=2ex, align=center,
                    on chain, join=by line},
    block/.style = {base, text width=40mm},
    cloud/.style = {base},
       no/.style = {ellipse, draw, minimum width=3em, inner sep=2pt,
                    node contents={Yes}},
      yes/.style = {no, node contents={No}},
     line/.style = {very thick, color=black!50, -Latex}
        }
\makeatother
\begin{document}
    \begin{center}
        \begin{tikzpicture}[
    node distance = 5mm and 9mm,
      start chain = going below]
        % Place nodes
        \node [cloud] (init)        {Start};
        \node [block] (council)     {Is it Gerard Hall council related?};
        \node [block] (robotics)    {Is it Dalhousie Robotics related?};
        \node [block] (tutor)       {Is it Engineering/Programming/\\
                                     Mathematics tutoring related?};
        \node [block] (else)        {So, it's Something Else?};
        \node [cloud] (contact)     {Contact Me.};
%
        \node [block, suspend join,
               right=of council] (cpay)  {Do you need reimbursement?};
        \node (cyes) [yes, above right=of cpay];
        \node (cno)  [no,  below right=of cpay];
%
        % Draw edges
        \draw [line] (council) -- (cpay);
        \draw [line] (cpay) -| (cyes);
        \draw [line] (cpay) -| (cno);
        \draw [line] (robotics.east) -- ++ (0.5,0) |- (contact);
        \draw [line] (tutor.west) -- ++ (-0.5,0) |- (contact);
        \end{tikzpicture}
    \end{center}
\end{document}

在此处输入图片描述

(红线表示页面布局)

笔记:由于您的流程图比文档文本宽度宽,我将节点中的文本分成多行(使用text width=40mm)。

相关内容