3

I'm trying to make a graph with TikZ and I've made all nodes: decision = diamond, block = rectangle, etc.

However, when I want to draw conditions arrows of decision part my arrow passes through rectangles.

I need to obtain this result :

enter image description here

code :

\documentclass[a4paper,12pt]{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{varwidth}

\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}


% Define block styles
\tikzstyle{decision} = [
    diamond, draw, 
    fill=gray!20, 
    text width=6.5em, 
    text badly centered, 
    node distance=3cm, 
    minimum height=1em,
    inner sep=0pt]
\tikzstyle{block} = [
    rectangle, draw, 
    fill=white!20, 
    text width=12em, 
    text centered, 
    rounded corners, 
    minimum height=1em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [
    draw, ellipse,
    fill=red!20, 
    node distance=5cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 1.5cm, auto]
    % Place nodes
    \node [block] (init) {Load inputs};
    \node [block, below of=init, node distance=1.0cm] (tag2) {\begin{varwidth}{15em}text text text text text text\end{varwidth}}; 
    \node [block, below of=tag2, node distance=1.2cm, minimum width=9cm] (tag3)  {\begin{varwidth}{19em}text text text text text text text text text text text\end{varwidth}}; 
    \node [block, below of=tag3, node distance=1.5cm] (tag4) {text text text text text text};
    \node [block, below of=tag4, node distance=1.5cm] (tag5) {text text text text text text};    
    \node [decision, below of=tag5, node distance=2.7cm] (tag6) {text text text text text text};
    \node [block, below of=tag6, node distance=2.7cm] (tag7) {text text text text text text};    
    \node [decision, below of=tag7, node distance=2.7cm] (tag8) {text text text text text text};    
    \node [block, below of=tag8, node distance=2.7cm] (tag9) {text text text text text text};
    \node [block, below of=tag9, node distance=1.5cm] (tag10) {text text text text text text};
    \node [decision, below of=tag10, node distance=2.9cm] (tag12a) {text text text text text text};    
    \node [block, below of=tag12a, node distance=2.5cm] (stop) {show results};

    % Draw edges
    \path [line] (init) -- (tag2);
    \path [line] (tag2) -- (tag3);
    \path [line] (tag3) -- (tag4);
    \path [line] (tag4) -- (tag5);    
    \path [line] (tag5) -- (tag6);
    \path [line] (tag6) -- (tag7);
    \path [line] (tag7) -- (tag8);
    \path [line] (tag8) -- (tag9);
    \path [line] (tag9) -- (tag10);    
    \path [line] (tag10) -- (tag12a);    
    \path [line] (tag12a) -- (stop);
%    \path [line] (tag6a) -| node [near start] {no} (stop);
 %   \path [line,dashed] (tag6a) |-  (stop);

\end{tikzpicture}


\end{document}
azdoud
  • 371

2 Answers2

5

maybe you liked:

enter image description here

code is reorganized with goal to be more consistent and somehow concise:

\documentclass[a4paper,12pt]{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{varwidth}% not used

\usetikzlibrary{arrows.meta,
                calc, chains, % added
                positioning,  % added
                shapes}
\makeatletter
\tikzset{FlowChart/.style={% enable to be used also at other flowcharts
suspend join/.code={\def\tikz@after@path{}},
startstop/.style = {rectangle, rounded corners, draw, fill=red!30,
                    minimum width=3cm, minimum height=1cm,
                    on chain, join=by line},
    block/.style = {rectangle, draw, %fill=blue!30,
                    text width=5cm, minimum height=1cm, align=center,
                    on chain, join=by line},
 decision/.style = {diamond, aspect=1.3, draw, fill=gray!30,
                    text width=3cm, minimum height=1cm, align=center,
                    on chain, join=by line},
     line/.style = {thick,-Triangle}
        }   }
\makeatother

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2em}

\begin{document}
\pagestyle{empty}
    \begin{tikzpicture}[FlowChart, auto,
node distance = 8mm and 6mm,
  start chain = going below
                        ]
% Place nodes
\node (init) [startstop]{Load inputs};
\node (tag2) [block]    {text text text text text text};
\node (tag3) [block]    {text text text text text text
                         text text text text text};
\node (tag4) [block]    {text text text text text text};
\node (tag4a) [block]   {text text text text text text};
\node (tag6a) [decision]{text text text text text text};
\node (tag8a) [block]   {text text text text text text};
\node (tag6b) [decision]{text text text text text text};
\node (tag7) [block,
              suspend join,
              below right=of tag6b.east]
                        {text text text text text text};
\node (tag12) [block]   {text text text text text text};
\node (tag12a) [decision]   {text text text text text text};
    % Draw edges
\draw[line] (tag6a.east) node[above right] {NON}
                         -- ++ ( 1.5,0) |-   ($(tag4a.south)!0.5!(tag6a.north)$);
\draw[line] (tag6b.east) node[above right] {NON} -| (tag7);
\draw[line] (tag6b.west) node[above left] {OUI}
                         -- ++ (-2,0) |-   ($(tag2.south)!0.5!(tag3.north)$);
\draw[line] (tag12a.east) node[above right] {NON}
                         -- ++ ( 1.5,0) |-   ($(tag8a.south)!0.5!(tag6b.north)$);
\draw[line] (tag12a.south) node[below right] {OUI}
                         -- ++ ( 0,-1) -| ($(tag12a.east)+(2,0)$)
                         |- ($(tag6a.south)!0.5!(tag8a.north)$);
\end{tikzpicture}
\end{document}
Zarko
  • 296,517
  • @azdoud, you are welcome! when you will have some spare time, compare your code, code of accepted answer and mine code :-). let me know, if you need some explanation about mine code (to me it is self explanatory ;-) ) – Zarko Apr 01 '18 at 13:06
  • I wonder if using the intersections library could allow to avoid placing the construction nodes by hand. Basically if an intersection is detected between a node and a path, some option like shift path 1cm right of nodename would create a node 1cm right of the crossed node to build the path. That could be a nice feature (if it doesn't exist at the moment). What do you think about this @christian-feuersänger – BambOo Apr 01 '18 at 15:15
  • @BambOo, sorry, your comment is quite cryptic to me. how is related to my answer? :-) – Zarko Apr 01 '18 at 15:55
  • Sorry @Zarko, we both used additional nodes but I thought of this when reading your answer ;). You used --++(1.5,0) etc to add some nodes manually, and I was simply wondering if creating some additional tikz library could be relevant to make it automatic. +1 by the way for ($(tag4a.south)!0.5!(tag6a.north)$) I did not know this feature. – BambOo Apr 01 '18 at 16:08
  • @BambOo, now is more clear. similar to what you looking for offer tikzlibrarypaths.ortho.tex in companion with tikzlibrarypaths.ortho.code.tex, which unfortunately is not part of tikz library bundle. this limited me in their use. however even with use of this library for path with three corners you need one auxiliary coordinate between start and end of path (one less as is used now). – Zarko Apr 01 '18 at 16:26
4

With this, you have what you need. Please pay attention when using the positioning library. If I am not mistaken, the below of= is deprecated and below= of should be used instead, see here.

The issue is that in such situations, you need to guide the arrows with some more nodes. In particular, I added nodes in the middle of some of the arrows to serve as anchors for the following ones with \path(a) -- node[pos=0.5,outer sep=0pt,inner sep=0pt](midab){}(b); calls as well as nodes near the beginning of the path for the OUI / NON labels with node[pos=0.2]{OUI}.

I cleaned your MWE a little bit, but the result is very close to your image.

\documentclass[a4paper,12pt]{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{varwidth}

\usetikzlibrary{shapes,arrows,positioning,calc}
\begin{document}
\pagestyle{empty}


% Define block styles
\tikzstyle{decision} = [
    diamond, draw,
    fill=gray!20,
    text width=6.5em,
    text badly centered,
    node distance=3cm,
    minimum height=1em,
    inner sep=0pt]
\tikzstyle{block} = [
    rectangle, draw,
    fill=white!20,
    text width=12em,
    text centered,
    rounded corners,
    minimum height=1em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [
    draw, ellipse,
    fill=red!20,
    node distance=5cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 0.5cm, auto]
    % Place nodes
    \node [cloud] (init) {Load inputs};
    \node [block, below= of init] (tag2) {tag2};
    \node [block, below= of tag2] (tag3)  {tag3};
    \node [block, below= of tag3] (tag4) {tag4};
    %\node [block, below= of tag4] (tag4a) {text text text text text text};
    \node [decision, below= of tag4] (tag6a) {tag6a};
    \node [block, below= of tag6a] (tag8a) {tag8a};
    \node [decision, below= of tag8a] (tag6b) {tag6b};
    \node [block, below right= 1cm of tag6b] (tag7) {tag7};
    \node [decision, below= of tag7] (tag12a) {tag12a};

    % Draw edges
    \path [line] (init) -- (tag2);
    \path [line] (tag2) -- node[pos=0.5,outer sep=0pt,inner sep=0pt](mid23){}(tag3);
    \path [line] (tag3) -- (tag4);
  %  \path [line] (tag4) -- (tag4a);
    \path [line] (tag4) -- node[pos=0.5,outer sep=0pt,inner sep=0pt](mid46a){}(tag6a);
    \path [line] (tag6a.east) --node[pos=0.2]{NON} ($(tag6a.east)+(2,0cm)$)|-(mid46a);
    \path [line] (tag6a) --node[pos=0.2]{OUI}
                           node[pos=0.5,outer sep=0pt,inner sep=0pt](mid6a8a){}(tag8a);
    \path [line] (tag8a) -- node[pos=0.5,outer sep=0pt,inner sep=0pt](mid8a6b){}(tag6b);
    \path [line] (tag6b.south)--node[pos=0.2]{OUI}($(tag6b.south)-(0,0.5)$) -|($(tag6b.west)-(2,0)$)|-(mid23);
    \path [line] (tag6b.east) -|node[pos=0.2]{NON}(tag7.north);
    \path [line] (tag7) -- (tag12a);
    \path [line] (tag12a.south)--node[pos=0.2]{OUI}($(tag12a.south)-(0,0.5)$) -|($(mid6a8a.east)+(10,0)$)|-(mid6a8a);
    \path [line] (tag12a.east)--node[pos=0.2]{NON}($(tag12a.east)+(1,0)$) -|($(mid8a6b.east)+(8,0)$)|-(mid8a6b);
\end{tikzpicture}


\end{document}

enter image description here

BambOo
  • 8,801
  • 2
  • 20
  • 47
  • Bravo, Excellent, Sir @BambOo, Thank you a lot . – azdoud Apr 01 '18 at 12:07
  • The diamond is too tall, use aspect=2, for example. Also the whole diagram is too sparse, needs to be more compact. – AboAmmar Apr 01 '18 at 12:19
  • The size of the blocks and the sparsity of the total diagram rely a lot on the contents of the blocks, that is why it did not put too much effort in that, but for sure it requires more tweaking. – BambOo Apr 01 '18 at 12:34
  • For the diagram sparse I don't have the problem with it, the main purpose is obtained thanks to all of you – azdoud Apr 01 '18 at 12:44