PGF offers an interface for placing arrow tips at the end of lines. The interface works as follows:
This is done once at the beginning of the document.
When an arrow kind has been installed and when PGF is about to stroke a path, the following things happen:
In the above description, there are a number of “appropriately.” The exact details are not quite trivial and described later on.
In PGF, arrows are “meta-arrows” in the same way that fonts in TEX are “meta-fonts.” When a meta-arrow is resized, it is not simply scaled, but a possibly complicated transformation is applied to the size.
A meta-font is not one particular font at a specific size with a specific stroke width (and with a large number of other parameters being fixed). Rather, it is a “blueprint” (actually, more like a program) for generating such a font at a particular size and width. This allows the designer of a meta-font to make sure that, say, the font is somewhat thicker and wider at very small sizes. To appreciate the difference: Compare the following texts: “Berlin” and “”. The first is a “normal” text, the second is the tiny version scaled by a factor of two. Obviously, the first look better. Now, compare “” and “Berlin”. This time, the normal text was scaled down, while the second text is a “normal” tiny text. The second text is easier to read.
PGF’s meta-arrows work in a similar fashion: The shape of an arrow tip can vary according to the line width of the arrow tip is used. Thus, an arrow tip drawn at a line width of 5pt will typically not be five times as large as an arrow tip of line width 1pt. Instead, the size of the arrow will get bigger only slowly as the line width increases.
To appreciate the difference, here are the latex and to arrows, as drawn by PGF at four different sizes:
Here, by comparison, is the same arrow when it is simply “resized” (as done by most programs):
As can be seen, simple scaling produces arrow tips that are way too large at larger sizes and way too small at smaller sizes.
To declare an arrow kind “from scratch,” the following command is used:
This command declares a new arrow kind. An arrow kind has two names, which will typically be the same. When the arrow tip needs to be drawn, the <arrow tip code> will be invoked, but the canvas transformation is setup beforehand to a rotation such that when an arrow tip pointing right is specified, the arrow tip that is actually drawn points in the direction of the line.
Naming the arrow kind. The <start name> is the name used for the arrow tip when it is at the start of a path, the <end name> is the name used at the end of a path. For example, the arrow kind that looks like a parenthesis has the <start name>( and the <end name>) so that you can say \pgfsetarrows{(-)} to specify that you want parenthesis arrows and both ends.
The <end name> and <start name> can be quite arbitrary and may contain spaces.
Basics of the arrow tip code. Let us next have a look at the <arrow tip code>. This code will be used to draw the arrow tip when PGF thinks this is necessary. The code should draw an arrow that “points right,” which means that is should draw an arrow at the end of a line coming from the left and ending at the origin.
As an example, suppose we wanted to declare an arrow tip consisting of two arcs, that is, we want the arrow tip to look more or less like the red part of the following picture:
|
We could use the following as <arrow tip code> for this:
|
Indeed, when the ... is set appropriately (in a moment), we can write the following:
|
As can be seen in the second example, the arrow tip is automatically rotated as needed when the arrow is drawn. This is achieved by a canvas rotation.
Special considerations about the arrow tip code. There are several things you need to be aware of when designing arrow tip code:
Instead of \pgfusepath, use the quick versions. Typically, you will use \pgfusepathqstroke, \pgfusepathqfill, or \pgfusepathqfillstroke.
Designing meta-arrows. The <arrow tip code> should adjust the size of the arrow in accordance with the line width. For a small line width, the arrow tip should be small, for a large line width, it should be larger. However, the size of the arrow typically should not grow in direct proportion to the line width. On the other hand, the size of the arrow head typically should grow “a bit” with the line width.
For these reasons, PGF will not simply executed your arrow code within a scaled scope, where the scaling depends on the line width. Instead, your <arrow tip code> is reexecuted again for each different line width.
In our example, we could use the following code for the new arrow tip kind arc' (note the prime):
|
|
However, sometimes, it can also be useful to have arrows that do not resize at all when the line width changes. This can be achieved by giving absolute size coordinates in the code, as done for arc. On the other hand, you can also have the arrow resize linearly with the line width by specifying all coordinates as multiples of \pgflinewidth.
The left and right extend. Let us have another look at the exact left and right “ends” of our arrow tip. Let us draw the arrow tip arc' at a very large size:
|
As one can see, the arrow tip does not “touch” the origin as it should, but protrudes a little over the origin. One remedy to this undesirable effect is to change the code of the arrow tip such that everything is shifted half an \arrowsize to the left. While this will cause the arrow tip to touch the origin, the line itself will then interfere with the arrow: The arrow tip will be partly “hidden” by the line itself.
PGF uses a different approach to solving the problem: The <extend code> argument can be used to “tell” PGF how much the arrow protrudes over the origin. The argument is also used to tell PGF where the “left” end of the arrow is. However, this number is important only when the arrow is being reversed or composed with other arrow tips.
Once PGF knows the right extend of an arrow kind, it can shorten lines by this amount when drawing arrows.
Here is a picture that shows what the visualizes the extends. The arrow tip itself is shown in red once more:
The <extend code> is normal TEX code that is executed whenever PGF wants to know how far the arrow tip will protrude to the right and left. The code should call the following two commands: \pgfarrowsrightextend and \pgfarrowsleftextend. Both arguments take one argument that specifies the size. Here is the final code for the arc'' arrow tip:
|
It is possible to declare arrow kinds in terms of existing ones. For these command to work correctly, the left and right extends must be set correctly.
This command can be used to create an alias (another name) for an existing arrow kind.
|
This command creates a new arrow kind that is the “reverse” of an existing arrow kind. The (automatically cerated) code of the new arrow kind will contain a flip of the canvas and the meanings of the left and right extend will be reversed.
|
This command creates a new arrow kind that combines two existing arrow kinds. The first arrow kind is the “innermost” arrow kind, the second arrow kind is the “outermost.”
The code for the combined arrow kind will install a canvas translation before the innermost arrow kind in drawn. This translation is calculated such that the right tip of the innermost arrow touches the right end of the outermost arrow. The optional <offset> can be used to increase (or decrease) the distance between the inner and outermost arrow.
|
In the star variant, the end of the line is not in the outermost arrow, but inside the innermost arrow.
|
This command is a shortcut for combining an arrow kind with itself.
|
This command is a shortcut for combining an arrow kind with itself and then again.
|
The following commands install the arrow kind that will be used when stroking is done.
Installs the given <start arrow kind> for all subsequent strokes in the in the current TEX-group. If <start arrow kind> is empty, no arrow tips will be drawn at the start of the last segment of paths.
|
Like \pgfsetarrowsstart, only for the end of the arrow.
|
Warning: If the compatibility mode is active (which is the default), there also exist old commands called \pgfsetstartarrow and \pgfsetendarrow, which are incompatible with the meta-arrow management.
Calls \pgfsetarrowsstart for <start kind> and \pgfsetarrowsend for <end kind>.
|
The following arrow tip kinds are always defined:
For further arrow tips, see page 176.