3

I'm trying to make a flowchart for my code using latex using this code,

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=1.7cm, minimum height=0.7cm,text centered, draw=black, fill=red!30]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=1.7cm, minimum height=0.7cm, text centered, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=1.7cm, minimum height=0.7cm, text centered, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, inner sep=0pt]
\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{block} = [rectangle, draw, text width=12em, text centered, minimum width=1.7cm, minimum height=0.7cm, fill=orange!30]

\begin{document}

\begin{center}
\begin{tikzpicture}[node distance=1cm]

\node (start) [startstop, align=center] {f.py \\ (Start)};
\node (pro1)  [process, below of=start, align=left, yshift=-0.1cm] {Import data from Inputs.py \\ Couter zero};
\node (pro2)  [process, below of=pro1, align=center, yshift=-0.1cm] {Apply inverse transformation \\ to filament and space points};
\node (pro3)  [process, below of=pro2] {Calculate magnetic field};
\node (pro4)  [block, below of=pro3, text width=16em,, yshift=-0.3cm]{\begin{itemize}
\item Apply transformation to filament and space points
\item Increase counter by 1
\end{itemize}};
\node (dec1)  [decision, below of=pro4, yshift=-0.8cm, align=center, aspect=2, yshift=-0.1cm] {Counter = no of filaments?};
\node (pro5)  [process, below of=dec1, align=center, yshift=-0.5cm] {Store data};
\node (stop)  [startstop, align=center, below of=pro5] {f.py \\ (Stop)};

\draw [arrow] (start) -- (pro1);
\draw [arrow] (pro1) -- (pro2);
\draw [arrow] (pro2) -- (pro3);
\draw [arrow] (pro3) -- (pro4);
\draw [arrow] (pro4) -- (dec1);
\draw [arrow] (dec1) -- ++ (1cm,0) |- node[anchor=east]{yes} (pro2);
\draw [arrow] (dec1) -- node[anchor=east]{yes} (pro5);
\draw [arrow] (pro5) -- (stop);

\end{tikzpicture}
\end{center}

\end{document}

and got the following result,

screenshot from given example

As shown in the image you notice that arrow for the loop is not proper. I'm new to this tikz environment so I don't know much. Pardon me if my doubt is too silly. Please help to solve this problem.

1
  • 1
    welcome to tex.se! similar questions has been asked many times here :-). try \draw [arrow] (dec1.east) -- ++ (2cm,0) |- node[above]{yes} (pro2); – Zarko Feb 6 '19 at 18:54
6

your problem should be solved by my comment: if you declare relative distance of a some coordinate from node, than is considered node's center. to obtain desired result, you should tell to tikz which node's shape coordinate you like to consider in relative distance.

so this answer is concerned with off-topic issues of your flowchart code. it can be simplified with use of tikz library chains and better definition of flowchart node's shape styles. beside this, to my taste i would draw your flowchart on the following way:

enter image description here

\documentclass[a4paper]{article}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                quotes,
                shapes.geometric}

\tikzset{FlowChart/.style={% this style can be used also at other flowcharts, 
                           % just call it with "FlowChart", see picture code below
startstop/.style = {rectangle, rounded corners, draw, fill=red!30,
                    minimum width=3cm, minimum height=1cm, align=center,
                    on chain, join=by arrow},
  process/.style = {rectangle, draw, fill=orange!30,
                    text width=5cm, minimum height=1cm, align=center,
                    on chain, join=by arrow},
 decision/.style = {diamond, aspect=1.5, draw, fill=green!30,
                    minimum width=3cm, minimum height=1cm, align=center,
                    on chain, join=by arrow},
       io/.style = {trapezium, trapezium stretches body,   % not used in your flowchart
                    trapezium left angle=70, trapezium right angle=110,
                    draw, fill=blue!30,
                    minimum width=3cm, minimum height=1cm,
                    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
                    align=center,
                    on chain, join=by arrow},
    arrow/.style = {thick,-Triangle}
                        }
        }% end of tikzset


\begin{document}
\begin{center}
    \begin{tikzpicture}[FlowChart,                   % used are styles from tikzset FlowChart    
    node distance = 5mm and 7mm,
      start chain = A going below                    % The nodes in the chain 
                                                     % will be named by A-1, A-2, ...
                        ]
\node   [startstop] {f.py \\ (Start)};               % A-1
\node   [process]   {Import data from Inputs.py \\
                     Couter zero};
\node   [process]   {Apply inverse transformation \\
                     to filament and space points};
\node   [process]   {Calculate magnetic field};
\node   [process]   {\begin{itemize}[leftmargin=*,nosep]
                \item Apply transformation to filament and space points
                \item Increase counter by 1
                    \end{itemize}
                    };
\node   [decision] {Counter =\\ no of filaments?};
\node   [process]   {Store data};
\node   [startstop] {f.py \\ (Stop)};
% lines not considered by join macro 
\draw [arrow] (A-6.east) to ["yes"] ++ (1.5,0) |- (A-3);
\path   (A-6) to ["no"] (A-7);
    \end{tikzpicture}
\end{center}

\end{document}
0
1

You should place you're point at more than 1 cm. You did:

\draw [arrow] (dec1) -- ++ (1cm,0) |- node[anchor=east]{yes} (pro2);

Try 2 or 3 cm like this:

\draw [arrow] (dec1) -- ++ (3cm,0) |- node[anchor=east]{yes} (pro2);

You put you're ++ at one cm from the center of the diamond.

1
  • 1
    welcome to tex.se! your solution anticipate measurement distance from center of shape, i.e. for some distance from shape in this case you need to consider shape width too. in cases, when it is not prescribed (with `text width, for example), this can be annoying. also placing of the "yes" is with your suggestion different than it is accepted answer. anyway, +1 :-) – Zarko May 15 '19 at 3:03

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.