PGF/TikZ Manual

The TikZ and PGF Packages
Manual for version 3.1.10

The Basic Layer

101 Specifying Coordinates

101.1 Overview

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.2 Basic Coordinate Commands

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

  • \pgfpointorigin

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

101.3 Coordinates 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”.

  • \pgfsetyvec{point}

  • Works like \pgfsetxvec.

  • \pgfpointpolarxy{degree}{radius/y-radius}

  • 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.

    (-tikz- diagram)

    \draw[help lines] (0,0) grid (3,2);

    \foreach \angle in {0,10,...,90}
101.4 Three 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.

  • \pgfsetzvec{point}

  • Works like \pgfsetxvec.

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

101.5 Building Coordinates From Other Coordinates

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

101.5.1 Basic Manipulations of Coordinates
  • \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.

    (-tikz- diagram)

101.5.2 Points 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.

101.5.3 Points 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.

101.5.4 Points on the Intersection of Lines
101.5.5 Points on the Intersection of Two Circles
101.5.6 Points 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.

    (-tikz- diagram)

    \usetikzlibrary {intersections}
    \foreach \s in {1,...,\pgfintersectionsolutions}
    • \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.6 Extracting 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.

    %% \mydim is now 2cm

  • \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.

    Macro \(x\) is ‘56.9055pt’ and macro \(y\) is ‘113.81102pt’.

    Macro $x$ is `\macrox' and macro $y$ is `\macroy'.

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

101.7 Internals 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:

{ % open scope
\global\pgf@x=\pgf@x % make the change of \pgf@x persist past the scope
\global\pgf@y=\pgf@y % make the change of \pgf@y persist past the scope
% \pgf@x and \pgf@y are now set correctly, all other variables are
% unchanged

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: