在 Latex 中绘制复杂的流程图(图表)

在 Latex 中绘制复杂的流程图(图表)

我对在 Latex 中绘制流程图还比较陌生。

从此开始:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=3cm, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

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

\node (start) [startstop] {Data};
\node (in1) [io, below of=start] {Sonification};
\node (pro1) [process, below of=in1] {User};
\node (dec1) [decision, below of=pro1, yshift=-0.5cm] {Decision 1};
\node (pro2a) [process, below of=dec1, yshift=-0.5cm] {Process 2a text text text text text text text text text text};
\node (pro2b) [process, right of=dec1, xshift=2cm] {Process 2b};
\node (out1) [io, below of=pro2a] {Output};
\node (stop) [startstop, below of=out1] {Stop};

\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (dec1);
\draw [arrow] (dec1) -- node[anchor=east] {yes} (pro2a);
\draw [arrow] (dec1) -- node[anchor=south] {no} (pro2b);
\draw [arrow] (pro2b) |- (pro1);
\draw [arrow] (pro2a) -- (out1);
\draw [arrow] (out1) -- (stop);


\end{tikzpicture}

\end{document}

但是,在盒子内再添加一个盒子似乎很困难。

我想要画类似这样的: A

答案1

这只是为了阐明我的上述评论。正如我所说,你需要三层,就像这个帖子。我在代码中标出了各个步骤,它们位于不同的范围内。

  1. 您用矩阵来排列您想要排列的节点(或者chains只是定位)。
  2. 您可以在后面的图层上填充白色节点main,该图层称为background
  3. 您可以在下面调用的层上为这些节点填充灰色behind

\documentclass[tikz,border=3pt]{standalone}
\usetikzlibrary{positioning,matrix,fit,calc}
\pgfdeclarelayer{behind}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{behind,background,main,foreground}
\begin{document}
\begin{tikzpicture}[T/.style={text width=10em,node font=\bfseries},
 A/.style={align=center},B/.style={A,draw},C/.style={text width=10em,align=right},
 L/.style={node font=\tiny,text=blue},
 F/.style={draw,dashed,fill=white},font=\sffamily]
 \matrix[matrix of nodes,nodes={minimum width=10.3em}] (mat){
  |[T,label={[anchor=north east,L,alias=L1]north east:Information Generator}]| Domain\\[2em]
  |[A]| Data\\[2em]
  |[T,text height=1.2em,label={[anchor=north east,L,alias=L2]north east:Communicative Medium}]| 
      Sonification\\[-1em]
  |[C]|Task\\
  |[C]|Model\\[1.5em]
  |[B]| Sound Representation\\[1.5em]
  |[B]| Sound Generation\\[1.5em]
  |[B]| Sound representation\\[1.5em]
  |[T,label={[anchor=north east,L,alias=L3]north east:Information Receiver}]| User\\[2em]
  |[A]| Ear\\[1.5em]
  |[A]| Sound Perception (brain)\\[2em]
  |[C]| Musical Knowledge\\
  |[C]| Acoustic Memory\\[1.5em]
 };
 \foreach \X in {6,7,10}
  {\draw[-latex] (mat-\X-1.south-|mat.center) -- 
  (mat-\the\numexpr\X+1\relax-1.north-|mat.center);}
 \draw[-latex] (mat-2-1.south-|mat.center) -- (L2.north-|mat.center);
 \draw[-latex] (L2.south-|mat.center) -- 
 (mat-6-1.north-|mat.center);
 \draw[-latex] (L2.south-|mat.center) -- 
 (mat-6-1.north-|mat.center);
 \begin{scope}% step 2
  \begin{pgfonlayer}{background}
   \node[F,fit=(mat-1-1)(mat-2-1)](F1){};
   \node[F,fit=(mat-3-1)(mat-8-1)](F2){};
   \node[F,fit=(mat-9-1)(mat-13-1)](F3){};
  \end{pgfonlayer}
  \draw[-latex] (F3.west) -| ++(-3em,4em) node[above] (Actions) {Actions};
  \draw[-latex] (Actions.north) |- (mat-3-1-|F2.west)
  node[pos=0.75,align=left,font=\tiny,below]{Control\\ interaction};
  \draw[-latex] (Actions.north) |- (F1.west)
  node[pos=0.75,align=left,font=\tiny,above]{Collection\\ Selection};
  \node[above left=2em and 3em of mat.north east,node font=\bfseries](title)
  {Typical Sonification Data Flow};
 \end{scope}
 \begin{scope}% step 3
  \begin{pgfonlayer}{behind}
   \node[fit=(mat)(title),draw,fill=gray!20,inner sep=1ex]{};
  \end{pgfonlayer}
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者与您的文档类和缺失的箭头有关。

\documentclass[12pt,oneside]{book} \usepackage{tikz}
\usetikzlibrary{positioning,matrix,fit,calc}
\pgfdeclarelayer{behind}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{behind,background,main,foreground}
\begin{document}
\begin{tikzpicture}[T/.style={text width=10em,node font=\bfseries},
 A/.style={align=center},B/.style={A,draw},C/.style={text width=10em,align=right},
 L/.style={node font=\tiny,text=blue},
 F/.style={draw,dashed,fill=white},font=\sffamily]
 \matrix[matrix of nodes,nodes={minimum width=10.3em}] (mat){
  |[T,label={[anchor=north east,L,alias=L1]north east:Information Generator}]| Domain\\[2em]
  |[A]| Data\\[2em]
  |[T,text height=1.2em,label={[anchor=north east,L,alias=L2]north east:Communicative Medium}]| 
      Sonification\\[-1em]
  |[C]|Task\\
  |[C]|Model\\[1.5em]
  |[B]| Sound Representation\\[1.5em]
  |[B]| Sound Generation\\[1.5em]
  |[B]| Sound representation\\[1.5em]
  |[T,label={[anchor=north east,L,alias=L3]north east:Information Receiver}]| User\\[2em]
  |[A]| Ear\\[1.5em]
  |[A]| Sound Perception (brain)\\[2em]
  |[C]| Musical Knowledge\\
  |[C]| Acoustic Memory\\[1.5em]
 };
 \foreach \X in {6,7,10}
  {\draw[-latex] (mat-\X-1.south-|mat.center) -- 
  (mat-\the\numexpr\X+1\relax-1.north-|mat.center);}
 \draw[-latex] (mat-2-1.south-|mat.center) -- (L2.north-|mat.center);
 \draw[-latex] (L2.south-|mat.center) -- 
 (mat-6-1.north-|mat.center);
 \draw[-latex] (L2.south-|mat.center) -- 
 (mat-6-1.north-|mat.center);
 \draw[-latex] (mat-8-1.south-|mat.center) -- (mat-10-1.north-|mat.center);
 \begin{scope}% step 2
  \begin{pgfonlayer}{background}
   \node[F,fit=(mat-1-1)(mat-2-1)](F1){};
   \node[F,fit=(mat-3-1)(mat-8-1)](F2){};
   \node[F,fit=(mat-9-1)(mat-13-1)](F3){};
  \end{pgfonlayer}
  \draw[-latex] (F3.west) -| ++(-3em,8em) node[above] (Actions) {Actions};
  \draw[-latex] (Actions.north) |- (mat-3-1-|F2.west)
  node[pos=0.75,align=left,font=\tiny,below]{Control\\ interaction};
  \draw[-latex] (Actions.north) |- (F1.west)
  node[pos=0.75,align=left,font=\tiny,above]{Collection\\ Selection};
  \node[above left=2em and 3em of mat.north east,node font=\bfseries](title)
  {Typical Sonification Data Flow};
 \end{scope}
 \begin{scope}% step 3
  \begin{pgfonlayer}{behind}
   \node[fit=(mat)(title),draw,fill=gray!20,inner sep=1ex]{};
  \end{pgfonlayer}
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

编辑 作为起点(从我的答案的第一个版本进行扩展和简化)。

\documentclass[12pt, tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                backgrounds,
                chains,
                fit,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 3mm and 0mm,
  start chain = going below,
   arr/.style = {thick, -{Triangle[angle=60:3pt 2]}, semithick},
   box/.style = {draw, dashed, fill=white,
                 minimum height=#1, text width=64mm,
                 inner sep=2mm, font=\small\sffamily,},
 sound/.style = {box, solid,
                 text width=36mm,  
                 on chain, join=by arr},
   lbl/.style = {font=\scriptsize\sffamily, 
                 text=blue, align=right,
                 below left, anchor=north east},
   FIT/.style = {draw, fill=gray!30, inner xsep=2mm, 
                 inner ysep=6mm, yshift=3mm,
                 fit=#1},      
 title/.style = {font=\small\bfseries\sffamily, below right,
                 anchor=north west}  
                        ]
\node (f1) [box=24mm] {};
    \node (f1a) [title] at (f1.north west) {DOMAIN};
    \node (f2b) [lbl] at (f1.north east)
                {Information generator};
    \node (f1c) [above=of f1.south]        {Data};
\node (f2) [box=52mm, below=of f1] {};
    \node (f2a) [title] at (f2.north west) {SONIFICATION};
    \node (f2b) [lbl] at (f2.north east)
                {Comunicaption Medium};
    \node (f2c) [below left=of f2b.south -| f2.east,
                 anchor=north east, align=right]
                                        {Task\\ Model};
    \node (f2d) [sound,
                 below=of f2c.south -| f2]    {Sound Representation};
    \node (f2e) [sound]                 {Sound ...};
    \node (f2f) [sound]                 {Sound ...};  
\node (f3) [box=38mm, below=of f2] {};
    \node (f3a) [title] at (f3.north west) {USER};
    \node (f3b) [lbl] at (f3.north east)
                {Information receiver};
    \node (f3c) [below=of f3a.south -| f3]  {Ear};
    \node (f3d) [below=of f3c]  {Sound Perception (Brain)};
    \node (f3e) [above left, align=right] at (f3.south east) 
                    {Musical Knowledge\\
                     Acoustic Memory};
%%
\draw[arr]  (f1c) -- (f2d);
\draw[arr]  (f2f) -- (f3c);
\draw[arr]  (f3c) -- (f3d);
    \node   (act) [left=11mm of f2f -| f2.west] {Action};
\draw[arr]  (f3) -| (act);
\draw[arr]  (act) |- (f2a -| f2.west) 
                node[lbl,text=black] {Control\\ Interraction}; 
\draw[arr]  (act |- f2a) |- (f1.west)
                node[lbl,text=black] {Colection\\ Selection};
%%%%
    \scoped[on background layer]
\node (f) [FIT=(f1) (act) (f3)] {};
    \node [title] at (f.north west) {Typical Sonification Data Flow};
\end{tikzpicture}
\end{document}

图像代码非常简单(除了在 Sonification 模块中使用“声音”节点的“链”)。它需要对模块高度进行小幅调整,一层背景层就足够了。

在此处输入图片描述

编辑(2)MWE 现在已适应字体大小。节点的宽度和高度12pt已更改。这些更改是必要的,因为节点形状的大小是硬编码的,并且已适应较小的字体大小。在您的问题中,您告诉我们您使用文档类的默认字体大小,即。soundbox10ptarticle10pt

编辑(3)在 MWE 中,毫米被替换为em(代码中的更改用 表示% <---)。此距离的大小取决于使用的字体大小(如 Schrödinger's cat 在下面的评论中所建议的那样)。还被认为是您关于将节点内容居中的后续问题sound

\documentclass[12pt,tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                backgrounds,
                chains,
                fit,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 0.8em and 0em,   % <---
  start chain = going below,
   arr/.style = {thick, -{Triangle[angle=45:3pt 3]}, semithick},    % <---   
   box/.style = {draw, dashed, fill=white,
                 minimum height=#1, text width=18em,    % <---
                 inner sep=2mm, font=\small\sffamily,},
   lbl/.style = {font=\scriptsize\sffamily, text=blue, align=right,
                 below left, anchor=north east},
   FIT/.style = {draw, fill=gray!30, inner xsep=0.5em,  % <---
                 inner ysep=1em, yshift=0.5em,          % <---
                 fit=#1},
 sound/.style = {box, solid, text width=9em,            % <---
                 align=center, on chain, join=by arr},
 title/.style = {font=\small\bfseries\sffamily, 
                 below right, anchor=north west}
                        ]
\node (f1) [box=4em] {};    % <---
    \node (f1a) [title] at (f1.north west) {DOMAIN};
    \node (f2b) [lbl] at (f1.north east)
                {Information generator};
    \node (f1c) [above=of f1.south]        {Data};
\node (f2) [box=13em,       % <---
            below=of f1] {};
    \node (f2a) [title] at (f2.north west) {SONIFICATION};
    \node (f2b) [lbl] at (f2.north east)
                {Comunicaption Medium};
    \node (f2c) [below left=of f2b.south -| f2.east,
                 anchor=north east, align=right]
                                        {Task\\ Model};
    \node (f2d) [sound,
                 below=of f2c.south -| f2]    {Sound Representation};
    \node (f2e) [sound]                 {Sound ...};
    \node (f2f) [sound]                 {Sound ...};
\node (f3) [box=9em,        % <---
            below=of f2] {};
    \node (f3a) [title] at (f3.north west) {USER};
    \node (f3b) [lbl] at (f3.north east)
                {Information receiver};
    \node (f3c) [below=of f3a.south -| f3]  {Ear};
    \node (f3d) [below=of f3c]  {Sound Perception (Brain)};
    \node (f3e) [above left, align=right] at (f3.south east)
                    {Musical Knowledge\\
                     Acoustic Memory};
%%
\draw[arr]  (f1c) -- (f2d);
\draw[arr]  (f2f) -- (f3c);
\draw[arr]  (f3c) -- (f3d);
    \node   (act) [left=3em of f2f -| f2.west] {Action};
\draw[arr]  (f3) -| (act);
\draw[arr]  (act) |- (f2a -| f2.west)
                node[lbl,text=black] {Control\\ Interraction};
\draw[arr]  (act |- f2a) |- (f1.west)
                node[lbl,text=black] {Colection\\ Selection};
%%%%
    \scoped[on background layer]
\node (f) [FIT=(f1) (act) (f3)] {};
    \node [title] at (f.north west) {Typical Sonification Data Flow};
\end{tikzpicture}
\end{document}

\end{document}

结果与之前类似,但这次字体大小的改变不会改变图像的外观。它只会根据使用的字体大小进行缩放。你可以12pt11pt10pt` 替换使用的字体大小来证明这一点。这样图像只会按比例缩小。

在此处输入图片描述 (字体大小 12 pt) 在此处输入图片描述 (字体大小10pt)

相关内容