36

Is there a summary of the differences of "--", "to", and "edge" and what is the best current practice to choose one these path commands to connect nodes?

2 Answers 2

46

Let's look at this example.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
    \draw (0,0) -- ++(2,0) -- ++(0,0.5);
    \draw (0,-1) to ++(2,0) -- ++(0,0.5);
    \draw (0,-2) edge ++(2,0) -- ++(0,0.5);
\end{tikzpicture}
\end{document}

which results in

three lines

Without options, to is really the same as --. edge is different, though: the edge operation adds a new path after completing the current one (see from where the vertical "stem" starts?)

The difference is that with to, you can have options that change the path (like in, out, bend ...), etc. This happens also with edge, with the plus that, edge being another path, you can change things like color, dashed pattern, etc.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
    \draw (0,0) -- ++(2,0) -- ++(0,0.5);
    \draw (0,-1) to[bend left] ++(2,0) -- ++(0,0.5);
    \draw (0,-2) edge[red, bend left] ++(2,0) -- ++(0,0.5);
\end{tikzpicture}
\end{document}

second example

Notice that \draw (0,-1) to[red, bend left] ++(2,0) -- ++(0,0.5); will not create a red line... because the color is a global property of a path.

Another important difference is that if the starting point is a node, the edge operation acts as if one were using the node name as the starting coordinate, so it uses border anchors:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={circle, draw}]
    \draw (0,0) node{} to[bend left] ++(2,0);
    \node (A) at (0,-1) {};
    \draw (A) to[bend left] ++(2,0);
    \draw (0,-2)  node{} edge[red, bend left] ++(2,0);
\end{tikzpicture}
\end{document}

starting from a node

4
  • 8
    +1 This is a super helpful answer!
    – Alan Munn
    Commented Jun 1, 2022 at 19:25
  • @AlanMunn thanks!
    – Rmano
    Commented Jun 1, 2022 at 19:54
  • We can use + in stead ++ in \draw (0,-2) edge[red, bend left] ++(2,0) -- ++(0,0.5); , that is, \draw (0,-2) edge[red, bend left] +(2,0) -- +(0,0.5);. In turn, it can be replaced by \draw (0,-2) -- +(0,0.5); \draw (0,-2) edge[red, bend left] +(2,0); Evenmore, without using edge, we may code \draw (0,-2) -- +(0,0.5); \draw[red] (0,-2) to[bend left] +(2,0);
    – Black Mild
    Commented Jun 2, 2022 at 9:30
  • 1
    Yes, you can do with several paths all these things. The objective of my answer is to explain the differences, not advocating for edge or to ;-). I find using edge useful when you do not want to break the path, see for example circuitikz.github.io/circuitikz/circuitikzmanualgit.pdf#f0
    – Rmano
    Commented Jun 2, 2022 at 9:40
5

Here is my personal experience only. From pgfmanual, the keys --, to, and edge are used to connect 2 nodes, or 2 coordinates, or 1 node with 1 coordinate. Note that coordinate is a special (without content) node. I see that edge can be ignored. I never use edge when drawing with TikZ, and I feel absolutely comfortable and effective: the less keys I use, the simpler coding, the same output figure. In other words, I use plain TikZ.

Only when to is used WITHOUT any other options, then it is abbreviated as --, meaning straight connecting. All other cases (to WITH some option like in, out, bend left, controls, distance, etc. see the list of options in Section 74.3 Curves in the pgfmanual), just use to.

For example,

  • \draw (A)--(B); is the same as \draw (A) to (B);
  • \draw (A) to [bend left] (B); works; meanwhile \draw (A) -- [bend left] (B); gives an error.

More examples:

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path 
(0,0) coordinate (A)    
(3,2) coordinate (B)
;

% "--" is "to" when it is used WITHOUT any other options
\draw (A) -- node [sloped,above,pos=.3,red] {x} (B);
\draw (A) to node [sloped,above,pos=.3,red] {x} (B);

% When "to" is used WITH some options, just "to"
\draw (A) to [out=90,in=180] node [sloped,above,pos=.7,blue] {x} (B);
\end{tikzpicture}
\begin{tikzpicture}
\path 
(0,0) node[draw] (A) {A}    
(3,2) node[draw,circle] (B) {B}
;
    
% "--" is "to" when it is used WITHOUT any other options
\draw (A) -- node [sloped,above,pos=.3,red] {x} (B);
\draw (A) to node [sloped,above,pos=.3,red] {x} (B);
    
% When "to" is used WITH some options, just "to"
\draw (A) to [out=90,in=180] node [sloped,above,pos=.7,blue] {x} (B);
\end{tikzpicture}
\begin{tikzpicture}
\path 
(0,0) node[draw] (A) {A}    
(3,2) coordinate (B)
;
    
% "--" is "to" when it is used WITHOUT any other options
\draw (A) -- node [sloped,above,pos=.3,red] {x} (B);
\draw (A) to node [sloped,above,pos=.3,red] {x} (B);
    
% When "to" is used WITH some options, just "to"
\draw (A) to [out=90,in=180] node [sloped,above,pos=.7,blue] {x} (B);
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .