4

I am relatively new to drawing a flow chart in Latex.

Started with this:

\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}

But, it seemed difficult to add in a box inside a box.

I want to draw similar to this: a

  • @Zarko, Actually I do not know where to add in and edit it. I start from a template from google search. But i feel I want to give up. – aan Aug 25 at 0:42
  • You can solve the problem with layers and fit (or local bounding box). You can fill the node white after it is "done". To also use the trick for the gray box, you need at least three layers. See tex.stackexchange.com/a/480685 where something very similar has been done. – Schrödinger's cat Aug 25 at 1:20
8

This is just to spell out my above comment. As I said, you need three layers, as in this post. I indicate the steps in the code, where they are in separate scopes.

  1. You arrange the nodes you want to arrange with a matrix (or chains or just positioning).
  2. You fit and fill the white nodes on a layer that is behind main, called background here.
  3. You fit and fill gray these nodes on a layer below, which is called behind here.

\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}

enter image description here

Or with your document class and the missing arrow.

\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}

enter image description here

  • thanks. How to draw arrows from second box: Sound Representation to third box: Ear? I tried: \draw[-latex] (L2.south-|mat.center) -- (mat-6-1.north-|mat.center) – aan Aug 25 at 15:04
  • 1
    @aan \draw[-latex] (mat-8-1.south-|mat.center) -- (mat-10-1.north-|mat.center);. The nodes in the matrix have the names mat-<row>-<column>. We only have one column, so all nodes have the names ` mat-<row>-1. – Schrödinger's cat Aug 25 at 15:07
  • which part is to change the box length? I want to make the box wider. – aan Aug 25 at 15:16
  • @aan It is in the keys text width=10em and in minimum width=10.3em (if you are at talking about the white boxes). – Schrödinger's cat Aug 26 at 0:51
6

Edit As starting point (extended and simplified from the first version my answer).

\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}

Code of image is quite elementary (except using `chains for "sound" nodes in the Sonification module). It need small tweak of modules height, sufficient is one level of background layer.

enter image description here

Edit (2) the MWE is now adopted to 12pt font size. Changed are widths of the sound nodes and heights of the box nodes. This changes are needed since size of nodes' shapes are hard coded and were adopted to smaller 10pt font size. In your question you inform us that you use default font size of article document class, which is 10pt.

Edit (3) in the MWE are millimeters replaced with em (in code changes are indicated by % <---). Size of this distance depends on the used font size (as suggested @ Schrödinger's cat in his comment below). Also is considered your follow-up question about centering contents of sound nodes:

\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}

result is similar as before, but this time the change of font size will not change image looks. It will be only scaled according to used font size. You an prove this with replace used font size 12pt with 11pt or 10pt`. With this image will become only proportional smaller.

enter image description here (font size 12 pt) enter image description here (font size 10pt)

  • thanks. It I change from \documentclass[tikz, margin=3mm]{standalone} to \documentclass[12pt,oneside]{book} \usepackage{tikz}. It will create the alignment different, the boxex all wrong – aan Aug 25 at 14:20
  • 1
    @aan This is because several distances are hard coded. Once you change something like the font size the boxes no longer fit. (The issue can be avoided to a large extent by using em and ex, which scale with the font size.) It can be completely avoided by using fit and three layers, in which case the frames dynamically adjust their sizes to the content. – Schrödinger's cat Aug 25 at 15:02
  • 1
    @aan, because you use bigger fonts (12 pt, in answer i supposed that you will use 10pt which is default value, when you declare \documentclass{article}, as it is in your MWE in question. I will increase bodes' shapes size accordingly asap. – Zarko Aug 25 at 15:08
  • 1
    @aan, I add version of MWE, where for lengths are used em. units (width of the letter M. Now the image design is independent from font size, however its image size is dependent from used font size. – Zarko Aug 26 at 3:06
  • 1
    @aan, yes. In definition title please add align=left and then write as you show in your comment. I will ad this to MWE in addendum asap. – Zarko Aug 26 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.