3

I am trying to make a flowchart in Latex, I want to put some of steps in a coloured box with a label, e.g. Deterministic Annealing, I added scope part but it doesn't compile with the new change.

\documentclass[useAMS,usenatbib,usegraphicx]{mn2e}
\usepackage{verbatim}
\usepackage{bm}
\usepackage{subfigure}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows}


\tikzstyle{decision} = [diamond, draw, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt, minimum height=5em]
\tikzstyle{block} = [rectangle, draw, 
    text width=5em, text centered, rounded corners, minimum height=5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=2cm,
    minimum height=2em]
\begin{document}

\begin{figure}
\flushleft
\begin{tikzpicture}[node distance = 1.45cm, auto]
\tiny
    % Place nodes
    \node [cloud] (start) {Start};
    \node [block, below of=start, node distance=1.5cm] (kde) {\tiny Apply KDE};
    \node [block, below of=kde, node distance=1.5cm] (init) {\tiny Initialize $\beta = \beta_0$\\$\hat{M}= \hat{M_0}$};
    \node [block, below of=init] (Centroid) {\tiny Update $\mu_c$, $\mu_{c'}$\\ $\sigma_c$,$\sigma_{c'}$\\ $\theta$};
    \node [block, below of=Centroid,text width=2.8cm] (Softassign) {\tiny Update $Q_{ij}$\\$M^{0}_{ij}\leftarrow \exp(\beta Q_{ij})$};
    \node [block, below of=Softassign] (slack) {\tiny Update \\$\hat{M^{1}_{ij}}$ \\ $\hat{M^{0}_{ij}}$};
    \node [decision, below of=slack,text width=1.25cm] (Sinkhorn) {\tiny Convergence\\ of $\hat M$ \\ or $I > I_1$};
    \node [decision, left of=Sinkhorn, node distance=2.8cm,text width=1.25cm] (Convergence of match matrix) {\tiny Convergence\\ of $M$ \\ or $I > I_0$};
    \node [block, below of =Sinkhorn,text width=1.0cm,yshift=-0.5cm] (End of Deterministic Annealing) {\tiny $\beta\leftarrow \beta_r\beta$};
    \node [decision, right of=End of Deterministic Annealing, node distance=2.2cm] (Deterministic Annealing) {\tiny $\beta \geq \beta_f $};
    \begin{scope}
    \node[rectangle,draw,minimum width=1cm][fit = (Centroid)(Softassign)(slack)(Sinkhorn)(Convergence of match matrix)(End of Deterministic Annealing)(Deterministic Annealing),fill=red!20] (DA) {};
    \node [above right] at (DA.north west) { Deterministic Annealing};
    \end{scope}
    \begin{scope}
    \node[rectangle,draw,minimum width=1cm][fit = (Softassign)(slack)(Sinkhorn)(Convergence of match matrix),fill=green!20] (SA) {};
    \node [above left] at (SA.north east) { Softassign};
    \end{scope}

    \node [block, right of=slack, node distance=3.2cm] (SVD) {\tiny Update\\ translations\\ via SVD};
    \node [decision, right of=init, node distance=3.2cm,text width=1.8cm] (Zero Points) {\tiny $\Delta C_{\gamma}\ll 1$ \\ and \\ $\Delta C_{\zeta}\ll 1$};
    \node [cloud, right of=Zero Points, node distance=2.0cm] (stop) {Stop};
    % Draw edges
    \path [line] (start) -- (kde);
    \path [line] (kde) -- (init);
    \path [line] (init) -- (Centroid);
    \path [line] (Centroid) -- (Softassign);
    \path [line] (Softassign) -- (slack);
    \path [line] (slack) -- (Sinkhorn);
    \path [line] (Sinkhorn) --+(1.5,0) -- +(1.5,3) -- node {No}(slack);
    \path [line] (Sinkhorn) -- node[above] {Yes} (Convergence of match matrix);
    \path [line] (Convergence of match matrix) |-  node[above][left]  {Yes}(End of Deterministic Annealing);
    \path [line] (Convergence of match matrix) |- node[above] {No}(Centroid);
    \path [line] (End of Deterministic Annealing) -- (Deterministic Annealing);
    \path [line] (Deterministic Annealing) |- node[above] {No}(Centroid);
    \path [line] (Deterministic Annealing) -| node[above][right] {Yes}(SVD);
    \path [line] (SVD) -- (Zero Points);
    \path [line] (Zero Points) -- node [above]{No}(init);
%     \path [line,dashed] (expert) -- (init);
    \path [line] (Zero Points) -- node[above]{Yes}(stop);
\end{tikzpicture}
\caption{The heuristic colour calibration procedure flowchart. The parameters in flowchart are explained in Table \ref{tab:initial} with details.}
\label{fig:flowchart}
\end{figure}

\end{document}

What I basically want to have in my flow-chart:

1. The written labels and coloured-boxes should not overlap with each other and with lines or boxes.

2. Figure out how to make the boxes appear which are missing again.

Update:

enter image description here Could somebody point out what would be my mistake?

  • Welcome to TeX.SX! Please help us help you by including a minimal working example (MWE) in your question. An MWE begins with \documentclass{...} and the required \usepackage{...} lines, and ends with \end{document}. If your code sample is immediately compilable, it is much easier for us to provide help. – eiterorm Sep 11 '14 at 13:06
  • @eiterorm Thanks for your comment. Well this part is just one of the figures in my paper and I am not very familiar which one of \usepackages are involved to make flowchart. – Dalek Sep 11 '14 at 13:12
  • You didn't happen to use these style definitions, by any chance? If you used example code from elsewhere, that should be included in your MWE. Your code sample doesn't compile in its current state. – eiterorm Sep 11 '14 at 13:38
  • @eiterorm I am sorry I made this flowchart a year ago and I am not familiar that you would reference to the format you have used here. – Dalek Sep 11 '14 at 13:43
  • All we're asking is that your code sample compiles and doesn't include too much irrelevant code. If the code sample compiles, we don't have to make assumptions about what styles and packages you have used, and if the code is kept at a minimum it will be easier for us to spot the problem. If you try and compile a new document with your code sample, you will get errors because the styles cloud, block, line and decision are undefined. Therefore the custom style definitions should be included. – eiterorm Sep 11 '14 at 13:58
3

First of all, you're missing a lot of TikZ libraries in your code,

\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows}

The reason the two big coloured rectangles cover all the other nodes, is naturally because you've removed the [on background layer] option to the scope. Without that, they are just drawn on top of the previous stuff.

In the code below I've modified slightly how things are positioned and drawn. I added the positioning library, and used its syntax (below=of othernode instead of below of=othernode). I also placed the SVD node relative to the DA node, and the Zero points node relative to init and SVD, with \node [..] at (init -| SVD) (Zero Points). This way there is less trial and error for position of those.

To make the chart narrower, you could reduce the width of the diamond shaped node in the bottom right. If placing the stop node above the Zero points node is an option, you can save a lot of horizontal space.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows,positioning}
\tikzset{decision/.style={diamond, draw, 
    text width=4.5em, text badly centered, inner sep=0pt, minimum height=5em},
block/.style={rectangle, draw, 
    text width=5em, text centered, rounded corners},
line/.style={draw, -latex'},
cloud/.style={draw, ellipse}}
\begin{document}

\begin{tikzpicture}[auto,every node/.style={font=\tiny,align=center},node distance=0.5cm]
    % Place nodes
    \node [cloud] (start) {Start};
    \node [block, below=of start] (kde) { Apply KDE};
    \node [block, below=of kde] (init) { Initialize $\beta = \beta_0$\\$\hat{M}= \hat{M_0}$};
    \node [block, below=of init] (Centroid) { Update $\mu_c$, $\mu_{c'}$\\ $\sigma_c$,$\sigma_{c'}$\\ $\theta$};
    \node [block, below=of Centroid,text width=2.8cm] (Softassign) { Update $Q_{ij}$\\$M^{0}_{ij}\leftarrow \exp(\beta Q_{ij})$};
    \node [block, below=of Softassign] (slack) { Update \\$\hat{M^{1}_{ij}}$ \\ $\hat{M^{0}_{ij}}$};
    \node [decision, below=of slack,text width=1.25cm] (Sinkhorn) { Convergence\\ of $\hat M$ \\ or $I > I_1$};
    \node [decision, left=0.5cm of Sinkhorn,text width=1.25cm] (Convergence of match matrix) { Convergence\\ of $M$ \\ or $I > I_0$};
    \node [block, below=1cm of Sinkhorn,text width=1.0cm] (End of Deterministic Annealing) { $\beta\leftarrow \beta_r\beta$};
    \node [decision, right=0.5cm of End of Deterministic Annealing, node distance=2.2cm] (Deterministic Annealing) { $\beta \geq \beta_f $};

    \begin{scope}[on background layer]
    \node[rectangle,draw,minimum width=1cm][fit = (Centroid)(Softassign)(slack)(Sinkhorn)(Convergence of match matrix)(End of Deterministic Annealing)(Deterministic Annealing)] (DA) {};
    \filldraw [fill=red!20] ([xshift=-3pt]DA.south west) rectangle (DA.north east);
    \node [above right,font=\bfseries\tiny] at (DA.north west) {Deterministic Annealing};
    \node[rectangle,draw,minimum width=1cm][fit = (Softassign)(slack)(Sinkhorn)(Convergence of match matrix),fill=green!20] (SA) {};
    \node [above right,xshift=1cm] at (SA.north west) {Softassign};
    \end{scope}

    \node [block,text width=4em, right=0.1cm of DA] (SVD) { Update\\ translations\\ via SVD};
    \node [decision, text width=1.8cm] at (init -|SVD) (Zero Points) { $\Delta C_{\gamma}\ll 1$ \\ and \\ $\Delta C_{\zeta}\ll 1$};
    \node [cloud, right=0.4cm of Zero Points] (stop) {Stop};
    % Draw edges
    \path [line] (start) -- (kde);
    \path [line] (kde) -- (init);
    \path [line] (init) -- (Centroid);
    \path [line] (Centroid) -- (Softassign);
    \path [line] (Softassign) -- (slack);
    \path [line] (slack) -- (Sinkhorn);
    \path [line] (Sinkhorn) --+(1.3,0) |-  node[above] {No}(slack);
    \path [line] (Sinkhorn) -- node[above] {Yes} (Convergence of match matrix);
    \path [line] (Convergence of match matrix) |-  node[above][left]  {Yes}(End of Deterministic Annealing);
    \path [line] (Convergence of match matrix) |- node[above] {No}(Centroid);
    \path [line] (End of Deterministic Annealing) -- (Deterministic Annealing);
    \path [line] (Deterministic Annealing) |- node[above] {No}(Centroid);
    \path [line] (Deterministic Annealing) -| node[above][right] {Yes}(SVD);
    \path [line] (SVD) -- (Zero Points);
    \path [line] (Zero Points) -- node [above]{No}(init);
%     \path [line,dashed] (expert) -- (init);
    \path [line] (Zero Points) -- node[above]{Yes}(stop);
\end{tikzpicture}
\end{document}

enter image description here

  • @TorbjotnT. I implemented your answer and I got the above result with an error message during compiling the latex file. – Dalek Sep 11 '14 at 13:58
  • the boxes inside the red-colour box are missing. – Dalek Sep 11 '14 at 14:10
  • @Dalek When you get an error, at least state which error. – Torbjørn T. Sep 11 '14 at 15:19
  • The error was about \begin{scope}[on background layer] and I removed [on background layer]. I don't have any error now. – Dalek Sep 11 '14 at 15:27
  • 1
    @Dalek For the red one, change the \filldraw to \draw, and add dash dot (or one of the other patterns defined in section 15.3.2 of TikZ' manual) instead of the colour definition, i.e. \draw [dash dot] .... For the green one, remove fill=green!20, and add dash dot dot or similar instead. – Torbjørn T. Dec 19 '14 at 9:39
3

Some how, I lost the mood of correcting your (non) MWE that is lacking lot of details. ( You guessed it right! I lost my mind ;-) Nevertheless, here is a MWE showing what you wanted to achieve:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds,positioning}
\begin{document}
\begin{tikzpicture}

\node (dm1) at (0,3) {Some};
\node[below = 2cm of dm1] (dm2)  {Another node here};
\begin{scope}[on background layer]
\node[rectangle,draw,minimum width=2cm] [fit = (dm1) (dm2),fill=green!20,label=above:Some label] (bx4) {};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

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.