我对 Latex 流程图有疑问

我对 Latex 流程图有疑问

请问有人能帮忙吗,我无法正确将块 9 连接到块 10,也无法将块 19 连接到块 6。

\documentclass[border=3mm]{standalone}
% Required package
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\usetikzlibrary{shapes,arrows}

\begin{document}
\begin{tikzpicture}[node distance = 0.3cm,auto][t]

\node[draw,
    rounded rectangle,
    minimum width=2.5cm,
    minimum height=1cm] (block1) {START};


\node[draw,
    below=of block1,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block2) {block1};
        

\node[draw,
    below=of block2,
    minimum width=1.0cm,
    minimum height=1cm
] (block3) {block2};
       

\node[draw,
    diamond,
    below=of block3,
    minimum width=2.5cm,
    inner sep=0] (block4) {block3};
 
\node[draw,
    draw,
    left=of block4,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block5) {block4};
 
\node[draw,
    draw,
    below=of block4,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block6) {block5};

\node[draw,
    draw,
    below=of block6,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block7) {block6};
    
  \node[draw,
    diamond,
    below=of block7,
    minimum width=2.5cm,
    inner sep=0] (block8) {block7};
 
\node[draw,
    draw,
    left=of block8,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block9) {block8};

\node[draw,
    draw,
    below=of block8,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block10) {block9};      

\node[draw,
    draw,
    right=of block2,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block11) {block10};      

\node[draw,
    draw,
    below=of block11,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block12) {block11}; 

\node[draw,
    draw,
    below=of block12,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block13) {block12}; 

\node[draw,
    draw,
    below=of block13,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block14) {block13}; 

\node[draw,
    draw,
    below=of block14,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block15) {block14}; 

\node[draw,
    draw,
    below=of block15,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block16) {block15}; 

\node[draw,
    draw,
    below=of block16,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block17) {block16}; 

\node[draw,
    draw,
    below=of block17,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block18) {block17}; 

\node[draw,
    diamond,
    below=of block18,
    minimum width=2.5cm,
    inner sep=0] (block19) {block18};
    
 \node[draw,
    draw,
    left=of block19,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block20) {block19};
 
\node[draw,
    draw,
    below=of block19,
    minimum width=3.5cm,
    minimum height=0.75cm
] (block21) {END};   
    
% Arrows
\draw[-latex] (block1) edge (block2);
\draw[-latex] (block2) edge (block3);
\draw[-latex] (block3) edge (block4);
\draw[-latex] (block4) edge node[pos=0.3,inner sep=0pt]{Yes}(block6)
     (block4) edge node[pos=0.15,inner sep=0pt]{No}(block5);
 \draw[-latex] (block6) edge (block7);  
  \draw[-latex] (block7) edge (block8);        
\draw[-latex] (block8) edge node[pos=0.3,inner sep=0pt]{Yes}(block9)
     (block8) edge node[pos=0.25,inner sep=0pt]{No}(block10);
 \draw[->] (block10) -| (block11);  
  \draw[-latex] (block11) edge (block12);  
  \draw[->] (block12) edge (block13);
  \draw[->] (block13) edge (block14);
  \draw[->] (block14) edge (block15);
  \draw[->] (block15) edge (block16);
  \draw[->] (block16) edge (block17);
  \draw[->] (block17) edge (block18);
  \draw[->] (block18) edge (block19);
  \draw[-latex] (block19) edge node[pos=0.3,inner sep=0pt]{Yes}(block20)
     (block19) edge node[pos=0.25,inner sep=0pt]{No}(block21);
     \draw[->] (block20) |- (block7);
\end{tikzpicture}
    \end{document}

答案1

以下是一种方法:

  • 引入样式fmtfmtw简化
  • 删除重复的绘图,移除最小宽度和最小高度
  • 稍微清理一下代码
  • 将一些方块稍微向左移动
  • 介绍了汤姆关于连接器的建议

xxx

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

\begin{document}
\begin{tikzpicture}[node distance = 0.3cm,auto, 
fmt/.style={draw, minimum width=2.5cm},
fmtw/.style={draw, minimum width=3.5cm, minimum height=0.75cm}][t]% introducing fmt for simplification

% deleted duplicate draw options
% cleaning up the code a little bit

\node[fmt,
    rounded rectangle,
    minimum height=1cm]         (block1) {START};

\node[fmtw,
    below=of block1,
    minimum height=0.75cm]      (block2) {block1};
        
\node[fmt,
    below=of block2,
    minimum width=1.0cm,
    minimum height=1cm]         (block3) {block2};     

\node[fmt,
    diamond,
    below=of block3,
    inner sep=0]                (block4) {block3};
 
\node[fmtw, left=of block4, 
        xshift=-5mm]            (block5) {block4};% shift to left
 
\node[fmtw,  below=of block4]   (block6) {block5};

\node[fmtw, below=of block6]    (block7) {block6};
    
\node[fmt, diamond, 
  below=of block7, inner sep=0] (block8) {block7};
 
\node[fmtw, left=of block8, 
            xshift=-5mm]        (block9) {block8};% shift to left

\node[fmtw, below=of block8]    (block10) {block9};      

\node[fmtw, right=of block2]    (block11) {block10};      

\node[fmtw, below=of block11]   (block12) {block11}; 

\node[fmtw, below=of block12]   (block13) {block12}; 

\node[fmtw, below=of block13]   (block14) {block13}; 

\node[fmtw, below=of block14]   (block15) {block14}; 

\node[fmtw, below=of block15]   (block16) {block15}; 

\node[fmtw, below=of block16]   (block17) {block16}; 

\node[fmtw, below=of block17]   (block18) {block17}; 

\node[fmt, diamond,
    below=of block18, 
    inner sep=0]                (block19) {block18};
    
\node[fmtw, left=of block19, 
        xshift=-5mm]            (block20) {block19};% moving a little to left

 
\node[fmtw, below=of block19]   (block21) {END};   
    
% Arrows
  \draw[-latex] (block1) edge (block2);
  \draw[-latex] (block2) edge (block3);
  \draw[-latex] (block3) edge (block4);
  \draw[-latex] (block4) edge node[pos=0.3,inner sep=0pt]{Yes}(block6)
     (block4) edge node[pos=0.15,inner sep=0pt]{No}(block5);
  \draw[-latex] (block6) edge (block7);  
  \draw[-latex] (block7) edge (block8);        
  \draw[-latex] (block8) edge node[pos=0.3,inner sep=0pt]{Yes}(block9)
     (block8) edge node[pos=0.25,inner sep=0pt]{No}(block10);
% \draw[->] (block10) -| (block11);  
  \draw[-latex] (block11) edge (block12);  
  \draw[->] (block12) edge (block13);
  \draw[->] (block13) edge (block14);
  \draw[->] (block14) edge (block15);
  \draw[->] (block15) edge (block16);
  \draw[->] (block16) edge (block17);
  \draw[->] (block17) edge (block18);
  \draw[->] (block18) edge (block19);
  \draw[-latex] (block19) edge node[pos=0.3,inner sep=0pt]{Yes}(block20)
     (block19) edge node[pos=0.25,inner sep=0pt]{No}(block21);
%     \draw[->] (block20) |- (block7);
     
  % Tom's comment
  \draw[->] (block10.east) --++(0.15cm,0cm) |- (block11.west); 
  \draw[->] (block20.west) --++(-4cm,0cm) |- (block7.west);
 \end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

答案2

使用chain库并在图片前言中定义节点、箭头和标签的样式:

编辑。 现在我发现我block2在主分支中丢失了节点,而分支“B”中的第一个块应该从节点级别开始A-2。现在我纠正了这个问题。

\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, arrows.meta,
                chains,
                positioning,
                quotes,
                shapes}

\begin{document}
    \begin{tikzpicture}[
    node distance = 3mm and 5mm,
      start chain = A going below,
      start chain = B going below,
       arr/.style = {draw, -Straight Barb},
       box/.style = {draw, minimum width=21mm, minimum height=7mm},
  decision/.style = {diamond, draw, minimum width=24mm, inner sep=0},
every edge/.style = {arr},
every edge quotes/.style={auto=right, font=\footnotesize},
                        ]
    \begin{scope}[nodes={on chain=A, join=by arr}]
\node[box, rounded rectangle]   {START};    % A-1
\node[box]      {block1};
\node[box]      {block2};
\node[decision] {block3};                   % A-4
\node[box]      {block5};
\node[box]      {block6};
\node[decision] {block7};                   % A-7
\node[box]      {block9};
    \end{scope}
\node (C-1)     [box, left=of A-4]  {block4};
\node (C-2)     [box, left=of A-7]  {block8};
    \draw   (A-4) edge ["Yes"] (C-1)
            (A-7) edge ["Yes"] (C-2);
    \begin{scope}[nodes={on chain=B, join= by arr}]
\node[box,
      right=of A-2] {block10};  % B-1
\node[box]      {blocj11)};
\node[box]      {block12};
\node[box]      {block13};
\node[box]      {block14};
\node[box]      {block15};
\node[box]      {block16};
\node[box]      {block17};
\node[decision] {block18};                  % B-9
\node[box]      {END};
    \end{scope}
\node (B-11)    [box, left=of B-9] {block19};
% Arrows
\coordinate[left=of C-2] (aux);
\draw[arr]  (A-8.east) -- ++ (0.2,0) |- (B-1);
\draw[arr]  (B-9)  edge ["Yes"] (B-11);
\draw[arr]  (B-11) -| (aux) |- (A-5);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

尝试使用这两个路径代替:

\draw[->] (block10.east) --++(0.15cm,0cm) |- (block11.west); 
\draw[->] (block20.west) --++(-4cm,0cm) |- (block7.west);

相关内容