LaTeX TikZ:创建复杂的流程图 - 如何创建对角决策框?

LaTeX TikZ:创建复杂的流程图 - 如何创建对角决策框?

我是 LaTeX 初学者,正在努力创建流程图。到目前为止,我有以下代码:

\documentclass[final, english]{scrreprt}

\usepackage{tikz,pgfplots}
    \pgfplotsset{compat=newest}
\usetikzlibrary{arrows,trees,graphs,positioning, arrows, chains, shapes, arrows.meta,decorations.pathreplacing}
\usepackage{pstricks,pst-node,pstricks-add}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}[node distance=1.5cm]

    \tikzstyle{process1} = [rectangle, minimum width=11cm, minimum height=1cm, text centered, draw=black] 
    \tikzstyle{process2} = [rectangle, minimum width=6cm, minimum height=1cm, text centered, draw=black] 
    \tikzstyle{decision} = [diamond, base, minimum width=5cm, minimum height=1cm, text centered, draw=black, aspect=2, inner xsep=0mm]
    \tikzstyle{stop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black]
    \tikzstyle{arrow} = [thick,->,>=stealth]
    
    %Nodes erstellen
    \node (start) [process1] {\begin{tabular}{c}
        Box 1: Row 1 \\
        Row 2
    \end{tabular}};    
    
    %Process boxes
    \node (p2) [process1, below of=start, yshift = -0.2cm] {Box 2};
    \node (p3) [process1, below of=p2] {Box 3};
    \node (p4) [process1, below of=p3] {Box 4};
    \node (p5) [process1, below of=p4] {Box 5};

    %decision boxes
    \node (dec1) [decision, below left of=p5, yshift=-1.5cm] {\begin{tabular}{c}
        Decision Box 1: Row 1 \\
        Row 2 \\
        Row 3 \\
        Row 4
    \end{tabular}};     
    \node (dec2) [decision, below right of=dec1, yshift=-4cm] {\begin{tabular}{c}
        Decision Box 2: Row 1 \\
        Row 2 \\
        Row 3 \\
        Row 4
    \end{tabular}};       
    
    %small process boxes
    \node (p6) [process2, below left of=dec1, yshift=-2cm] {\begin{tabular}{c}
        Box 6: Row 1 \\
        Row 2 \\
        Row 3 
    \end{tabular}};   
    
    \node (p7) [process2, below of=dec2, yshift=-2cm] {\begin{tabular}{c}
        Box 7: Row 1 \\
        Row 2 \\
        Row 3 
    \end{tabular}};    
    
    \node (p7) [process2, below of=dec2] {\begin{tabular}{c}
        Box 7: row 1\\
        row 2
    \end{tabular}};
    
    \node (dec3) [decision, below of=p6, yshift=-2cm] {Decision box};
    
    \node (p8) [process2, below of=p7] {\begin{tabular}{c}
        Box 7: row 1\\
        row 2
    \end{tabular}};

    \node (dec4) [decision, below right of=dec3, yshift=-2cm] {\begin{tabular}{c}
        Last decision box \\
        another row
    \end{tabular}};    
    
    \node (stop) [stop, right of=dec4] {STOP};
    
    % arrows
    \draw [arrow] (start) -- (p2);
    \draw [arrow] (p2) -- (p3);
    \draw [arrow] (p3) -- (p4);
    \draw [arrow] (p4) -- (p5);    
    
    % Labeled arrows
    \draw [arrow] (dec1) -- node {Yes} (p6);
    \draw [arrow] (dec1) -- node {No} (dec2);
    
    \draw [arrow] (dec2) -- node {Yes} (p7);
    \draw [arrow] (dec2) -- node {No} (p8);
    
    \draw [arrow] (dec3) -- node {Yes} (dec1);
    \draw [arrow] (dec3) -- node {No} (dec4);
    
    \draw [arrow] (dec4) -- node {Yes} (stop);
    \draw [arrow] (dec4) -- node {No} (p4);
    
\end{tikzpicture}
\end{figure}

\end{document}

我目前失败了,因为决策框彼此呈对角线排列。最后,流程图应该尽可能与模板相似,因此我还必须管理对齐,以便以相同的方式格式化。

  1. 如何使决策框 1 和 2 彼此对角排列?
  2. 有什么技巧可以让箭头在某些点处形成角度曲线并且不会直接终止于一个框中,而是终止于其他箭头中?
  3. 除了创建表格环境之外,有没有更好的方法在框内创建换行符?

我想感谢所有能帮助我的人!

答案1

只是为了让你明白我在评论中所说的内容。这不是最好的方法,但至少它是可定制的并且易于理解。第一个框可以(应该?)使用 来完成chain,这需要更少的代码。无论如何:

流程图

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}

\tikzset{
    process1/.style={rectangle, minimum width=11cm, minimum height=1cm, text width=10cm,text centered, draw=black},
    process2/.style={rectangle, minimum width=6cm, minimum height=1cm, text width=5cm,text centered, draw=black},
    decision/.style={diamond, text centered, draw=black, aspect=2, inner xsep=-2mm},
    stop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black},
    arr/.style={thick,-stealth}
    }
\begin{document}
    \begin{tikzpicture}[node distance=1cm]
        \node[process1] (start) {Initiate the seed permutation sequence by a proven scheduling heuristic: Set of scheduled jobs $=\lbrace\delta\rbrace$};
        \node[below = of start,process1] (box2) {Pick the first job that occurs in $\lbrace\delta\rbrace$};
        \node[below = of box2,process1] (box3) {Whatever text you want to put here};
        \node[below = of box3,process1] (box4) {Whatever text you want to put here};
        \node[below = of box4,process1] (box5) {Whatever text you want to put here};
        
        \node[below left =2cm and -3cm of box5,decision,text width=4cm] (dec1) {On the following machine stage, can job be inserted completely ahead of preceding job in $\lbrace\delta\rbrace$};
        \node[below right = 1cm and 2cm of dec1,decision,text width=4cm] (dec2) {Is insertion including job shifting of precding jobs in $\lbrace\delta\rbrace$ possible?};
        
        \node[below left = 2cm and -3cm of dec1,process2] (p6) {Implement insertion of job, compute its ST and FT on machine \textit{i}, reschedule jobs in $\lbrace\delta\rbrace$ which are affected by insertion on machine \textit{i}};
        
        \node[below = 4cm of  p6,decision] (p7) {$i< m$?};
        
        \draw[arr] (start) -- (box2);
        \draw[arr] (box2) -- (box3);
        \draw[arr] (box3) -- (box4);
        \draw[arr] (box4) -- (box5);
        \draw[arr] (box5.south) --++ (0,-10pt) -| (dec1);
        \draw[arr] (dec1) -| (dec2) node[pos=0.1,above]{\textit{No}};
        \draw[arr] (dec1.south) --++ (0,-10pt) -| (p6) node[pos=0.3,above]{\textit{Yes}};
        \draw[arr] (p6) -- (p7);
        \draw[arr] (dec2) |- ([yshift=20pt]p7.north);
    \end{tikzpicture}
\end{document}

答案2

设计有点不同,在流程图代码中使用了chains库:

\documentclass[final, english]{scrreprt}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains, 
                positioning, 
                quotes,
                shapes.symbols}
\makeatletter
\tikzset{FlowChart/.style={
suppress join/.code={\def\tikz@after@path{}},
    off chain/.code={\def\tikz@lib@on@chain{}},
%
node distance = 7mm and 5mm,
  start chain = going below,
%
   arr/.style = {thick,-Stealth},
  base/.style = {draw, semithick, minimum height=3ex, align=center}, 
proces/.style = {base, text width=##1},
proces/.default = 24em,
decision/.style = {signal, signal pointer angle=120,
                   signal to=west and east, 
                   base, text width=##1, inner xsep=0ex},
decision/.default = 8em,
  stop/.style = {rounded corners, fill=gray!10, minimum width=12ex},
                            }
        }
\makeatother

\begin{document}
    \begin{figure}
\centering
    \begin{tikzpicture}[FlowChart]
%   Nodes erstellen
    \begin{scope}[nodes={on chain, join=by arr}]
\node (s)   [proces]    {Initiate the seed permutation sequence by a proven scheduling heuristic: Set of scheduled jobs $=\{\delta\}$};
%   Process boxes
\node (p2) [proces]     {Box 2};
\node (p3) [proces]     {Box 3};
\node (p4) [proces]     {Box 4};
\coordinate (aux0);
\node (p5) [proces]     {Box 5};
\coordinate (aux1);
%   decision boxes
\node (d1) [decision=12em]   
                        {On the following machine stage, can job be inserted completely ahead of preceding job in $\{\delta\}$};
%   left branch
\node (p6)  [proces=9em, 
off chain, suppress join,
             below left=of d1]   {Implement insertion of job, compute its ST and FT on machine $i$, reschedule jobs in $\{\delta\}$ which are affected by insertion on machine $i$};
\coordinate [below=of p6]   (aux2);
\node (d2)  [decision, 
             below=14mm of aux2]   {$i\leq m$ ?};
\node (d3)  [decision]   {Are al jobs in $\{\delta\}$ considered};
    \end{scope}
%   right branch
\node (d4)  [decision=9em, 
             below right=of d1] {Is insertion including job shifting of precding jobs in $\{\delta\}$ possible?};
\node (p7)  [proces=12em,
             below left=of d4]       {Implement insertion of job, compute its ST and FT on machine $i$, reschedule jobs in $\{\delta\}$ an machine $i$ to $m$ and check further possible job swaps};
\node (p8)  [proces=8em,
             below=of d4]  {Compute ST and FT of this job on machine $i$};
% end flowchart
\node (s)   [stop,
             right=of d3 -| p7] {STOP};
%   connection not included in join macro
    \draw[arr] (d1.west) node[above left]   {Yes} -|  (p6); 
    \draw[arr] (d1.east) node[above right]  {No} -|  (d4);
    
    \draw[arr] (d2.west) node[above left]   {Yes} -|  ([xshift=-2.2em] p6.west) |- (aux1);
    \draw[arr] (d3.west) node[above left]   {No}  -|  ([xshift=-3em] p6.west) |- (aux0);

    \draw[arr] (d4.west) node[above left]   {Yes} -| (p7);
    \draw[arr] (d4.south) -- node[right]    {No}  (p8)
               (p7.west |- aux2) -- (aux2); 
    \draw[arr] (p8) |- (d2);
    \draw[arr] (d3.east) node[above right]  {Yes}  -- (s);
    \end{tikzpicture}
    \end{figure}
\end{document}

在此处输入图片描述

(红线表示页面布局)

相关内容