LaTeX forum ⇒ GeneralFlowchart with TikZ

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

Flowchart with TikZ

Postby renato07 » Mon Nov 30, 2015 11:15 pm

Hi,
I'm trying to use thi powerfull library, to create a simple flow-chart.
I've seen this beautifull example
  1. 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
  1. join
to connect block. I don't know why, on this simple example:
  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{shapes,arrows,shadows}
  4. %-----------------------------da qui
  5. \usepackage{verbatim}
  6. \usepackage[active,tightpage]{preview}
  7. \PreviewEnvironment{tikzpicture}
  8. \setlength\PreviewBorder{5mm}%
  9. %------fin qui---------------------
  10. \tikzstyle{decision}=[draw,diamond]
  11. \tikzstyle{line}=[draw,-latex']
  12. \tikzstyle{elli}=[draw,ellipse]
  13. \tikzstyle{block}=[draw,rectangle]
  14. \begin{document}
  15. \scriptsize\noindent
  16. \begin{tikzpicture}[scale=0.6]
  17. \node [elli, align=center] (dc) {Run DCOPF};
  18. \node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
  19. \node [block, join]] (t2) {ordinanza ingiunzione P2};
  20. \node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
  21. \node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
  22. %--------------------------------
  23. \node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
  24. %-----------------------------
  25. \node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
  26. \node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
  27. \node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
  28. \node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
  29. \node [block, below of=benders,node distance=4em] (master) {Solve master problem};
  30. \node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
  31. \node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
  32. \node [elli, below of=convCheck,node distance=7em] (end){end};
  33. \node [decision, left of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
  34. %arrows
  35. \path [line] (dc)--(checkCong);
  36. \path [line] (checkCong)--(Init);
  37. \path [line] (Init)--(masterIni);
  38. \path [line] (masterIni)--(IncNu);
  39. \path [line] (IncNu)--(sub);
  40. \path [line] (sub)--(phiCheck);
  41. \path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
  42. \path [line] (benders)--(master);
  43. \path [line] (master)--(update);
  44. \path [line] (update)--(convCheck);
  45. \path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
  46. \path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
  47. \path [line] (maxIter)|-(IncNu);
  48. \path [line] (phiCheck)--++ (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
  49. \end{tikzpicture}
  50. %
  51. \begin{tikzpicture}
  52. \usetikzlibrary{shapes,arrows,shadows}
  53. \tikzstyle{decision}=[diamond,draw]
  54. \tikzstyle{line}=[draw,-latex']
  55. \tikzstyle{elli}=[draw,ellipse]
  56. \tikzstyle{block}=[draw,rectangle]
  57. \scriptsize
  58. \node [block, align=center] (masterm) {Solve master problem} ;
  59. \node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
  60. \node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
  61. \node [elli, below of=check, align=center, node distance=6em] (end) {End};
  62. \node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
  63. %arrows
  64. \path [line] (masterm)--(subm);
  65. \path [line] (subm)--(check);
  66. \path [line] (check)-- node [near start, xshift=1em] {No} (end);
  67. \path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
  68. \path [line] (cut) |- (masterm);
  69. \end{tikzpicture}
  70. \end{document}

it does not works.

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

TIA

Renato

Tags:

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

Postby Stefan Kottwitz » Tue Dec 01, 2015 1:20 am

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
  1. \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
Site admin

renato07
Posts: 7
Joined: Sat Feb 07, 2015 8:37 pm

Postby renato07 » Fri Dec 11, 2015 11:11 am

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
  1. \documentclass[x11names]{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{shapes,arrows,chains}
  4. %%%<
  5. \usepackage{verbatim}
  6. \usepackage[active,tightpage]{preview}
  7. \usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
  8. \PreviewEnvironment{tikzpicture}
  9. \setlength\PreviewBorder{5mm}%
  10. %%%>
  11. \tikzstyle{decision}=[draw,diamond]
  12. \tikzstyle{line}=[draw,-latex']
  13. \tikzstyle{elli}=[draw,ellipse]
  14. \tikzstyle{block}=[draw,rectangle]
  15. \begin{document}
  16. \scriptsize\noindent
  17. \begin{tikzpicture}[scale=0.6]
  18. \node [elli, align=center] (dc) {Run DCOPF};
  19. \node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
  20. \node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
  21. \node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
  22. %--------------------------------
  23. \node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
  24. %-----------------------------
  25. \node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
  26. \node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
  27. \node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
  28. \node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
  29. \node [block, below of=benders,node distance=4em] (master) {Solve master problem};
  30. \node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
  31. \node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
  32. \node [elli, below of=convCheck,node distance=7em] (end){end};
  33. \node [decision, left of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
  34. %arrows
  35. \path [line] (dc)--(checkCong);
  36. \path [line] (checkCong)--(Init);
  37. \path [line] (Init)--(masterIni);
  38. \path [line] (masterIni)--(IncNu);
  39. \path [line] (IncNu)--(sub);
  40. \path [line] (sub)--(phiCheck);
  41. \path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
  42. \path [line] (benders)--(master);
  43. \path [line] (master)--(update);
  44. \path [line] (update)--(convCheck);
  45. \path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
  46. \path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
  47. \path [line] (maxIter)|-(IncNu);
  48. \path [line] (phiCheck)--++ (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
  49. \end{tikzpicture}
  50. %
  51. \begin{tikzpicture}
  52. \usetikzlibrary{shapes,arrows,shadows}
  53. \tikzstyle{decision}=[diamond,draw]
  54. \tikzstyle{line}=[draw,-latex']
  55. \tikzstyle{elli}=[draw,ellipse]
  56. \tikzstyle{block}=[draw,rectangle]
  57. \scriptsize
  58. \node [block, align=center] (masterm) {Solve master problem};
  59. \node [block, join] (t2) {ordinanza ingiunzione P2};
  60. \node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
  61. \node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
  62. \node [elli, below of=check, align=center, node distance=6em] (end) {End};
  63. \node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
  64. %arrows
  65. \path [line] (masterm)--(subm);
  66. \path [line] (subm)--(check);
  67. \path [line] (check)-- node [near start, xshift=1em] {No} (end);
  68. \path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
  69. \path [line] (cut) |- (masterm);
  70. \end{tikzpicture}
  71. \end{document}


this line:
  1. \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: 9504
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Dec 12, 2015 2:30 am

You will get this error message:

  1. ! Undefined control sequence.
  2. <argument> \tikzchainprevious
  3.  
  4. l.59 ...ck, join] (t2) {ordinanza ingiunzione P2};


This means, there's no previous node on the chain of nodes.


Stefan
Site admin

renato07
Posts: 7
Joined: Sat Feb 07, 2015 8:37 pm

Postby renato07 » Sat Dec 12, 2015 4:45 pm

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

  1. \documentclass[x11names]{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{shapes,arrows,chains}
  4. %%%<
  5. \usepackage{verbatim}
  6. \usepackage[active,tightpage]{preview}
  7. \usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
  8. \PreviewEnvironment{tikzpicture}
  9. \setlength\PreviewBorder{5mm}%
  10. %%%>
  11. \tikzstyle{decision}=[draw,diamond]
  12. \tikzstyle{line}=[draw,-latex']
  13. \tikzstyle{elli}=[draw,ellipse]
  14. \tikzstyle{block}=[draw,rectangle]
  15. \begin{document}
  16. \scriptsize\noindent
  17. \begin{tikzpicture}[scale=0.6]
  18. \node [elli, align=center] (dc) {Run DCOPF};
  19. \node [decision, below of=dc, align=center, text width=4em,node distance=7em] (checkCong) {Is there any\\ congested\\ line?};
  20. \node [block, below of=checkCong, align=center,node distance=6.5em] (Init) {$\nu=0$\\$\theta^0=\theta^{DCOPF}$};
  21. \node [block, below of=Init, align=center,node distance=4em] (masterIni) {Run master problem\\ without considering benders cut\\ to initialize $\lambda,\,p_g,\,u$};
  22. %--------------------------------
  23. \node [block, right of=Init,node distance=9.5em] (pippo){io sono pippo};
  24. %-----------------------------
  25. \node [block, below of=masterIni, align=center,node distance=4em] (IncNu) {$\nu=\nu+1$};
  26. \node [block, below of=IncNu,node distance=4em] (sub) {Solve subproblem};
  27. \node [decision, below of=sub,node distance=5em, align=center] (phiCheck) {Is\\ $\phi>0$?};
  28. \node [block, below of=phiCheck, align=center,node distance=7em] (benders) {Generate infeasibilty cuts\\ and forward it\\ to master problem};
  29. \node [block, below of=benders,node distance=4em] (master) {Solve master problem};
  30. \node [block, below of=master,node distance=3em] (update) {Update $\lambda^{\nu},\,p_g^{\nu},\,u^{\nu},\,\theta^{\nu}$};
  31. \node [decision, below of=update, align=center,node distance=6em, minimum height=5mm] (convCheck) {Convergence \\ check};
  32. \node [elli, below of=convCheck,node distance=7em] (end){end};
  33. \node [decision, left of=end,xshift=-10em,yshift=20em,align=center] (maxIter) {Is\\ $\nu>\nu^{max}$?};
  34. %arrows
  35. \path [line] (dc)--(checkCong);
  36. \path [line] (checkCong)--(Init);
  37. \path [line] (Init)--(masterIni);
  38. \path [line] (masterIni)--(IncNu);
  39. \path [line] (IncNu)--(sub);
  40. \path [line] (sub)--(phiCheck);
  41. \path [line] (phiCheck) -- node [near start, xshift=1em] {Yes} (benders);
  42. \path [line] (benders)--(master);
  43. \path [line] (master)--(update);
  44. \path [line] (update)--(convCheck);
  45. \path [line] (convCheck)--node [near start, xshift=1em] {Yes} (end);
  46. \path [line] (convCheck)-|node [near start, yshift=0.5em] {No}(maxIter);
  47. \path [line] (maxIter)|-(IncNu);
  48. \path [line] (phiCheck)--++ (3,0) node [near start, yshift=0.5em] {No} |- (convCheck);
  49. \end{tikzpicture}
  50. %
  51. \begin{tikzpicture}[start chain=going below]
  52. \begin{tikzpicture}
  53. \usetikzlibrary{shapes,arrows,shadows}
  54. \tikzstyle{decision}=[diamond,draw]
  55. \tikzstyle{line}=[draw,-latex']
  56. \tikzstyle{elli}=[draw,ellipse]
  57. \tikzstyle{block}=[draw,rectangle]
  58. %\scriptsize 7
  59. \node [block, align=center, on chain] (masterm) {Solve master problem};
  60. %\node [block, align=center] (masterm) {Solve master problem};
  61. \node [block, join] (t2) {ordinanza ingiunzione P2};
  62. \node [block, below of=masterm, align=center, node distance=3em] (subm) {Solve sub problem};
  63. \node [decision, below of=subm, align=center, node distance=6em] (check) {Is there\\ infeasibility?};
  64. \node [elli, below of=check, align=center, node distance=6em] (end) {End};
  65. \node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
  66. %arrows
  67. \path [line] (masterm)--(subm);
  68. \path [line] (subm)--(check);
  69. \path [line] (check)-- node [near start, xshift=1em] {No} (end);
  70. \path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
  71. \path [line] (cut) |- (masterm);
  72. \end{tikzpicture}
  73. \end{document}


I receive the error:
  1. 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: 9504
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Dec 12, 2015 5:02 pm

Hi Renato,

look here:

  1. \begin{tikzpicture}[start chain=going below]
  2. \begin{tikzpicture}


You copied my suggestion into it, instead of modifying, so the picture was started twice. Simply change there to

  1. \begin{tikzpicture}[start chain=going below]


Stefan
Site admin

renato07
Posts: 7
Joined: Sat Feb 07, 2015 8:37 pm

Postby renato07 » Sat Dec 12, 2015 6:26 pm

ok,
I've simplified the code:
  1. \documentclass[x11names]{article}
  2. \usepackage{tikz}
  3. \usepackage{verbatim}
  4. \usepackage[active,tightpage]{preview}
  5. \usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
  6. \PreviewEnvironment{tikzpicture}
  7. \setlength\PreviewBorder{5mm}%
  8. %%%>
  9. \usetikzlibrary{shapes,arrows,shadows,chains}
  10. \tikzstyle{decision}=[diamond,draw]
  11. \tikzstyle{line}=[draw,-latex']
  12. \tikzstyle{elli}=[draw,ellipse]
  13. \tikzstyle{block}=[draw,rectangle]
  14. \begin{document}
  15. %---------------------INIZIO IMMAGINE------------
  16. \begin{tikzpicture}[%
  17. >=triangle 60, % Nice arrows;variando i simboli > e =, cambia la forma della freccia
  18. start chain=going below, % General flow is top-to-bottom unisce i blocchi andando da sopra a sotto
  19. node distance=6mm and 60mm, % Global setup of box spacing 6mm verticale 60 orizzontale
  20. ]
  21. \scriptsize 8
  22. \node [block, align=center, on chain] (masterm) {Solve master problem};
  23. \node [block, join] (t2) {ordinanza ingiunzione P2};
  24. \node [block, join] (subm) {Solve sub problem};
  25. \node [decision, join] (check) {Is there\\ infeasibility?};
  26. \node [elli, join] (end) {End};
  27. \node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
  28. %arrows
  29. %\path [line] (masterm)--(subm);
  30. %\path [line] (subm)--(check);
  31. \path [line] (check)-- node [near start, xshift=1em] {No} (end);
  32. \path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
  33. \path [line] (cut) |- (masterm);
  34. \end{tikzpicture}
  35. \end{document}

and now it compile, but all the form are drawn at the same point, I thought, that this code:
  1. \begin{tikzpicture}[%
  2. >=triangle 60, % Nice arrows;
  3. start chain=going below, % General flow is top-to-bottom
  4. node distance=6mm and 60mm, % Global setup of box spacing 6mm ]


let the chain go from top to bottom
  1. start chain=going below,


and the node distance was set
  1. node distance=6mm and 60mm,


why do alla the node are designed at the same place?

Renato

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

Postby Stefan Kottwitz » Sat Dec 12, 2015 7:21 pm

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):

  1. \tikzset{
  2. base/.style = { draw, on chain, on grid, align=center},
  3. decision/.style = { base, diamond },
  4. elli/.style = { base, ellipse },
  5. block/.style = { base, rectangle },
  6. line/.style = { -latex' },
  7. }


This way it should work with the placement immediately.

Complete code:

  1. \documentclass[x11names]{article}
  2. \usepackage{tikz}
  3. \usepackage{verbatim}
  4. \usepackage[active,tightpage]{preview}
  5. \usepackage[latin1]{inputenc} %----permette l' uso dei caratteri accentati
  6. \PreviewEnvironment{tikzpicture}
  7. \setlength\PreviewBorder{5mm}%
  8. %%%>
  9. \usetikzlibrary{shapes,arrows,shadows,chains}
  10. \tikzset{
  11. base/.style = { draw, on chain, on grid, align=center},
  12. decision/.style = { base, diamond },
  13. elli/.style = { base, ellipse },
  14. block/.style = { base, rectangle },
  15. line/.style = { -latex' },
  16. }
  17. \begin{document}
  18. %---------------------INIZIO IMMAGINE------------
  19. \begin{tikzpicture}[%
  20. >=triangle 60, % Nice arrows;variando i simboli > e =, cambia la forma della freccia
  21. start chain=going below, % General flow is top-to-bottom unisce i blocchi andando da sopra a sotto
  22. node distance=6mm and 60mm, % Global setup of box spacing 6mm verticale 60 orizzontale
  23. ]
  24. \scriptsize 8
  25. \node [block, align=center, on chain] (masterm) {Solve master problem};
  26. \node [block, join] (t2) {ordinanza ingiunzione P2};
  27. \node [block, join] (subm) {Solve sub problem};
  28. \node [decision, join] (check) {Is there\\ infeasibility?};
  29. \node [elli, join] (end) {End};
  30. \node [block, left of=end, xshift=-8em, yshift=10em, align=center] (cut) {Generate infeasibility cut};
  31. %arrows
  32. %\path [line] (masterm)--(subm);
  33. %\path [line] (subm)--(check);
  34. \path [line] (check)-- node [near start, xshift=1em] {No} (end);
  35. \path [line] (check) -| node [near start, yshift=0.5em] {Yes} (cut);
  36. \path [line] (cut) |- (masterm);
  37. \end{tikzpicture}
  38. \end{document}


flowchart.png
flowchart.png (15.71 KiB) Viewed 5134 times


Stefan
Site admin

renato07
Posts: 7
Joined: Sat Feb 07, 2015 8:37 pm

Postby renato07 » Sat Dec 12, 2015 10:52 pm

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:


Return to “General”

Who is online

Users browsing this forum: No registered users and 9 guests