PGF/TikZ Manual

The TikZ and PGF Packages
Manual for version 3.1.10

The Basic Layer

104 Using Paths

104.1 Overview

Once a path has been constructed, it can be used in different ways. For example, you can draw the path or fill it or use it for clipping.

Numerous graph parameters influence how a path will be rendered. For example, when you draw a path, the line width is important as well as the dashing pattern. The options that govern how paths are rendered can all be set with commands starting with \pgfset. All options that influence how a path is rendered always influence the complete path. Thus, it is not possible to draw part of a path using, say, a red color and drawing another part using a green color. To achieve such an effect, you must use two paths.

In detail, paths can be used in the following ways:

  • 1. You can stroke (also known as draw) a path.

  • 2. You can add arrow tips to a path.

  • 3. You can fill a path with a uniform color.

  • 4. You can clip subsequent renderings against the path.

  • 5. You can shade a path.

  • 6. You can use the path as bounding box for the whole picture.

You can also perform any combination of the above, though it makes no sense to fill and shade a path at the same time.

To perform (a combination of) the first four actions, you can use the following command:

To shade a path, use the \pgfshadepath command, which is explained in Section 114.

104.2 Stroking a Path

When you use \pgfusepath{stroke} to stroke a path, several graphic parameters influence how the path is drawn. The commands for setting these parameters are explained in the following.

Note that all graphic parameters apply to the path as a whole, never only to a part of it.

All graphic parameters are local to the current {pgfscope}, but they persists past groups, except for the interior rule (even-odd or nonzero) and the arrow tip kinds. The latter graphic parameters only persist till the end of the current group, but this may change in the future, so do not count on this.

104.2.1 Graphic Parameter: Line Width
  • \pgflinewidth

  • You can access the current line width via the dimension \pgflinewidth. It will be set to the correct line width, that is, even when a group closed, the value will be correct since it is set globally, but when a {pgfscope} closes, the value is set to the correct value it had before the scope.

104.2.2 Graphic Parameter: Caps and Joins
  • \pgfsetbuttcap

  • Sets the line cap to a butt cap. See Section 15.3.1 for an explanation of what this is.

  • \pgfsetroundcap

  • Sets the line cap to a round cap. See again Section 15.3.1.

  • \pgfsetrectcap

  • Sets the line cap to a square cap. See again Section 15.3.1.

  • \pgfsetroundjoin

  • Sets the line join to a round join. See again Section 15.3.1.

  • \pgfsetbeveljoin

  • Sets the line join to a bevel join. See again Section 15.3.1.

  • \pgfsetmiterjoin

  • Sets the line join to a miter join. See again Section 15.3.1.

  • \pgfsetmiterlimit{miter limit factor}

  • Sets the miter limit to miter limit factor. See again Section 15.3.1.

104.2.3 Graphic Parameter: Dashing
104.2.4 Graphic Parameter: Stroke Color
  • \pgfsetstrokecolor{color}

  • Sets the color used for stroking lines to color, where color is a color like red or black!20!red. Unlike the \color command, the effect of this command lasts till the end of the current {pgfscope} and not till the end of the current group.

    The color used for stroking may be different from the color used for filling. However, a \color command will always “immediately override” any special settings for the stroke and fill colors.

    In plain , this command will also work, but the problem of defining a color arises. After all, plain does not provide colors. For this reason, pgf implements a minimalistic “emulation” of the \definecolor, \colorlet, and \color commands. Only gray-scale and rgb colors are supported. For most cases this turns out to be enough.

    (-tikz- diagram)

    \pgfpathcircle{\pgfpoint{0cm}{0cm}}{3mm} \pgfusepath{fill,stroke}
    \pgfpathcircle{\pgfpoint{1cm}{0cm}}{3mm} \pgfusepath{fill,stroke}
    \pgfpathcircle{\pgfpoint{2cm}{0cm}}{3mm} \pgfusepath{fill,stroke}
  • \pgfsetcolor{color}

  • Sets both the stroke and fill color. The difference to the normal \color command is that the effect lasts till the end of the current {pgfscope}, not only till the end of the current group.

104.2.5 Graphic Parameter: Stroke Opacity

You can set the stroke opacity using \pgfsetstrokeopacity. This command is described in Section 115.

104.2.6 Inner Lines

When a path is stroked, it is possible to request that it is stroked twice, the second time with a different line width and a different color. This is a useful effect for creating “double” lines, for instance by setting the line width to 2pt and stroking a black line and then setting the inner line width to 1pt and stroking a white line on the same path as the original path. This results in what looks like two lines, each of thickness 0.5pt, spaced 1pt apart.

You may wonder why there is direct support for this “double stroking” in the basic layer. After all, this effect is easy to achieve “by hand”. The main reason is that arrow tips must be treated in a special manner when such “double lines” are present. First, the order of actions is important: First, the (thick) main line should be stroked, then the (thin) inner line, and only then should the arrow tip be drawn. Second, the way an arrow tip looks typically depends strongly on the width of the inner line, so the arrow tip code, which is part of the basic layer, needs access to the inner line thickness.

Two commands are used to set the inner line width and color.

  • \pgfsetinnerlinewidth{dimension}

  • This command sets the width of the inner line. Whenever a path is stroked (and only then), it will be stroked normally and, afterward, it is stroked once more with the color set to the inner line color and the line width set to dimension.

    In case arrow tips are added to a path, the path is first stroked normally, then the inner line is stroked, and then the arrow tip is added. In case the main path is shortened because of the added arrow tip, this shortened path is double stroked, not the original path (which is exactly what you want).

    When the inner line width is set to 0pt, which is the default, no inner line is stroked at all (not even a line of width 0pt). So, in order to “switch off” double stroking, set dimension to 0pt.

    The setting of the inner line width is local to the current group and not to the current pgf scope.

    Note that inner lines will not be drawn for paths that are also used for clipping. However, this may change in the future, so you should not depend on this.

    (-tikz- diagram)

104.3 Arrow Tips on a Path

After a path has been drawn, pgf can add arrow tips at the ends, depending on how the tips key is set, on whether stroke or clip are used and on whether the path contains closed subpaths. The exact rules when arrow tips are added are explained in Section 16.2.

  • \pgfsetarrows{argument}

  • The argument can be of the form start arrow tip specification-end arrow tip specification. In this case, both the start and the end arrow specification are set:

    (-tikz- diagram)

    \usepgflibrary {arrows.meta}

    Alternatively, argument can be of the form [arrow keys]. In this case, the arrow keys will be set for all arrow tips in the current scope, see Section 16.4.5.

  • \pgfsetshortenend{dimension}

  • Works like \pgfsetshortenstart.

104.4 Filling a Path

Filling a path means coloring every interior point of the path with the current fill color. It is not always obvious whether a point is “inside” a path when the path is self-intersecting and/or consists or multiple parts. In this case either the nonzero winding number rule or the even-odd crossing number rule is used to decide which points lie “inside”. These rules are explained in Section 15.5.

104.4.1 Graphic Parameter: Interior Rule

You can set which rule is used using the following commands:

104.4.2 Graphic Parameter: Filling Color
  • \pgfsetfillcolor{color}

  • Sets the color used for filling paths to color. Like the stroke color, the effect lasts only till the next use of \color.

104.4.3 Graphic Parameter: Fill Opacity

You can set the fill opacity using \pgfsetfillopacity. This command is described in Section 115.

104.5 Clipping a Path

When you add the clip option, the current path is used for clipping subsequent drawings. The same rule as for filling is used to decide whether a point is inside or outside the path, that is, either the even-odd rule or the nonzero rule.

Clipping never enlarges the clipping area. Thus, when you clip against a certain path and then clip again against another path, you clip against the intersection of both.

The only way to enlarge the clipping path is to end the {pgfscope} in which the clipping was done. At the end of a {pgfscope} the clipping path that was in force at the beginning of the scope is reinstalled.

104.6 Using a Path as a Bounding Box

When you add the use as bounding box option, the bounding box of the picture will be enlarged such that the path in encompassed, but any subsequent paths of the current scope will not have any effect on the size of the bounding box. Typically, you use this command at the very beginning of a {pgfpicture} environment. Alternatively, you can use \pgfresetboundingbox, followed by \pgfusepath{use as bounding box} to overrule the picture’s bounding box completely.

Left(-tikz- diagram)right.

\pgfusepath{use as bounding box} % draws nothing