我可以把一个节点放入另一个节点吗

我可以把一个节点放入另一个节点吗

我想制作带有 while 循环和其中的一些操作的流程图。我已经编写了以下 latex 代码:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
    chains,       
    positioning,  
    quotes,       
    shapes.geometric,
    shapes.multipart,
    babel
}

\oddsidemargin=-15.4mm
\textwidth=190mm
\headheight=-32.4mm
\textheight=277mm
\tolerance=100
\parindent=0pt
\parskip=8pt
\pagestyle{empty}

\makeatletter
\tikzset{FlowChart/.style={  % <--- corrected, new
        base/.style = {draw,
            minimum width=3cm, minimum height=1cm, align=center,
            outer sep=0pt,
            on chain, join=by arrow},
        startstop/.style = {base, rounded corners, fill=blue!30},
        process/.style = {base, fill=orange!30},
        decision/.style = {base, diamond, aspect=1.3, fill=green!30},
        io/.style = {base, trapezium, trapezium stretches body,
            trapezium left angle=70, trapezium right angle=110,
            fill=red!30,
            text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
        },
        loop/.style = {base, rectangle split, rectangle split parts=2,
            fill=gray!50},
        arrow/.style = {thick,-Triangle},
        % suspend
        suspend join/.code={\def\tikz@after@path{}}
    }
}% end of tikzset
\makeatother

\begin{document}        
    {\textbf{Задача 2.}}
    \\
    Схема алгоритма:
    \\
    \begin{center}
        \begin{tikzpicture}[FlowChart,
            node distance = 1cm and 3cm,
            start chain = A going below
            ]
            
            \node (start) [startstop] {Start};
            
            \node (input) [io] {Input n};
            
            \node (for) [loop]{ 
                \nodepart{one} for i = 1; i<len(n); i++
                \nodepart{two} Output n[i].
            };

            \node (stop) [startstop, below of=for] {End of programm};
        \end{tikzpicture}
    \end{center}
    
\end{document}

目前我得到了这个:

程序流程图

我怎样才能将“输出节点”放入 for 循环节点中?此外,对于另一个程序,我需要将整个决策节点放入 for 循环中。这可能吗?

从图形上看,我希望循环块如下所示: for 循环

对于另一个程序,我希望它看起来像这样: 另一个程序的 for 循环

答案1

第二个例子(稍微复杂一些):

在此处输入图片描述

\documentclass[12pt, a4paper]{article}
\usepackage[russian]{babel}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                fit,    % new
                positioning,
                quotes,
                shapes.geometric,
                shapes.multipart,
                babel
}
\usepackage[margin=10mm]{geometry}
\tolerance=100
\parindent=0pt
\parskip=8pt
\pagestyle{empty}

\makeatletter
\tikzset{FlowChart/.style={  
     base/.style = {draw,
                    minimum width=3cm, minimum height=1cm, align=center,
                    outer sep=0pt},
startstop/.style = {base, rounded corners, fill=blue!30},
  process/.style = {base, fill=orange!30},
 decision/.style = {base, diamond, aspect=1.3, fill=green!30},
      FIT/.style = {base, semithick, inner sep=3mm, fit=##1},  % new
       io/.style = {base, trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110,
                    fill=red!30,
                    text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
                    },
     loop/.style = {base, rectangle split, rectangle split parts=2,
                    fill=gray!50},
    arrow/.style = {thick,-Triangle},
 % suspend
                suspend join/.code={\def\tikz@after@path{}}
    }
}% end of tikzset
\makeatother

\begin{document}
\begin{center}
    \begin{tikzpicture}[FlowChart,
node distance = 5mm and 7mm,
  start chain = going below
                        ]
\node (d1) [decision]                   {$n>0$};
\node (d2) [process, 
            below right=of d1.south] {n=n+1};
\coordinate[below=of d1 |- d2.south] (aux1);
\node (d3) [io, 
            below=of aux1]            {output};
%
\draw   (d1) -| node[pos=0.25,above] {Yes}    (d2)  |- (aux1)
        (d1.west) -- node[pos=0.25,above] {No} ++ 
        (-1,0) coordinate (aux2) |- (aux1);
\draw[arrow]    (aux1) -- (d3);
% fit
\node (f1) [FIT=(d1) (aux2) (d2) (d3)] {};
\node (f2) [FIT=(f1.north west) (f1.north east),
            inner sep=0pt,
            above=0pt of f1]
           {for $j=1$; $j<\mathrm{len}(r)$; $j+1$};
    \end{tikzpicture}
\end{center}
\end{document}

笔记:

  • 对于这张图片,我改变了FlowChart风格。因此,现在已更正/采用了这个新版本。请参阅您之前的问题。
  • 您可以按照与上述相同的方式绘制第一个草图。主要区别在于它只包含一个节点(io

附录: 假设你只有一张这样的合成图像,你可以开始用它的组合进行绘制,并在其上方f2和下方添加节点f1。为此,有必要替换旧样式

    arrow/.style = {thick,-Triangle},

其中两个新的箭头指向相反的方向:

      arr/.style = {thick,-Triangle},
      arl/.style = {thick,Triangle-},

完成 MWE:

\documentclass[12pt, a4paper]{article}
\usepackage[russian]{babel}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                fit,    % new
                positioning,
                quotes,
                shapes.geometric,
                shapes.multipart,
                babel
                }
\usepackage[margin=10mm]{geometry}
\tolerance=100
\parindent=0pt
\parskip=8pt
\pagestyle{empty}

\makeatletter
\tikzset{FlowChart/.style={
     base/.style = {draw,
                    minimum width=3cm, minimum height=1cm, align=center,
                    outer sep=0pt},
startstop/.style = {base, rounded corners, fill=blue!30},
  process/.style = {base, fill=orange!30},
 decision/.style = {base, diamond, aspect=1.3, fill=green!30},
      FIT/.style = {base, semithick, inner sep=3mm, fit=##1},  % new
       io/.style = {base, trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110,
                    fill=red!30,
                    text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
                    },
     loop/.style = {base, rectangle split, rectangle split parts=2,
                    fill=gray!50},
      arr/.style = {thick,-Triangle},
      arl/.style = {thick,Triangle-},
 % suspend
                suspend join/.code={\def\tikz@after@path{}}
    }
}% end of tikzset
\makeatother

\begin{document}
\begin{center}
    \begin{tikzpicture}[FlowChart,
node distance = 5mm and 13mm,
  start chain = A going above,
  start chain = B going below
                        ]
% nodes in node                                         
\node (d1) [decision]                   {$n>0$};
\node (d2) [process,
            below right=of d1.south] {n=n+1};
\coordinate[below=of d1 |- d2.south] (aux1);
\node (d3) [io,
            below=of aux1]            {output};
%
\draw   (d1) -| node[pos=0.25,above] {Yes}    (d2)  |- (aux1)
        (d1.west) -- node[pos=0.25,above] {No} ++
        (-1,0) coordinate (aux2) |- (aux1);
\draw[arr]  (aux1) -- (d3);
% fit
    \begin{scope}[nodes={on chain=B, join= by arr}]
\node (f1) [FIT=(d1) (aux2) (d2) (d3)] {};
% nodes below  f1
\node (output)  [io,
                 below=of f1]   {Вывод pow};
\node (stop)    [startstop]     {Конец программы};
    \end{scope}
    \begin{scope}[nodes={on chain=A, join= by arl}]
\node (f2) [FIT=(f1.north west) (f1.north east),
            inner sep=0pt,
            above=0pt of f1]
           {for $j=1$; $j<\mathrm{len}(r)$; $j+1$};
% nodes above  f2
\node (ds1)     [decision,
                 above=of f2]   {$n = 0?$};
\node (input)   [io]            {Ввод n};
\node (start)   [startstop]     {Начало};
    \end{scope}
\node (y-case) [process,right=of ds1]   {pow = 1};
%
\draw[arr]  (ds1) to ["Yes"] (y-case);
\draw[arr]  (y-case) |- (f2);
\draw[arr]  (ds1.west) to [pos=0.2, "No" ']  ++ (-3,0) |- (f2); % new
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

当然,以上只是解决问题的可能方法之一。其他方法之一可能是,您为内部节点单独绘制图像,将其存储在某个节点中\savebox,然后在选定节点中使用\node [...] {\usebox{˙<\saveboxname>}。为了展示这个选项,我需要更多的空闲时间。

相关内容