PGF/TikZ Manual

# The TikZ and PGF Packages Manual for version 3.1.10

## The Basic Layer

#### 101Specifying Coordinates

##### 101.1Overview¶

Most pgf commands expect you to provide the coordinates of a point (also called coordinate) inside your picture. Points are always “local” to your picture, that is, they never refer to an absolute position on the page, but to a position inside the current {pgfpicture} environment. To specify a coordinate you can use commands that start with \pgfpoint.

##### 101.2Basic Coordinate Commands¶

The following commands are the most basic for specifying a coordinate.

• \pgfpoint{x coordinate}{y coordinate}

• Yields a point location. The coordinates are given as dimensions.

• \pgfpointorigin

• Yields the origin. Same as \pgfpoint{0pt}{0pt}.

• Yields a point location given in polar coordinates. You can specify the angle only in degrees, radians are not supported, currently.

If the optional y-radius is given, the polar coordinate is actually a coordinate on an ellipse whose $$x$$-radius is given by radius and whose $$y$$-radius is given by y-radius.

##### 101.3Coordinates in the XY-Coordinate System¶

Coordinates can also be specified as multiples of an $$x$$-vector and a $$y$$-vector. Normally, the $$x$$-vector points one centimeter in the $$x$$-direction and the $$y$$-vector points one centimeter in the $$y$$-direction, but using the commands \pgfsetxvec and \pgfsetyvec they can be changed. Note that the $$x$$- and $$y$$-vector do not necessarily point “horizontally” and “vertically”.

• \pgfpointxy{$$s_x$$}{$$s_y$$}

• Yields a point that is situated at $$s_x$$ times the $$x$$-vector plus $$s_y$$ times the $$y$$-vector.

• \pgfsetxvec{point}

• Sets that current $$x$$-vector for usage in the $$xyz$$-coordinate system.

Example:

• \pgfsetyvec{point}

• Works like \pgfsetxvec.

• This command is similar to the \pgfpointpolar command, but the radius is now a factor to be interpreted in the $$xy$$-coordinate system. This means that a degree of 0 is the same as the $$x$$-vector of the $$xy$$-coordinate system times radius and a degree of 90 is the $$y$$-vector times radius. As for \pgfpointpolar, a radius can also be a pair separated by a slash. In this case, the $$x$$- and $$y$$-vectors are multiplied by different factors.

##### 101.4Three Dimensional Coordinates¶

It is also possible to specify a point as a multiple of three vectors, the $$x$$-, $$y$$-, and $$z$$-vector. This is useful for creating simple three dimensional graphics.

• \pgfpointxyz{$$s_x$$}{$$s_y$$}{$$s_z$$}

• Yields a point that is situated at $$s_x$$ times the $$x$$-vector plus $$s_y$$ times the $$y$$-vector plus $$s_z$$ times the $$z$$-vector.

• \pgfsetzvec{point}

• Works like \pgfsetxvec.

Inside the $$xyz$$-coordinate system, you can also specify points using spherical and cylindrical coordinates.

• This command yields the same as

\pgfpointadd{\pgfpointpolarxy{degree}{radius}}{\pgfpointxyz{0}{0}{height}}



• This command yields a point “on the surface of the earth” specified by the longitude and the latitude. The radius of the earth is given by radius. The equator lies in the $$xy$$-plane.

##### 101.5Building Coordinates From Other Coordinates¶

Many commands allow you to construct a coordinate in terms of other coordinates.

###### 101.5.1Basic Manipulations of Coordinates¶
• \pgfpointadd{$$v_1$$}{$$v_2$$}

• Returns the sum vector $$\meta {v_1} + \meta {v_2}$$.

• \pgfpointscale{factor}{coordinate}

• Returns the vector $$\meta {factor}\meta {coordinate}$$.

• \pgfpointdiff{start}{end}

• Returns the difference vector $$\meta {end} - \meta {start}$$.

• \pgfpointnormalised{point}

• This command returns a normalised version of point, that is, a vector of length 1pt pointing in the direction of point. If point is the $$0$$-vector or extremely short, a vector of length 1pt pointing upwards is returned.

This command is not implemented by calculating the length of the vector, but rather by calculating the angle of the vector and then using (something equivalent to) the \pgfpointpolar command. This ensures that the point will really have length 1pt, but it is not guaranteed that the vector will precisely point in the direction of point due to the fact that the polar tables are accurate only up to one degree. Normally, this is not a problem.

###### 101.5.2Points Traveling along Lines and Curves¶

The commands in this section allow you to specify points on a line or a curve. Imagine a point “traveling” along a curve from some point $$p$$ to another point $$q$$. At time $$t=0$$ the point is at $$p$$ and at time $$t=1$$ it is at $$q$$ and at time, say, $$t=1/2$$ it is “somewhere in the middle”. The exact location at time $$t=1/2$$ will not necessarily be the “halfway point”, that is, the point whose distance on the curve from $$p$$ and $$q$$ is equal. Rather, the exact location will depend on the “speed” at which the point is traveling, which in turn depends on the lengths of the support vectors in a complicated manner. If you are interested in the details, please see a good book on Bézier curves.

• \pgfpointlineattime{time $$t$$}{point $$p$$}{point $$q$$}

• Yields a point that is the $$t$$th fraction between $$p$$ and $$q$$, that is, $$p + t(q-p)$$. For $$t=1/2$$ this is the middle of $$p$$ and $$q$$.

• \pgfpointlineatdistance{distance}{start point}{end point}

• Yields a point that is located distance many units away from the start point in the direction of the end point. In other words, this is the point that results if we travel distance steps from start point towards end point.

Example:

• \pgfpointarcaxesattime{time $$t$$}{center}{0-degree axis}{90-degree axis}{start angle}
{end angle}

• Yields a point on the arc between start angle and end angle on an ellipse whose center is at center and whose two principal axes are 0-degree axis and 90-degree axis. For $$t=0$$ the point at the start angle is returned and for $$t=1$$ the point at the end angle.

• \pgfpointcurveattime{time $$t$$}{point $$p$$}{point $$s_1$$}{point $$s_2$$}{point $$q$$}

• Yields a point that is on the Bézier curve from $$p$$ to $$q$$ with the support points $$s_1$$ and $$s_2$$. The time $$t$$ is used to determine the location, where $$t=0$$ yields $$p$$ and $$t=1$$ yields $$q$$.

###### 101.5.3Points on Borders of Objects¶

The following commands are useful for specifying a point that lies on the border of special shapes. They are used, for example, by the shape mechanism to determine border points of shapes.

• \pgfpointborderrectangle{direction point}{corner}

• This command returns a point that lies on the intersection of a line starting at the origin and going towards the point direction point and a rectangle whose center is in the origin and whose upper right corner is at corner.

The direction point should have length “about 1pt”, but it will be normalized automatically. Nevertheless, the “nearer” the length is to 1pt, the less rounding errors.

• \pgfpointborderellipse{direction point}{corner}

• This command works like the corresponding command for rectangles, only this time the corner is the corner of the bounding rectangle of an ellipse.

###### 101.5.4Points on the Intersection of Lines¶
• \pgfpointintersectionoflines{$$p$$}{$$q$$}{$$s$$}{$$t$$}

• This command returns the intersection of a line going through $$p$$ and $$q$$ and a line going through $$s$$ and $$t$$. If the lines do not intersection, an arithmetic overflow will occur.

###### 101.5.5Points on the Intersection of Two Circles¶
• \pgfpointintersectionofcircles{$$p_1$$}{$$p_2$$}{$$r_1$$}{$$r_2$$}{solution}

• This command returns the intersection of the two circles centered at $$p_1$$ and $$p_2$$ with radii $$r_1$$ and $$r_2$$. If solution is 1, the first intersection is returned, otherwise the second one is returned.

###### 101.5.6Points on the Intersection of Two Paths¶
• TikZ Library intersections

• \usepgflibrary{intersections} % and plain and pure pgf
\usepgflibrary[intersections] % Cont and pure pgf
\usetikzlibrary{intersections} % and plain when using TikZ
\usetikzlibrary[intersections] % Cont when using TikZ

This library defines the below command and allows you to calculate the intersections of two arbitrary paths. However, due to the low accuracy of , the paths should not be “too complicated”. In particular, you should not try to intersect paths consisting of lots of very small segments such as plots or decorated paths.

• \pgfintersectionofpaths{path 1}{path 2}

• This command finds the intersection points on the paths path 1 and path 2. The number of intersection points (“solutions”) that are found will be stored, and each point can be accessed afterward. The code for path 1 and path 2 is executed within a group and so can contain transformations (which will be in addition to any existing transformations). The code should not use the path in any way, unless the path is saved first and restored afterward. pgf will regard solutions as “a bit special”, in that the points returned will be “absolute” and unaffected by any further transformations.

• \pgfintersectionsolutions

• After using the \pgfintersectionofpaths command, this -macro will indicate the number of solutions found.

• \pgfpointintersectionsolution{number}

• After using the \pgfintersectionofpaths command, this command will return the point for solution number or the origin if this solution was not found. By default, the intersections are simply returned in the order that the intersection algorithm finds them. Unfortunately, this is not necessarily a “helpful” ordering. However the following two commands can be used to order the solutions more helpfully.

• \pgfintersectionsortbyfirstpath

• Using this command will mean the solutions will be sorted along path 1.

• \pgfintersectionsortbysecondpath

• Using this command will mean the solutions will be sorted along path 2.

##### 101.6Extracting Coordinates¶

There are two commands that can be used to “extract” the $$x$$- or $$y$$-coordinate of a coordinate.

• \pgfextractx{dimension}{point}

• Sets the -dimension to the $$x$$-coordinate of the point.

• \pgfextracty{dimension}{point}

• Like \pgfextractx, except for the $$y$$-coordinate.

• \pgfgetlastxy{macro for $$x$$}{macro for $$y$$}

• Stores the most recently used $$(x,y)$$ coordinates into two macros.

Since $$(x,y)$$ coordinates are usually assigned globally, it is safe to use this command after path operations.

##### 101.7Internals of How Point Commands Work¶

As a normal user of pgf you do not need to read this section. It is relevant only if you need to understand how the point commands work internally.

When a command like \pgfpoint{1cm}{2pt} is called, all that happens is that the two -dimension variables \pgf@x and \pgf@y are set to 1cm and 2pt, respectively. These variables belong to the set of internal pgf registers, see section 117 for details. A command like \pgfpathmoveto that takes a coordinate as parameter will just execute this parameter and then use the values of \pgf@x and \pgf@y as the coordinates to which it will move the pen on the current path.

Since commands like \pgfpointnormalised modify other variables besides \pgf@x and \pgf@y during the computation of the final values of \pgf@x and \pgf@y, it is a good idea to enclose a call of a command like \pgfpoint in a -scope and then make the changes of \pgf@x and \pgf@y global as in the following example:

Since this situation arises very often, the macro \pgf@process can be used to perform the above code:

• \pgf@process{code}

• Executes the code in a scope and then makes \pgf@x and \pgf@y global.

Note that this macro is used often internally. For this reason, it is not a good idea to keep anything important in the variables \pgf@x and \pgf@y since they will be overwritten and changed frequently. Instead, intermediate values can be stored in the -dimensions \pgf@xa, \pgf@xb, \pgf@xc and their y-counterparts \pgf@ya, \pgf@yb, \pgf@yc. For example, here is the code of the command \pgfpointadd: