TransWikia.com

How can I move arrows towards left or right in a tikz figure?

TeX - LaTeX Asked on May 13, 2021

I have been trying to draw a flowchart using tikz. I took help from the overleaf’s guide to define the elements. Overleaf LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 3)—Creating Flowcharts
This works fine until I faced a complicated chart that goes back several steps from decision block.
The code I used:

    documentclass[a4paper]{article} 
usepackage{graphicx}
usepackage{amsmath,amssymb,amsfonts}
usepackage{xcolor}
usepackage[colorlinks=true,urlcolor=blue,citecolor=blue]{hyperref}
usepackage{tikz}
usetikzlibrary{shapes.geometric, arrows}

tikzstyle{io} = [trapezium, trapezium left angle=80, trapezium right angle=100, minimum width=1cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30]
tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
tikzstyle{arrow} = [thick,->,>=stealth]

begin{document}
section{Example of a Flowchart}
%
begin{figure}[h]
    centering
    begin{tikzpicture}[node distance=2cm]
    node (W) [io]{Some Matrix A};
    node (C) [io, right of=W, xshift=6cm]{Some Matrix B};
    node (Comp3) [process, below of=W]{Compute this};
    node (Comp4) [process, below of=C]{Compute that};
    node (Proc) [process, below of=Comp3, xshift=4cm]{Compare result A with result B sequentially};
    node (Match) [decision, below of=Proc, yshift=-0.5cm] {Match found ?};
    node (Comp5) [process, below of=Match,yshift=-0.5cm]{Found this match};

    draw [arrow] (W) -- (Comp3);
    draw [arrow] (C) -- (Comp4);
    draw [arrow] (Comp3) |- (Proc);
    draw [arrow] (Comp4) |- (Proc);
    draw [arrow] (Proc) -- (Match);
    draw [arrow] (Match) -- node[anchor=east] {Yes} (Comp5);
    draw [arrow] (Match) -| node[anchor=north] {No} (Comp4);
    end{tikzpicture}
end{figure}

end{document}

In the result, the two arrows got merged. I want to create some horizontal space between them. Is there any way to introduce space or adjust the starting and ending point for those arrows? Or to make curved arrows, instead of straight lines?
enter image description here

One Answer

The code you found somewhere is very old code that was written with the syntax of TikZ version 2. With TikZ 3.0, the code has evolved and makes it much easier to manage the relative positioning of nodes. To do so, just load the positioning library.

For example, when the old syntax is below of=W the new one is below=of W. You will notice that = and of are written in reverse.

With this new syntax, I place the nodes in a different order than in your code. I updated the syntax of the styles which are also written with the old syntax.

Translated with www.DeepL.com/Translator (free version)

screenshot

documentclass[tikz,border=5mm]{standalone}

usetikzlibrary{shapes.geometric, arrows} 
usetikzlibrary{positioning}   
tikzset{io/.style = {trapezium, trapezium left angle=80, trapezium right angle=100, minimum width=1cm, minimum height=1cm, text centered, draw=black, fill=blue!30},
    process/.style = {rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30},
    decision/.style={diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30},
     every arrow/.style={thick,>=stealth}}
begin{document}

begin{tikzpicture}%[node distance=1cm]
node (W) [io]{Some Matrix A};
node (Comp3) [process, below =of W]{Compute this};
node (Proc) [process, below right=of Comp3]{Compare result A with result B sequentially};
node (Comp4) [process, above right =of Proc]{Compute that};
node (C) [io, above =of Comp4]{Some Matrix B};
node (Match) [decision, below =of Proc] {Match found ?};
node (Comp5) [process, below =of Match]{Found this match};

draw [->] (W) -- (Comp3);
draw [->] (C) -- (Comp4);
draw [->] (Comp3) |- (Proc);
draw [->] (Comp4.230) |- (Proc);
draw [->] (Proc) -- (Match);
draw [->] (Match) -- node[anchor=east] {Yes} (Comp5);
draw [->] (Match) -| node[anchor=north] {No} (Comp4.310);
end{tikzpicture}

end{document}

Correct answer by AndréC on May 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP