7

The flowchart is almost what I want, but I am having a problem getting the arrow head to point in the correct direction in one of the paths that has turns in it. The picture shows the problem, and the code I used to generated looks good, it just doesn't work.

What am I doing wrong, and how can I fix it? I want to really understand the fix and not just fix this one flowchart, since I have to typeset many flowcharts for a manual.

example

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,arrows.meta}
\tikzstyle{startstop} = [draw, rounded rectangle, text centered, draw=black,thick]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, text centered]
\tikzstyle{process} = [rectangle, text centered, draw=black,thick]
\tikzstyle{decision} = [diamond, text centered, draw=black,thick]
\tikzstyle{arrow} = [-{Stealth[scale=1.2]},rounded corners,thick]
\begin{document}
\begin{tikzpicture}
\node (start) [startstop] {Start};
\node (io1) [io,below=0.5 of start] {Read $R$};
\node (box1) [process,below=0.5 of io1] {$X \gets 0$};
\node (branch1) [decision,aspect=2,below=0.7 of box1] {$X > R-1$};
\node (return) [startstop,left=1 of branch1] {Return};
\node (box2) [process,below=0.7 of branch1] {$C \gets 0$};
\node (branch2) [decision,below=0.7 of box2] {$C > 2$};
\node (box3) [process,right=1 of branch2] {$X \gets X+1$};
\node (io2) [io,below=0.7 of branch2] {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
\draw [arrow] (start) -- (io1);
\draw [arrow] (io1) -- (box1);
\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (branch1) -- coordinate[pos=0.4](m3)(box2);
\node [black,right=0.1 of m3] {False};
\draw [arrow] (branch1) -- coordinate[pos=0.4](m4)(return);
\node [black,above=0.1 of m4] {True};
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);
\draw [arrow] (branch2) -- coordinate[pos=0.4](m5)(io2);
\node [black,right=0.1 of m5] {False};
\draw [arrow] (branch2) -- coordinate[pos=0.4](m6)(box3);
\node [black,above=0.1 of m6] {True};
\draw [arrow] (io2) -- (box4);
\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- ([xshift=-2.5]m2) -- (m2.west);
\draw [arrow] (box3) |- (m1);
\end{tikzpicture}
\end{document}
  • I cannot compile this MWE, I get an error related to [io,below=0.5 of start] – Sergio Parreiras Jan 21 '16 at 20:34
  • On ubuntu 15.10/amd64, the file is sad.tex and I do latex sad.tex twice, then dvipdf sad.dvi, and finally view with mupdf sad.pdf – Latex Loser Jan 21 '16 at 21:00
  • Just replace |-([xshift=-2.5]m2) -- (m2.west) with |-(m2). – Salim Bou Jan 21 '16 at 21:09
8

Instead of

\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- ([xshift=-2.5]m2) -- (m2.west);

use

\draw [arrow] (box4) |- + (-2.5,-1) |- (m2);

Cause of wrong arrow direction is incorrect use of |- in arrow path.

Upgrade: Complete code, with corrected errors in it and simplified is:

\documentclass[border=5mm,tikz,preview]{standalone}
    \usetikzlibrary{arrows, arrows.meta, 
                    chains,
                    positioning,
                    shapes}
\makeatletter
\tikzset{reset join/.code={\def\tikz@after@path{}}}
\makeatother

\begin{document}
\begin{tikzpicture}[
node distance= 7mm and 13 mm,
     start chain = going below,
     base/.style = {draw, thick, align=center, 
                    inner ysep=1mm, inner xsep=2mm,
                    join=by arrow, on chain},
startstop/.style = {base, rounded rectangle},
       io/.style = {trapezium, base,
                    trapezium left angle=70, trapezium right angle=110},
  process/.style = {base},
 decision/.style = {diamond, aspect=1.5, base, inner xsep=0pt},
    arrow/.style = {-{Stealth[scale=1.2]}, rounded corners, thick}
                    ]
 % main
\node (start)   [startstop] {Start};
\node (io1)     [io]        {Read $R$};
\node (box1)    [process]   {$X \gets 0$};
\node (branch1) [decision]  {$X > R-1$};
\node (box2)    [process]   {$C \gets 0$};
\node (branch2) [decision]  {$C > 2$};
\node (io2)     [io]        {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
% left and right
\node (return)  [startstop,left=1 of branch1,reset join] {Return};
\node (box3)    [process, right=1 of branch2,reset join] {$X \gets X+1$};
% coordinates
\node (return)  [startstop,left=of branch1,reset join] {Return};
\node (box3)    [process, right=of branch2,reset join] {$X \gets X+1$};

\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);

\draw[arrow] (branch2.east) node[above right] {True} -- (box3);
%
\draw[arrow] (branch1.west) node[above left] {True} -- (return);
\node[below right] at (branch1.south) {False};
%
\draw[arrow] (box3) |- (m1);
\draw[arrow] (box4) |- + (-2.5,-1) |- (m2);
\end{tikzpicture}
\end{document}

It gives:

                                       

Main changes in code:

  • the connection between nodes is done by join parameter in base style.
  • for two nodes, where use of join parameter is not desired, are set on the end of "main" nodes and use macro reset join defined in the preamble of MWE
  • replace obsolete \tikzstyle{ ...} (now are recommended \tikzset{...} with styles written as option of tikzpicture
  • 1
    In the MWE there is an error, instead of text cent you will place text centered]. – Salim Bou Jan 21 '16 at 21:15
  • @salimbou, thank you for info. I also have problems with some nodes, which not show up as it can be seen from provided picture. I will manage this soon. – Zarko Jan 21 '16 at 21:21
  • I also have the same problem nodes io1 and io2 have no frames? – Salim Bou Jan 21 '16 at 21:27
  • I think key draw must be added to io style. – Salim Bou Jan 21 '16 at 21:29
  • Meanwhile I succeed to rewrote MWE in question. See, if you like approach used in my solution. – Zarko Jan 21 '16 at 21:55
1
  • I simply eliminated ([xshift=-2.5]m2) --, since |- should arrive in the last point, (m2).
  • Anyway I think \tikzstyle{io} is incomplete. EDIT: The draw=black,thick options were missing.
  • Furthermore I had to load petri library.

The MWE is the following.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,arrows.meta,petri}
\tikzstyle{startstop} = [draw, rounded rectangle, text centered, draw=black,thick]
\tikzstyle{io} = [draw=black,thick,trapezium, trapezium left angle=70, trapezium right angle=110,text centered]
\tikzstyle{process} = [rectangle, text centered, draw=black,thick]
\tikzstyle{decision} = [diamond, text centered, draw=black,thick]
\tikzstyle{arrow} = [-{Stealth[scale=1.2]},rounded corners,thick]
\tikzstyle{arrow2} = [{Stealth[scale=1.2]}-,rounded corners,thick]
\begin{document}
\begin{tikzpicture}
\node (start) [startstop] {Start};
\node (io1) [io,below=0.5 of start] {Read $R$};
\node (box1) [process,below=0.5 of io1] {$X \gets 0$};
\node (branch1) [decision,aspect=2,below=0.7 of box1] {$X > R-1$};
\node (return) [startstop,left=1 of branch1] {Return};
\node (box2) [process,below=0.7 of branch1] {$C \gets 0$};
\node (branch2) [decision,below=0.7 of box2] {$C > 2$};
\node (box3) [process,right=1 of branch2] {$X \gets X+1$};
\node (io2) [io,below=0.7 of branch2] {Read $A_{X,C}$};
\node (box4) [process,below=0.7 of io2] {$C \gets C+1$};
\draw [arrow] (start) -- (io1);
\draw [arrow] (io1) -- (box1);
\draw [arrow] (box1) -- coordinate[midway](m1)(branch1);
\draw [arrow] (branch1) -- coordinate[pos=0.4](m3)(box2);
\node [black,right=0.1 of m3] {False};
\draw [arrow] (branch1) -- coordinate[pos=0.4](m4)(return);
\node [black,above=0.1 of m4] {True};
\draw [arrow] (box2) -- coordinate[midway](m2)(branch2);
\draw [arrow] (branch2) -- coordinate[pos=0.4](m5)(io2);
\node [black,right=0.1 of m5] {False};
\draw [arrow] (branch2) -- coordinate[pos=0.4](m6)(box3);
\node [black,above=0.1 of m6] {True};
\draw [arrow] (io2) -- (box4);
\draw [arrow] (box4) |- ++(0,-1) -| ++(-2.5,0) |- (m2);
\draw [arrow] (box3) |- (m1);
\end{tikzpicture}
\end{document}

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.