19

Maybe this question is naive, but when will I need to use edge instead of --?, and how can I use edge to replicate the same output of path conventional draw command --?

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes.geometric,calc}
\begin{document}
    \begin{tikzpicture}[>=latex',very thick,align=center]

        \tikzset{%
    block/.style= {draw, rectangle, text width=2cm,minimum height=1cm,align=center}
    }

    \node [block]  (start1) {Start1};
    \node [block, right = 1cm of start1.east] (end1) {End1};
    \node [block, below= 2cm of start1]  (start2) {Start2};
    \node [block, right = 1cm of start2] (end2) {End2};

    \path[draw,->] 
    (start1.north) -- ++(0,1cm) -- ++(3.25cm,0) -- (end1.north);
    \path[draw,->] 
    (start2.north) edge ++(0,1cm) edge ++(3.25cm,0) edge (end2.north);
    \end{tikzpicture}
\end{document}

enter image description here

  • 1
    The edge command is for a particular purpose and isn't as flexible as normally drawing commands, so this might need a bit of work to make it happen. Can you explain why you want to be able to do this using edge rather than just normal path construction commands? – Loop Space Jun 11 '16 at 23:39
  • I am new to Tikz, so I found sometimes -- is used and other times edge is used instead. So basically, I don't know what the advantages of using edge are and the I can't fully understand from the manual when to use edge – Diaa Jun 11 '16 at 23:42
  • 1
    In short, edge is mainly for connecting a node that's just been defined. If you've named your nodes, you can refer to them later and don't need to use edge. So until you get more familiar with TikZ, it is better to use the standard drawing commands than edge. – Loop Space Jun 11 '16 at 23:53
  • Thanks for your explanation, considering my MWE, I would be grateful, if you don't mind showing me how to, for example, connect Start 2 to End 2 which wasn't predefined. – Diaa Jun 11 '16 at 23:59
  • 1
    See, if tex.stackexchange.com/questions/251642/… can help you. – Zarko Jun 12 '16 at 0:17
23

This answer doesn't attempt to cover all the details but, rather, to give a 'first pass' explanation of some of the differences.

edge constructs a separate path which can have entirely different attributes from the main path. Unlike points on the main path, an edge does not move anywhere.

This isn't really what happens, but it is as if this happens:

  1. Suppose we start with a path \path [draw=blue, -{Stealth[]}] (0,0).

    • So far, we haven't actually gone anywhere. We've said that this path should be drawn and that it should have a certain arrow tip. We've moved to the origin for the beginning of the path.
  2. So we continue with ++(1,0).

    • This moves us 1 unit to the right, still without drawing anything.
  3. Now we add -- ++(1,0).

    • This moves us another 1 to the right and, this time, the path is drawn. If we put a ; here, we'll get our arrow tip here. But we don't do that, so the tip isn't needed yet.

    stage 3

  4. Now, rather than continuing the main path, we add edge ++(0,1}.

    • We can think of TikZ as temporarily stopping the main path construction and taking a diversion to make a small side-path. This path goes from our current position up 1 unit. By default, it inherits the attributes of the main path, so it is blue with an arrow. When the edge operation is complete, TikZ returns to the point at which it started the diversion. It is like nipping into a side-street to pick up some bread from the baker's on the way to picking the kids up from school. Once you've got the bread, you come back to the main street to continue to school.

    stage 4

  5. We now add -- ++(1,0) to our path.

    • So, since we've come back from the detour, we will continue our straight line to the right.

    stage 5

  6. Next, we add edge [draw=red, -{Circle[fill=green]}] ++(0,1).

    • This is another diversion, but this time, we draw the edge in red and change the arrow tip. This only applies to our diversion, of course.

    stage 6

    • Note that this isn't possible with a normal path - you can't make part of it red and part of it blue, for example, but an edge can have quite different attributes from the main path and from any other edge drawn as part of the same \path command.
  7. Finally, we complete the path with -- ++(1,0);.

    stage 7

Complete code:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[x=25mm, y=25mm, line width=1pt]
  \path [draw=blue, -{Stealth[]}] (0,0) ++(1,0) -- ++(1,0) edge ++(0,1) -- ++(1,0) edge [draw=red, -{Circle[fill=green]}] ++(0,1) -- ++(1,0);
  \foreach \i/\j in {(0,0)/{(0,0)},(1,0)/{++(1,0)},(2,0)/{++(1,0)},(3,0)/{++(1,0)},(4,0)/{++(1,0)}} \path \i node [below, ] {\j};
  \foreach \i/\j in {(2,1)/{++(0,1)},(3,1)/{++(0,1)}} \path \i node [right, ] {\j};
\end{tikzpicture}
\end{document}

Re. MWE

In the case of the example in the question, you can, of course, just write

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}
  [
    >=Latex,
    very thick,
    align=center,
    block/.style= {draw, rectangle, text width=2cm,minimum height=1cm,align=center},
  ]
  \node [block]  (start1) {Start1};
  \node [block, right = 1cm of start1.east] (end1) {End1};
  \node [block, below= 2cm of start1]  (start2) {Start2};
  \node [block, right = 1cm of start2] (end2) {End2};
  \path [draw, ->] (start1.north) -- ++(0,1cm) -- ++(3.25cm,0) -- (end1.north);
  \path [draw, ->] (start2.north) -- ++(0,1cm) -- ++(3.25cm,0) -- (end2.north);
\end{tikzpicture}
\end{document}

to produce

two U-turns

However, I take it you would like to know how to do this all-in-one as it were.

The node and path commands above could be replaced by the following 2 paths

  \path [draw, ->] node [block]   (start1) {Start1} (start1.north) |- ++(3.25cm,1cm) node [block, right=1cm of start1] (end1)  {End1} -- (end1);
  \path [draw, ->] node [block, below= 2cm of start1]   (start2) {Start2} (start2.north) |- ++(3.25cm,1cm) node [block, right=1cm of start2] (end2)  {End2} -- (end2);

which produce identical output, or, more concisely but less obviously, with

  \coordinate (start0);
  \foreach \i [evaluate=\i as \j using int(\i-1), evaluate=\j as \k using \j*2] in {1,2}
  \path [draw, ->] node [block, below=\k cm of start\j]   (start\i) {Start\i} (start\i.north) |- ++(3.25cm,1cm) node [block, right=1cm of start\i] (end\i)  {End\i} -- (end\i);
  • 3
    This answer is on the highest level of clarity and elegance. I don't have words to highly appreciate your time and effort you put to constitute such an answer. – Diaa Jun 12 '16 at 0:27
  • 1
    @DiaaAbidou Thank you very much. I really appreciate your saying so, even if my answer scarcely deserves it :-). I've added an edit which addresses the question you asked in a comment on your question. – cfr Jun 12 '16 at 0:44
  • Honestly, I got more than I bargained for. Your answer is encyclopedic to me in every aspect. I greatly appreciate it. – Diaa Jun 12 '16 at 0:50
  • 1
    You know it is because you asked a really good question, right? I knew what you already understood and what you found confusing. So I could focus on explaining the next bit you needed to use TikZ effectively. It is fantastic when people ask questions like that! – cfr Jun 12 '16 at 0:53
  • Following the same MWE, I don't understand how to accurately locate the nodes anchors when drawing the path segments. So, I asked this question and I hope you can help me there tex.stackexchange.com/q/314473/2288. – Diaa Jun 13 '16 at 8:44

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.