10 Actions on Paths

Once a path has been constructed, different things can be done with it. It can be drawn (or stroked) with a “pen,” it can be filled with a color or shading, it can be used for clipping subsequent drawing, it can be used to specify the extend of the picture--or any combination of these actions at the same time.

To decide what is to be done with a path, two methods can be used. First, you can use a special-purpose command like \draw to indicate that the path should be drawn. However, commands like \draw and \fill are just abbreviations for special cases of the more general method: Here, the \path command is used to specify the path. Then, options encountered on the path indicate what should be done with the path.

For example, \path (0,0) circle (1cm); means “This is a path consisting of a circle around the origin. Do not do anything with it (throw it away).” However, if the option draw is encountered anywhere on the path, the circle will be drawn. “Anywhere” is any point on the path where an option can be given, which is everywhere where a path command like circle (1cm) or rectangle (1,1) or even just (0,0) would also be allowed. Thus, the following commands all draw the same circle:


\path [draw] (0,0) circle (1cm);
\path (0,0) [draw] circle (1cm);
\path (0,0) circle (1cm) [draw];

Finally, \draw (0,0) circle (1cm); also draws a path, because \draw is an abbreviation for \path [draw] and thus the command expands to the first line of the above example.

Similarly, \fill is an abbreviation for \path[fill] and \filldraw is an abbreviation for the command \path[fill,draw]. Since options accumulate, the following commands all have the same effect:


\path [draw,fill]   (0,0) circle (1cm);
\path [draw] [fill] (0,0) circle (1cm);
\path [fill] (0,0) circle (1cm) [draw];
\draw [fill] (0,0) circle (1cm);
\fill (0,0) [draw] circle (1cm);
\filldraw (0,0) circle (1cm);

In the following subsection the different actions are explained that can be performed on a path. The following commands are abbreviations for certain sets of actions, but for many useful combinations there are no abbreviations:

\draw

Inside {tikzpicture} this is an abbreviation for \path[draw].

\fill

Inside {tikzpicture} this is an abbreviation for \path[fill].

\filldraw

Inside {tikzpicture} this is an abbreviation for \path[fill,draw].

\shade

Inside {tikzpicture} this is an abbreviation for \path[shade].

\shadedraw

Inside {tikzpicture} this is an abbreviation for \path[shade,draw].

\clip

Inside {tikzpicture} this is an abbreviation for \path[clip].

\useasboundingbox

Inside {tikzpicture} this is an abbreviation for \path[use as bounding box].

\node

Inside {tikzpicture} this is an abbreviation for \path node. Note that, for once, node is not an option but a path operation.

\coordinate

Inside {tikzpicture} this is an abbreviation for \path coordinate.

10.1 Specifying a Color

The most unspecific option for setting colors is the following:

As pointed out above, the color= option applies to “everything” (except to shadings), which is not always what you want. Because of this, there are several more specialized color options. For example, the draw= option sets the color used for drawing, but does not modify the color used for filling. These color options are documented where the path action they influence is described.

10.2 Drawing a Path

You can draw a path using the following option:

The following subsections list the different options that influence how a path is drawn. All of these options only have an effect if the draw options is given (directly or indirectly).

10.2.1 Graphic Parameters: Line Width, Line Cap, and Line Join

There are a number of predefined styles that provide more “natural” ways of setting the line width. You can also redefine these styles. Remember that you can leave out the style= when setting a style.

10.2.2 Graphic Parameters: Dash Pattern

As for the line thickness, some predefined styles allow you to set the dashing conveniently.

10.2.3 Graphic Parameters: Draw Opacity

When a line is drawn, it will normally “obscure” everything behind it as if you has used perfectly opaque ink. It is also possible to ask TikZ to use an ink that is a little bit (or a big bit) transparent. To do so, use the following option:

Note that the draw opacity options only sets the opacity of drawn lines. The opacity of fillings is set using the option fill opacity (documented in Section 10.3.2. The option opacity sets both at the same time.

10.2.4 Graphic Parameters: Arrow Tips

When you draw a line, you can add arrow tips at the ends. It is only possible to add one arrow tip at the start and one at the end. If the path consists of several segments, only the last segment gets arrow tips. The behavior for paths that are closed is not specified and may change in the future.

10.2.5 Graphic Parameters: Double Lines and Bordered Lines

10.3 Filling a Path

To fill a path, use the following option:

10.3.1 Graphic Parameters: Interior Rules

The following two options can be used to decide how interior points should be determined:

10.3.2 Graphic Parameters: Fill Opacity

Analogously to the draw opacity, you can also set the filling opacity:

10.4 Shading a Path

You can shade a path using the shade option. A shading is like a filling, only the shading changes its color smoothly from one color to another.

For some shadings it is not really clear how they can “fill” the path. For example, the ball shading normally looks like this:

SVG-Viewer needed.

. How is this supposed to shade a rectangle? Or a triangle?

To solve this problem, the predefined shadings like ball or axis fill a large rectangle completely in a sensible way. Then, when the shading is used to “shade” a path, what actually happens is that the path is temporarily used for clipping and then the rectangular shading is drawn, scaled and shifted such that all parts of the path are filled.

10.4.1 Choosing a Shading Type

The default shading is a smooth transition from gray to white and from above to bottom. However, other shadings are also possible, for example a shading that will sweep a color from the center to the corners outward. To choose the shading, you can use the shading= option, which will also automatically invoke the shade option. Note that this does not change the shading color, only the way the colors sweep. For changing the colors, other options are needed, which are explained below.

You can also define new shading types yourself. However, for this, you need to use the basic layer directly, which is, well, more basic and harder to use. Details on how to create a shading appropriate for filling paths are given in Section 28.3.

10.4.2 Choosing a Shading Color

The following options can be used to change the colors used for shadings. When one of these options is given, the shade option is automatically selected and also the “right” shading.

10.5 Establishing a Bounding Box

PGF is reasonably good at keeping track of the size of your picture and reserving just the right amount of space for it in the main document. However, in some cases you may want to say things like “do not count this for the picture size” or “the picture is actually a little large.” For this you can use the option use as bounding box or the command \useasboundingbox, which is just a shorthand for \path[use as bounding box].

There is a node that allows you to get the size of the current bounding box. The current bounding box node has the rectangle shape rectangle shape and its size is always the size of the current bounding box.

 

SVG-Viewer needed.

 

\begin{tikzpicture}
  \draw[red] (0,0) circle (2pt);
  \draw[red] (2,1) circle (3pt);

  \draw (current bounding box.south west) rectangle
        (current bounding box.north east);

  \draw[red] (3,-1) circle (4pt);

  \draw[thick] (current bounding box.south west) rectangle
               (current bounding box.north east);
\end{tikzpicture}

10.6 Using a Path For Clipping

To use a path for clipping, use the clip option.