[Tex/LaTex] tikz arrow direction problem in flowchart

tikz-arrowstikz-pgf

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}

Best Answer

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
Related Question