18

The TikZ manual says

The basic building block of all pictures in TikZ is the path. A path is a series of straight lines and curves that are connected (that is not the whole picture, but let us ignore the complications for the moment).

As this is the fundamental TikZ construction I would like to properly understand what a path really is. And so, for example in the code below:

\path (0,0) -- (1,0);

we have one invisible path. But what about the following:

\draw (0,0) -- (1,0) (2,0) -- (3,0);

Do we have one path or two paths? As a geometrical object, the two segments are disconnected, yet they are part of the same construction. Thus my question also concerns the problem of what connected means in the case of paths. Naive interpretation is that it means something like topologically connected, but then the following should be one path:

\draw (0,0) circle (1);
\draw (-2,-2) -- (2,2);

while I think these are two independent paths. On the other hand, what if we enclose the above in the scope environment:

\begin{scope}
\draw (0,0) circle (1);
\draw (-2,-2) -- (2,2);
\end{scope}

I've been trying to figure it out and I tend to think that a path is anything that is encapsulated in an independent fragment of code limited by a semicolon (that is while using loops we would have as many instances of paths as repetitions of the loop). But I might as well be wrong.

I know that the full picture of the meaning of the path concept may not be necessary to master TikZ, but it does not stymie my curiosity.

3
  • 1
    I'd say that \draw (0,0) -- (1,0) (2,0) -- (3,0); is creating only one path. Try it with \draw[->] (0,0) -- (1,0) (2,0) -- (3,0); and you'll see it.
    – SebGlav
    Nov 10, 2021 at 22:49
  • 1
    One tikz \path command can create an arbitrary large number (up to TeX constraints) of pgf paths, i.e. things that can be impacted by pgf commands like \pgfusepath{stroke}. To see this, consider e.g. an edge or decorations. In this sense, a path is not well defined. A pgf path is better defined.
    – user255043
    Nov 10, 2021 at 23:21
  • \draw (0,0) -- (1,0) (2,0) -- (3,0); is one path that consists of 2 connected path components, see more in my previous answer here tex.stackexchange.com/a/615851/140722
    – Black Mild
    Nov 10, 2021 at 23:41

2 Answers 2

19

It might be helpful to look at the PDF produced by three different constructions that look the same to see the difference between simple paths, compound paths and grouped paths:

I've done this in l3draw since it's syntax matches closely what you get in the final PDF.

\documentclass{article}
\pdfcompresslevel=0
\pdfobjcompresslevel=0

\usepackage{l3draw}

\pagestyle{empty}

\begin{document}

\ExplSyntaxOn

% four simple paths
% there are 4 stroke commands
\draw_begin:
  \color_select:n { red }
  \draw_path_moveto:n { 0bp , 0bp }
  \draw_path_lineto:n { 10bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \color_select:n { black }
  \draw_path_moveto:n { 20bp , 0bp }
  \draw_path_lineto:n { 20bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \draw_path_moveto:n { 30bp , 0bp }
  \draw_path_lineto:n { 30bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \color_select:n { red }
  \draw_path_moveto:n { 40bp , 0bp }
  \draw_path_lineto:n { 50bp , 20bp }
  \draw_path_use_clear:n { stroke }
\draw_end:

\par

% red paths are simple paths, black path is a compound path
% there are 3 stroke commands
\draw_begin:
  \color_select:n { red }
  \draw_path_moveto:n { 0bp , 0bp }
  \draw_path_lineto:n { 10bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \color_select:n { black }
  \draw_path_moveto:n { 20bp , 0bp }
  \draw_path_lineto:n { 20bp , 20bp }
  \draw_path_moveto:n { 30bp , 0bp } % ← notice the moveto
  \draw_path_lineto:n { 30bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \color_select:n { red }
  \draw_path_moveto:n { 40bp , 0bp }
  \draw_path_lineto:n { 50bp , 20bp }
  \draw_path_use_clear:n { stroke }
\draw_end:

\par

% four simple paths, but the black paths are grouped
% there is no need to set the stroke colour back to red after the group
% there are 4 stroke commands
\draw_begin:
  \color_select:n { red }
  \draw_path_moveto:n { 0bp , 0bp }
  \draw_path_lineto:n { 10bp , 20bp }
  \draw_path_use_clear:n { stroke }
  \draw_scope_begin:
    \color_select:n { black }
    \draw_path_moveto:n { 20bp , 0bp }
    \draw_path_lineto:n { 20bp , 20bp }
    \draw_path_use_clear:n { stroke }
    \draw_path_moveto:n { 30bp , 0bp }
    \draw_path_lineto:n { 30bp , 20bp }
    \draw_path_use_clear:n { stroke }
  \draw_scope_end:
  \draw_path_moveto:n { 40bp , 0bp }
  \draw_path_lineto:n { 50bp , 20bp }
  \draw_path_use_clear:n { stroke }
\draw_end:

\ExplSyntaxOff

\end{document}

The output of the 3 drawings is the same to look at:

output

But the PDF is slightly different for each case:

First drawing PDF

1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour
0 0 m                            ← move to (0,0)
10 20 l                          ← line to (10,20)
S                                ← stroke path with current stroke colour (red)
0.0 g 0.0 G                      ← select Grayscale black stroke and fill colour
20 0 m                           ← move to (20,0)
20 20 l                          ← line to (20,20)
S                                ← stroke path with current stroke colour (black)
30 0 m                           ← move to (30,0)
30 20 l                          ← line to (30,20)
S                                ← stroke path with current stroke colour (black)
1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour
40 0 m                           ← move to (40,0)
50 20 l                          ← line to (50,20)
S                                ← stroke path with current stroke colour (red)

This is (approximately) equivalent to the following tikz picture:

\begin{tikzpicture}[color=red]
  \draw (0bp,0bp) -- (10bp,20bp);
  \draw[black] (20bp,0bp) -- (20bp,20bp);
  \draw[black] (30bp,0bp) -- (30bp,20bp);
  \draw (40bp,0bp) -- (50bp,20bp);
\end{tikzpicture}

Second drawing PDF

1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour
0 0 m                            ← move to (0,0)
10 20 l                          ← line to (10,20)
S                                ← stroke path with current stroke colour (red)
0.0 g 0.0 G                      ← select Grayscale black stroke and fill colour
20 0 m                           ← move to (20,0)
20 20 l                          ← line to (20,20)
30 0 m                           ← move to (30,0)
30 20 l                          ← line to (30,20)
S                                ← stroke path with current stroke colour (black)
1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour
40 0 m                           ← move to (40,0)
50 20 l                          ← line to (50,20)
S                                ← stroke path with current stroke colour (red)

This is (approximately) equivalent to the following tikz picture:

\begin{tikzpicture}[color=red]
  \draw (0bp,0bp) -- (10bp,20bp);
  \draw[black] (20bp,0bp) -- (20bp,20bp) (30bp,0bp) -- (30bp,20bp);
  \draw (40bp,0bp) -- (50bp,20bp);
\end{tikzpicture}

Third drawing PDF

1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour
0 0 m                            ← move to (0,0)
10 20 l                          ← line to (10,20)
S                                ← stroke path with current stroke colour  (red)
q                                ← save graphics state
0.0 g 0.0 G                      ← select Grayscale black stroke and fill colour
20 0 m                           ← move to (20,0)
20 20 l                          ← line to (20,20)
S                                ← stroke path with current stroke colour (black)
30 0 m                           ← move to (30,0)
30 20 l                          ← line to (30,20)
S                                ← stroke path with current stroke colour (black)
1.0 0.0 0.0 rg 1.0 0.0 0.0 RG    ← select RGB red stroke and fill colour (this isn't technically needed, but LaTeX uses a stack for colour)
Q                                ← restore graphics state
40 0 m                           ← move to (40,0)
50 20 l                          ← line to (50,20)
S                                ← stroke path with current stroke colour (red)

This is (approximately) equivalent to the following tikz picture:

\begin{tikzpicture}[color=red]
  \draw (0bp,0bp) -- (10bp,20bp);
  \begin{scope}[color=black]
    \draw (20bp,0bp) -- (20bp,20bp);
    \draw (30bp,0bp) -- (30bp,20bp);
  \end{scope}
  \draw (40bp,0bp) -- (50bp,20bp);
\end{tikzpicture}
4
  • Just ..... wow! Nov 11, 2021 at 6:53
  • 1
    l3draw is heavily modelled on the pgf layer: the syntax is the same in a lot of places, with changes quite deliberate (for example where reordering arguments makes more sense with hindsight).
    – Joseph Wright
    Nov 11, 2021 at 8:16
  • Correct me if I am wrong. In the first example, we have four independent paths. In the second, three with one of them being a compound path (composed of two segments). In the third, there are four paths (stroke path is used four times) but two of them are grouped. In this last case we have a semi-global option ([color=black]) which is applied two times, while in the second example it is used once to the compound path. So, path is a context dependent concept, as I can see it. Nov 11, 2021 at 17:32
  • 2
    @MadHatter, yes this is right. @ABC's answer shows the the tikz command \path allows for all kinds of drawing objects to be chained together, but paths are a better defined thing at the PDF/PostScript level. And typically drawing programs (Illustrator, Inkscape) talk about paths in the sense I have in this answer. I would also put rectangles, circles, and arcs in category. But text, for example, is handled in a different way at the PDF level. Nov 11, 2021 at 23:02
13

The quote you start your question with stems from section 2.3 Straight Path Construction in the pgfmanual. However, the term "path" appears in many different contexts.

  1. The \path command can actually create an almost arbitrary number of more low-level pgf path objects. Within a \path command you can put lines, as described in section 2.3 and curves, but also many other things like edges, which in a certain way correspond to independent paths, nodes, which have a boundary path (and possibly additional low-level paths) and even pics, which can contain their own \path commands. Here I am referring loosely to all descendants of \path commands like \draw, \node and \pic as \path.
  2. There are objects that one can loosely call "pgf paths". These get created from low-level commands like \pgfpathlineto and \pgfpathcurveto as well as a command of the sort \pgfusepath{stroke} which really, say, draws them. Notice that these paths can have "gaps" created via \pgfpathmoveto, yet they have one common line style (color, width, etc.).
  3. To make matters worse, there are yet other path objects, namely paths used in intersections and defined via name path. They are almost like the ones under 1. except that not everything that gets put behind \path[name path] makes it to the path you can use in intersections. For instance, if you put a node in a named path, the node boundary won't be part of the named path, however, if you use node[named path=...] it will.

The last point is illustrated by a small example.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[d/.style={circle,fill,inner sep=1pt,label=above:#1}]
 \path node[name path=c1,circle,draw,minimum size=1cm] (C1){};
 \path[name path=l1,draw] (-1,1) -- (1,-1);
 \path[name intersections={of=l1 and c1,total=\t}]
  foreach \i in {1,...,\t} {(intersection-\i) 
    node[d={$i_\i$}]{}};
\begin{scope}[xshift=3cm]
 \path[name path=c2,draw] (-1,-1) -- (1,1) 
    (0,0) node[circle,draw,minimum size=1cm] (C1){};
 \path[name path=l2,draw] (-1,1) -- (1,-1);
 \path[name intersections={of=l2 and c2,total=\t}]
  foreach \i in {1,...,\t} {(intersection-\i) 
    node[d={$i_\i'$}]{}};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

The bottomline is that a path in tikz is not necessarily a well-defined term, there are several different objects that carry the name. I also think that section 2.3 wants to make the reader familiar with certain 1-dimensional objects, in this case straight lines, and uses the term "path" for those. However, as already mentioned, this term gets used for other things as well.

You must log in to answer this question.

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