GeneralFlowchart with TikZ

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
renato07
Posts: 12
Joined: Sat Feb 07, 2015 8:37 pm

Flowchart with TikZ

Post by renato07 »

Hi,
I'm trying to use thi powerfull library, to create a simple flow-chart.
I've seen this beautifull example

Code: Select all

http://www.texample.net/tikz/examples/flexible-flow-chart/
and now, I'm trying to reproduce it, with some variantion.
I would use the comfortable parameter

Code: Select all

join
to connect block. I don't know why, on this simple example:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows}
%-----------------------------da qui
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5mm}%
%------fin qui---------------------
\tikzstyle{decision}=[draw,diamond]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\begin{document}
\scriptsize\noindent
\begin{tikzpicture}[scale=0.6]
\node [elli, align=center] (dc) {Run DCOPF};
\node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
\node [block, join]] (t2) {ordinanza ingiunzione P2};
\node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
\node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
%--------------------------------
\node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
%-----------------------------
\node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
\node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
\node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
\node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
\node [block, below of=benders,node distance=4em] (master) {Solve master problem};
\node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
\node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
\node [elli, below of=convCheck,node distance=7em] (end){end};
\node [decision, left  of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
%arrows
\path [line] (dc)--(checkCong);
\path [line] (checkCong)--(Init);
\path [line] (Init)--(masterIni);
\path [line] (masterIni)--(IncNu);
\path [line] (IncNu)--(sub);
\path [line] (sub)--(phiCheck);
\path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
\path [line] (benders)--(master);
\path [line] (master)--(update);
\path [line] (update)--(convCheck);
\path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
\path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
\path [line] (maxIter)|-(IncNu);
\path [line] (phiCheck)--++  (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
\end{tikzpicture}
%
\begin{tikzpicture}
\usetikzlibrary{shapes,arrows,shadows}
\tikzstyle{decision}=[diamond,draw]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\scriptsize
\node [block, align=center] (masterm) {Solve master problem} ;
\node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
\node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
\node [elli, below of=check, align=center, node distance=6em] (end) {End};
\node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
%arrows
\path [line] (masterm)--(subm);
\path [line] (subm)--(check);
\path [line] (check)-- node [near start, xshift=1em] {No} (end);
\path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
\path [line] (cut) |- (masterm);
\end{tikzpicture}
\end{document}
it does not works.

Do I forget some package? Or what else? :?

TIA

Renato

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Flowchart with TikZ

Post by Stefan Kottwitz »

Hi Renato!

That example simplifies flowchart because it uses the chains library to add flowchart nodes to a chain with a simple syntax, such as by join. So you would need

Code: Select all

\usetikzlibrary{chains}
plus some chains syntax as in the example (start chain=going below, every join/.style={norm}, ...)

Easy to maintain but not simple to learn, it needs some reading in the TikZ manual about chains. Can be 10 minutes or more, depends on how much one already knows.

Here's another nice example - the only complexity is that I used \foreach loops to repeat connections: http://latex-cookbook.net/cookbook/examples/math-flowchart/

Stefan
LaTeX.org admin
renato07
Posts: 12
Joined: Sat Feb 07, 2015 8:37 pm

Flowchart with TikZ

Post by renato07 »

Hi Stefan,
I read your answer, but had no time to answer you :(
I've tried the example that you sudgest to me, added a single raw (with the join parameter) but I receive an error. Maybe the prob could reside in the chain syntax

Code: Select all

\documentclass[x11names]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,chains}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5mm}%
%%%>
\tikzstyle{decision}=[draw,diamond]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\begin{document}
\scriptsize\noindent
\begin{tikzpicture}[scale=0.6]
\node [elli, align=center] (dc) {Run DCOPF};
\node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
\node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
\node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
%--------------------------------
\node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
%-----------------------------
\node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
\node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
\node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
\node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
\node [block, below of=benders,node distance=4em] (master) {Solve master problem};
\node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
\node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
\node [elli, below of=convCheck,node distance=7em] (end){end};
\node [decision, left  of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
%arrows
\path [line] (dc)--(checkCong);
\path [line] (checkCong)--(Init);
\path [line] (Init)--(masterIni);
\path [line] (masterIni)--(IncNu);
\path [line] (IncNu)--(sub);
\path [line] (sub)--(phiCheck);
\path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
\path [line] (benders)--(master);
\path [line] (master)--(update);
\path [line] (update)--(convCheck);
\path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
\path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
\path [line] (maxIter)|-(IncNu);
\path [line] (phiCheck)--++  (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
\end{tikzpicture}
%
\begin{tikzpicture}
\usetikzlibrary{shapes,arrows,shadows}
\tikzstyle{decision}=[diamond,draw]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\scriptsize
\node [block, align=center] (masterm) {Solve master problem};
\node [block, join] (t2) {ordinanza ingiunzione P2};
\node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
\node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
\node [elli, below of=check, align=center, node distance=6em] (end) {End};
\node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
%arrows
\path [line] (masterm)--(subm);
\path [line] (subm)--(check);
\path [line] (check)-- node [near start, xshift=1em] {No} (end);
\path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
\path [line] (cut) |- (masterm);
\end{tikzpicture}
\end{document}
this line:

Code: Select all

\node [block, join] (t2) {ordinanza ingiunzione P2};
give me error.

I would use the join parameter, because it can be used on the 90% of my flow (without any other \path...

after that, i need to create a java code that build the flow.

Can you help me?

Renato
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Flowchart with TikZ

Post by Stefan Kottwitz »

You will get this error message:

Code: Select all

! Undefined control sequence.
<argument> \tikzchainprevious 
                              
l.59 ...ck, join] (t2) {ordinanza ingiunzione P2};
This means, there's no previous node on the chain of nodes.
  • You need to start a chain in line 51:

    Code: Select all

    \begin{tikzpicture}[start chain=going below]
  • In line 58 you can put the first node on the chain:

    Code: Select all

    \node [block, align=center, on chain] (masterm) {Solve master problem};
Stefan
LaTeX.org admin
renato07
Posts: 12
Joined: Sat Feb 07, 2015 8:37 pm

Flowchart with TikZ

Post by renato07 »

Hi,
I am upset :?
I can't find the prob on this code

Code: Select all

\documentclass[x11names]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,chains}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5mm}%
%%%>
\tikzstyle{decision}=[draw,diamond]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\begin{document}
\scriptsize\noindent
\begin{tikzpicture}[scale=0.6]
\node [elli, align=center] (dc) {Run DCOPF};
\node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
\node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
\node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
%--------------------------------
\node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
%-----------------------------
\node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
\node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
\node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
\node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
\node [block, below of=benders,node distance=4em] (master) {Solve master problem};
\node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
\node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
\node [elli, below of=convCheck,node distance=7em] (end){end};
\node [decision, left  of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
%arrows
\path [line] (dc)--(checkCong);
\path [line] (checkCong)--(Init);
\path [line] (Init)--(masterIni);
\path [line] (masterIni)--(IncNu);
\path [line] (IncNu)--(sub);
\path [line] (sub)--(phiCheck);
\path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
\path [line] (benders)--(master);
\path [line] (master)--(update);
\path [line] (update)--(convCheck);
\path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
\path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
\path [line] (maxIter)|-(IncNu);
\path [line] (phiCheck)--++  (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
\end{tikzpicture}
%
\begin{tikzpicture}[start chain=going below]
\begin{tikzpicture}
\usetikzlibrary{shapes,arrows,shadows}
\tikzstyle{decision}=[diamond,draw]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
%\scriptsize 7
\node [block, align=center, on chain] (masterm) {Solve master problem};
%\node [block, align=center] (masterm) {Solve master problem};
\node [block, join] (t2) {ordinanza ingiunzione P2};
\node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
\node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
\node [elli, below of=check, align=center, node distance=6em] (end) {End};
\node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
%arrows 
\path [line] (masterm)--(subm);
\path [line] (subm)--(check);
\path [line] (check)-- node [near start, xshift=1em] {No} (end);
\path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
\path [line] (cut) |- (masterm);
\end{tikzpicture}
\end{document}
I receive the error:

Code: Select all

Missing \endgroup inserted. <inserted text> \endgroup l.73 \end{document}
which group is this? :(

I'm working with overleaf.com. Are you working on a standalone compiler?

Renato
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Flowchart with TikZ

Post by Stefan Kottwitz »

Hi Renato,

look here:

Code: Select all

\begin{tikzpicture}[start chain=going below]
\begin{tikzpicture}
You copied my suggestion into it, instead of modifying, so the picture was started twice. Simply change there to

Code: Select all

\begin{tikzpicture}[start chain=going below]
Stefan
LaTeX.org admin
renato07
Posts: 12
Joined: Sat Feb 07, 2015 8:37 pm

Flowchart with TikZ

Post by renato07 »

ok,
I've simplified the code:

Code: Select all

\documentclass[x11names]{article}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5mm}%
%%%>
\usetikzlibrary{shapes,arrows,shadows,chains}
\tikzstyle{decision}=[diamond,draw]
\tikzstyle{line}=[draw,-latex']
\tikzstyle{elli}=[draw,ellipse]
\tikzstyle{block}=[draw,rectangle]
\begin{document}
%---------------------INIZIO IMMAGINE------------
\begin{tikzpicture}[%
    >=triangle 60,              % Nice arrows;variando i simboli > e =, cambia la forma della freccia
    start chain=going below,    % General flow is top-to-bottom unisce i blocchi andando da sopra a sotto
    node distance=6mm and 60mm, % Global setup of box spacing 6mm verticale 60 orizzontale
]
\scriptsize 8
\node [block, align=center, on chain] (masterm) {Solve master problem};
\node [block, join] (t2) {ordinanza ingiunzione P2};
\node [block, join] (subm) {Solve sub problem};
\node [decision, join] (check) {Is there\\ infeasibility?};
\node [elli, join] (end) {End};
\node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
%arrows 
%\path [line] (masterm)--(subm);
%\path [line] (subm)--(check);
\path [line] (check)-- node [near start, xshift=1em] {No} (end);
\path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
\path [line] (cut) |- (masterm);
\end{tikzpicture}
\end{document}
and now it compile, but all the form are drawn at the same point, I thought, that this code:

Code: Select all

\begin{tikzpicture}[%
    >=triangle 60,              % Nice arrows;
    start chain=going below,    % General flow is top-to-bottom
    node distance=6mm and 60mm, % Global setup of box spacing 6mm ]
let the chain go from top to bottom

Code: Select all

start chain=going below,
and the node distance was set

Code: Select all

node distance=6mm and 60mm,
why do alla the node are designed at the same place?

Renato
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Flowchart with TikZ

Post by Stefan Kottwitz »

I see. Actually, every node should be on the grid. That's what the original example had: a base style (with on grid) which is inherited by all node styles.

So, I changed your \tikzstyle commands to such styles (with the newer syntax):

Code: Select all

\tikzset{
  base/.style     = { draw, on chain, on grid, align=center},
  decision/.style = { base, diamond },
  elli/.style     = { base, ellipse },
  block/.style    = { base, rectangle },
  line/.style     = { -latex' },
}
This way it should work with the placement immediately.

Complete code:

Code: Select all

\documentclass[x11names]{article}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5mm}%
%%%>
\usetikzlibrary{shapes,arrows,shadows,chains}
\tikzset{
  base/.style     = { draw, on chain, on grid, align=center},
  decision/.style = { base, diamond },
  elli/.style     = { base, ellipse },
  block/.style    = { base, rectangle },
  line/.style     = { -latex' },
}
\begin{document}
%---------------------INIZIO IMMAGINE------------
\begin{tikzpicture}[%
    >=triangle 60,              % Nice arrows;variando i simboli > e =, cambia la forma della freccia
    start chain=going below,    % General flow is top-to-bottom unisce i blocchi andando da sopra a sotto
    node distance=6mm and 60mm, % Global setup of box spacing 6mm verticale 60 orizzontale
]
\scriptsize 8
\node [block, align=center, on chain] (masterm) {Solve master problem};
\node [block, join] (t2) {ordinanza ingiunzione P2};
\node [block, join] (subm) {Solve sub problem};
\node [decision, join] (check) {Is there\\ infeasibility?};
\node [elli, join] (end) {End};
\node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
%arrows
%\path [line] (masterm)--(subm);
%\path [line] (subm)--(check);
\path [line] (check)-- node [near start, xshift=1em] {No} (end);
\path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
\path [line] (cut) |- (masterm);
\end{tikzpicture}
\end{document}
flowchart.png
flowchart.png (15.71 KiB) Viewed 9729 times
Stefan
LaTeX.org admin
renato07
Posts: 12
Joined: Sat Feb 07, 2015 8:37 pm

Re: tikz problem

Post by renato07 »

yes Stefan, it works! In a great way! I think to be on the right way for my work.

Really thank you!

Renato

ps
i STILL do not close the thread :roll:
Post Reply