PGF/TikZ Manual | PGFplots | TikZ Editor

TikZ and PGF Manual

The Basic Layer

103 Decorations

  • \usepgfmodule{decorations} % and plain and pure pgf

  • \usepgfmodule[decorations] % Cont and pure pgf

  • The commands for creating decorations are defined in this module, so you need to load this module to use decorations. This module is automatically loaded by the different decoration libraries.

103.1 Overview

Decorations are a general way of creating graphics by “moving along” a path and, while doing so, either drawing something or constructing a new path. This could be as simple as extending a path with a “zigzagged” line…

(-tikz- diagram)

\usetikzlibrary {decorations,decorations.pathmorphing}
\tikz \draw decorate[decoration=zigzag] {(0,0) -- (3,0)};

…but could also be as complex as typesetting text along a path:

(-tikz- diagram)

\usetikzlibrary {decorations,decorations.text}
\tikz \path decorate [decoration={text along path,
text={Some text along a path}}
]
{ (0,2) .. controls (2,2) and (1,0) .. (3,0) };

The workflow for using decorations is the following:

  • 1. You define a decoration using the \pgfdeclaredecoration command. Different useful decorations are already declared in libraries like decorations.shapes.

  • 2. You use normal path construction commands like \pgfpathlineto to construct a path. Let us call this path the to-be-decorated path.

  • 3. You place the path construction commands inside the environment {pgfdecoration}. This environment takes the name of a previously declared decoration as a parameter. It will then start “walking along” the to-be-decorated path. As it does this, a special finite automaton called a decoration automaton produces new path commands as its output (or even other outputs). These outputs replace the to-be-decorated path; indeed, after the to-be-decorated path has been fully walked along it is thrown away, only the output of the automaton persists.

In the present section the process of how decoration automata work is explained first. Then the command(s) for declaring decoration automata and for using them are covered.

103.2 Decoration Automata

Decoration automata (and the closely related meta-decoration automata) are a general concept for creating graphics “along paths”. For straight lines, this idea was first proposed by Till Tantau in an earlier version of pgf, the idea to extend this to arbitrary path was proposed and implemented by Mark Wibrow. Further versatility is provided by “meta-decorations”. These are automata that decorate a path with decorations.

In the present subsection the different ideas underlying decoration automata are presented.

103.2.1 The Different Paths

In order to prevent confusion with different types of paths, such as those that are extended, those that are decorated and those that are created, the following conventions will be used:

  • The preexisting path refers to the current path in existence before a decoration environment. (Possibly this path has been created by another decoration used earlier, but we will still call this path the preexisting path also in this case.)

  • The input path refers to the to-be-decorated path that the decoration automaton moves along. The input path may consist of many line and curve input segments (for example, a circle or an ellipse consists of four curves). It is specified inside the decoration environment.

  • The output path refers to the path that the decoration creates. Depending on the decoration, this path may or may not be empty (a decoration can also choose to use side-effects instead of producing an output path). The input path is always consumed by the decoration automaton, that is, it is no longer available in any way after the decoration automaton has finished.

The effect of a decoration environment is the following: The input path, which is specified inside the environment, is constructed and stored. This process does not alter the preexisting path in any way. Then the decoration automaton is started (as described later) and it produces an output path (possibly empty). Whenever part of the output path is produced, it is concatenated with the preexisting path. After the environment, the current path will equal the original preexisting path followed by the output path.

It is permissible that a decoration issues a \pgfusepath command. As usual, this causes the current path to be filled or stroked or some other action to be taken and the current path is set to the empty path. As described above, when the decoration automaton starts, the current path is the preexisting path and as the automaton progresses, the current path is constantly being extended by the output path. This means that first time a \pgfusepath command is used on a decoration, the preexisting path is part of the path this command operates on; in subsequent calls only the part of the output path constructed since the last \pgfusepath command will be used.

You can use this mechanism to stroke or fill different parts of the output path in different colors, line widths, fills and shades; all within the same decoration. Alternatively, a decoration can choose to produce no output path at all: the text decoration simply typesets text along a path.

103.2.2 Segments and States

The most common use of a decoration is to “repeat something along a path” (for example, the zigzag decoration repeats (-tikz- diagram) along a path). However, it not necessarily the case that only one thing is repeated: a decoration can consist of different parts, or segments, repeated in a particular order.

When you declare a decoration, you provide a description of how their different segments will be rendered. The description of each segment should be given in a way as if the “\(x\)-axis” of the segment is the tangent to the path at a particular point, and that point is the origin of the segment. Thus, for example, the segment of the zigzag decoration might be defined using the following code:


\pgfpathlineto{\pgfpoint{5pt}{5pt}}
\pgfpathlineto{\pgfpoint{15pt}{-5pt}}
\pgfpathlineto{\pgfpoint{20pt}{0pt}}

pgf will ensure that an appropriate coordinate transformation is in place when the segment is rendered such that the segment actually points in the right direction. Also, subsequent segments will be transformed such that they are “further along the path” toward the end of the path. All transformations are set up automatically.

Note that we did not use a \pgfpathmoveto{\pgfpointorigin} at the beginning of the segment code. Doing so would subdivide the path into numerous subpaths. Rather, we assume that the previous segment caused the current point to be at the origin.

The width of a segment can (and must) be specified explicitly. pgf will use this width to find out the start point of the next segment and the correct rotation. The width the you provide need not be the “real” width of the segment, which allows decoration segments to overlap or to be spaced far apart.

The zigzag decoration only has one segment that is repeated again and again. However, we might also like to have different segments and use rules to describe which segment should be used where. For example, we might have special segments at the start and at the end.

Decorations use a mechanism known in theoretical in computer science as finite state automata to describe which segment is used at a particular point. The idea is the following: For the first segment we start in a special state called the initial state. In this state, and also in all other states later, pgf first computes how much space is left on the input path. That is, pgf keeps track of the distance to the end of the input path. Attached to each state there is a set of rules of the following form: “If the remaining distance on the input path is less than \(x\), switch to state \(q\).” pgf checks for each of these rules whether it applies and, if so, immediately switches to state \(q\).

Only if none of the rules tell us to switch to another state, pgf will execute the state’s code. This code will (typically) add a segment to the output path. In addition to the rules there is also a width parameter attached to each state. pgf then translates the coordinate system by this width and reduces the remaining distance on the input path. Then, pgf either stays in the current state or switches to another state, depending on yet another property attached of the state.

The whole process stops when a special state called final is reached. The segment of this state is immediately added to the output path (it is often empty, though) and the process ends.

103.3 Declaring Decorations

The following command is used to declare a decoration. Essentially, this command describes the decoration automaton.

103.3.1 Predefined Decorations

The three decorations moveto, lineto, and curveto are predefined and “always available”. They are mostly useful in conjunction with meta-decorations. They are documented in Section 50 alongside the other decorations.

103.4 Using Decorations

Once a decoration has been declared, it can be used.

  • \begin{pgfdecoration}{decoration list}

  • environment contents

  • \end{pgfdecoration}

  • The environment contents should contain commands for creating an path. This path is the basis for the input paths for the decorations in the decoration list. In detail, the following happens:

    • 1. The preexisting unused path is saved.

    • 2. The path commands specified in environment contents are executed and this resulting path is saved. The path is then divided into different input paths as follows: The format for each item in {decoration list} is

      {decoration}{length}{before code}{after code}

      The before code and the after code are optional. The input path is divided into input paths as follows: The first input path consists of the first lines of the path specified in the environment contents until the length of the first element of the decoration list is reached. If this length is reached in the middle of a line, the line is broken up at this exact position. Then the second input path has the length of the second element in the decoration list and consists of the lines making up the following length part of the path in the environment contents, and so on.

      If the lengths in the decoration list do not add up to the total length of the path in the environment contents, either some decorations are dropped (if their lengths add up to more than the length of the environment contents) or the input path is not fully used (if their lengths add up to less).

    • 3. The preexisting path is reinstalled.

    • 4. The decoration automata move along the input paths, thus creating (and possibly using) the output paths. These output paths extend the current path (unless they are used).

    Some important points should be noted regarding the use of this environment:

    • If environment contents does not begin with \pgfpathmoveto, the last known point on the preexisting path is assumed as the starting point.

    • All except the last of any sequence of consecutive move-to commands in environment contents are discarded.

    • Any move-to commands at the end of environment contents are ignored.

    • Any close-path commands on the input path are interpreted as straight lines. Internally, something a little more complicated is going on, however, a closed path on the input path has no effect on the output path, other than causing the automaton to travel in a straight line towards the location of the last move-to command on the input path.

    • Although tangent computations for the input path work with the last point on the preexisting path, no automatic move-to operations are issued for the output path. If an output path starts with a line-to or curve-to when the existing path is empty, an appropriate move-to command should be inserted before the decoration starts.

    • If a decoration uses its own path, the first time this happens the preexisting path is part of the path that is used at this point.

    Before the automata start to “work on” their respective inputs paths, before code is executed. After the decoration automaton has finished, after code is executed.

    (-tikz- diagram)

    \usetikzlibrary {decorations,decorations.pathmorphing}
    \begin{tikzpicture}[decoration={segment length=5pt}]
    \draw [help lines] grid (3,2);
    \begin{pgfdecoration}{{curveto}{1cm},{zigzag}{2cm},{curveto}{1cm}}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcurveto
    {\pgfpoint{0cm}{2cm}}{\pgfpoint{3cm}{2cm}}{\pgfpoint{3cm}{0cm}}
    \end{pgfdecoration}
    \pgfusepath{stroke}
    \end{tikzpicture}

    When the lengths are evaluated, the dimension \pgfdecoratedremainingdistance holds the remaining distance on the entire decorated path, and \pgfdecoratedpathlength holds the total length of the path. Thus, it is possible to specify lengths like \pgfdecoratedpathlength/3.

    (-tikz- diagram)

    \usetikzlibrary {decorations,decorations.pathmorphing}
    \begin{tikzpicture}[decoration={segment length=5pt}]
    \draw [help lines] grid (3,2);
    \begin{pgfdecoration}{
    {curveto}{\pgfdecoratedpathlength/3},
    {zigzag}{\pgfdecoratedpathlength/3},
    {curveto}{\pgfdecoratedremainingdistance}
    }
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcurveto
    {\pgfpoint{0cm}{2cm}}{\pgfpoint{3cm}{2cm}}{\pgfpoint{3cm}{0cm}}
    \end{pgfdecoration}
    \pgfusepath{stroke}
    \end{tikzpicture}

    When before code is executed, the following macro is useful:

    • \pgfpointdecoratedpathfirst

    • Returns the point corresponding to the start of the current input path.

    When after code is executed, the following macro can be used:

    • \pgfpointdecoratedpathlast

    • Returns the point corresponding to the end of the current input path.

    This means that if decorations do not use their own path, it is possible to do something with them and continue from the correct place.

    (-tikz- diagram)

    \usetikzlibrary {decorations,decorations.pathmorphing}
    \begin{tikzpicture}
    \draw [help lines] grid (3,2);
    \begin{pgfdecoration}{
    {curveto}{\pgfdecoratedpathlength/3}
    {}
    {
    \pgfusepath{stroke}
    },
    {zigzag}{\pgfdecoratedpathlength/3}
    {
    \pgfpathmoveto{\pgfpointdecoratedpathfirst}
    \pgfdecorationsegmentlength=5pt
    }
    {
    \pgfsetstrokecolor{red}
    \pgfusepath{stroke}
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
    \pgfsetstrokecolor{black}
    },
    {curveto}{\pgfdecoratedremainingdistance}
    }
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathcurveto
    {\pgfpoint{0cm}{2cm}}{\pgfpoint{3cm}{2cm}}{\pgfpoint{3cm}{0cm}}
    \end{pgfdecoration}
    \pgfusepath{stroke}
    \end{tikzpicture}

    After the {decoration} environment has finished, the following macros are available:

    • \pgfdecorationpath

    • The output path. If the path is used, this macro contains only the last unused part of the output path.

    • \pgfpointdecoratedpathlast

    • The final point of the input path.

    • \pgfpointdecorationpathlast

    • The final point of the output path.

    The following style is executed each time a decoration is used. You may use it to set up default options for decorations.

    • /pgf/every decoration(style, initially empty)

    • This style is executed for every decoration.

  • \pgfdecoration{name}

  • environment contents

  • \endpgfdecoration

  • The plain- version of the {pgfdecorate} environment.

  • \startpgfdecoration{name}

  • environment contents

  • \stoppgfdecoration

  • The Cont version of the {pgfdecoration} environment.

For convenience, the following macros provide a “shorthand” for decorations (internally, they all use the {pgfdecoration} environment).

  • \pgfdecoratecurrentpath{name}

  • Decorate the preexisting path with the decoration name.

Both the above commands use the current definitions of the following macros:

  • \pgfdecoratebeforecode

  • Code executed as before code, see the description of \pgfdecoration.

  • \pgfdecorateaftercode

  • Code executed as after code, see the description of \pgfdecoration.

It may sometimes be useful to add an additional transformation for each segment of a decoration. The following command allows you to define such a “last minute transformation”.

103.5 Meta-Decorations

A meta-decoration provides an alternative way to decorate a path with multiple decorations. It is, in essence, an automaton that decorates an input path with decoration automatons. In general, however, the end effect is still that a path is decorated with other paths, and the input path should be thought of as being divided into sub-input-paths, each with their own decoration. Like ordinary decorations, a meta-decoration must be declared before it can be used.

103.5.1 Declaring Meta-Decorations
103.5.2 Predefined Meta-decorations

There are no predefined meta-decorations loaded with pgf.

103.5.3 Using Meta-Decorations

Using meta-decorations is “simpler” than using decorations, because you can only use one meta-decoration per path.

  • \begin{pgfmetadecoration}{name}

  • environment contents

  • \end{pgfmetadecoration}

  • This environment decorates the input path described in environment contents, with the meta-decoration name.

  • \pgfmetadecoration{name}

  • environment contents

  • \endpgfmetadecoration

  • The plain version of the {pgfmetadecoration} environment.

  • \startpgfmetadecoration{name}

  • environment contents

  • \stoppgfmetadecoration

  • The Cont version of the {pgfmetadecoration} environment.