PGF/TikZ Manual | PGFplots | TikZ Editor

TikZ and PGF Manual

Libraries

71 Shape Library

71.1 Overview

In addition to the standard shapes rectangle, circle and coordinate, there exist a number of additional shapes defined in different shape libraries. Most of these shapes have been contributed by Mark Wibrow. In the present section, these shapes are described. Note that the library shapes is provided for compatibility only. Please include sublibraries like shapes.geometric or shapes.misc directly.

The appearance of shapes is influenced by numerous parameters like minimum height or inner xsep. These general parameters are documented in Section 17.2.3

In all of the examples presented in this section, the following shape example style is used:


\tikzset{
shape example/.style= {color = black!30,
draw,
fill = yellow!30,
line width = .5cm,
inner xsep = 2.5cm,
inner ysep = 0.5cm}
}
71.2 Predefined Shapes

The three shapes rectangle, circle, and coordinate are always defined and no library needs to be loaded for them. While the coordinate shape defines only the center anchor, the other two shapes define a standard set of anchors.

  • Shape circle

  • This shape draws a tightly fitting circle around the text. The following figure shows the anchors this shape defines; the anchors 10 and 130 are example of border anchors.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=circle,shape example] {Circle\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/above, east/right,
    mid west/right, mid/above, mid east/left,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, 10/right, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape rectangle

  • This shape, which is the standard, is a rectangle around the text. The inner and outer separations (see Section 17.2.3) influence the white space around the text. The following figure shows the anchors this shape defines; the anchors 10 and 130 are example of border anchors.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=rectangle,shape example] {Rectangle\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/above, east/right,
    mid west/right, mid/above, mid east/left,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, 10/right, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
71.3 Geometric Shapes
  • Shape diamond

  • This shape is a diamond tightly fitting the text box. The ratio between width and height is 1 by default, but can be changed by setting the shape aspect ratio using the following pgf key (to use this key in TikZ simply remove the /pgf/ path).

    • /pgf/aspect=value (no default, initially 1.0)

    • The aspect is a recommendation for the quotient of the width and the height of a shape. This key calls the macro \pgfsetshapeaspect.

    The following figure shows the anchors this shape defines; the anchors 10 and 130 are example of border anchors.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=diamond,shape example] {Diamond\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/above, east/right,
    mid/above,
    base/below,
    south west/below left, south/below, south east/below right,
    text/left, 10/right, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape ellipse

  • This shape is an ellipse tightly fitting the text box, if no inner separation is given. The following figure shows the anchors this shape defines; the anchors 10 and 130 are example of border anchors.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=ellipse,shape example] {Ellipse\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/above, east/right,
    mid west/right, mid/above, mid east/left,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, 10/right, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape semicircle

  • This shape is a semicircle, which tightly fits the node contents. This shape supports the rotation of the shape border, as described in Section 17.2.3. The anchors for the semicircle shape are shown below. Anchor 30 is an example of a border anchor.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=semicircle,shape border rotate=0,shape example, inner sep=1cm]
    {Semicircle\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {apex/above, arc start/below, arc end/below, chord center/below,
    center/above, base/below, mid/right, text/left,
    base west/below, base east/below, mid west/left, mid east/right,
    north/below, south/above, east/above, west/above,
    north west/above left, north east/above right,
    south west/below, south east/below, 30/right}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape regular polygon

  • This shape is a regular polygon, which, by default, is drawn so that a side (rather than a corner) is always at the bottom. This shape supports the rotation as described in Section 17.2.3, but the border of the polygon is always constructed using the incircle, whose radius is calculated to tightly fit the node contents (including any inner sep).

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \begin{tikzpicture}
    \foreach \a in {3,...,7}{
    \draw[red, dashed] (\a*2,0) circle(0.5cm);
    \node[regular polygon, regular polygon sides=\a, draw,
    inner sep=0.3535cm] at (\a*2,0) {};
    }
    \end{tikzpicture}

    If the node is enlarged to any specified minimum size, this is interpreted as the diameter of the circumcircle, that is, the circle that passes through all the corners of the polygon border.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \begin{tikzpicture}
    \foreach \a in {3,...,7}{
    \draw[blue, dashed] (\a*2,0) circle(0.5cm);
    \node[regular polygon, regular polygon sides=\a, minimum size=1cm, draw] at (\a*2,0) {};
    }
    \end{tikzpicture}

    There is a pgf key to set the number of sides for the regular polygon. To use this key in TikZ, simply remove the /pgf/ path.

    • /pgf/regular polygon sides=integer (no default, initially 5)

    The anchors for a regular polygon shape are shown below. The anchor 75 is an example of a border anchor.

    (-tikz- diagram)

    \usetikzlibrary {shapes.geometric}
    \Huge
    \begin{tikzpicture}
    \node[name=s, shape=regular polygon, shape example, inner sep=.5cm]
    {Regular Polygon\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {corner 1/above, corner 2/above, corner 3/left, corner 4/right, corner 5/above,
    side 1/above, side 2/left, side 3/below, side 4/right, side 5/above,
    center/above, text/left, mid/right, base/below, 75/above,
    west/above, east/above, north/below, south/above,
    north east/below, south east/above, north west/below, south west/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}

71.4 Symbol Shapes
  • Shape correct forbidden sign

  • This shape places the node inside a circle with a diagonal from the upper left to the lower right added. The circle is part of the background, the diagonal line part of the foreground path; thus, the diagonal line is on top of the text.

    (-tikz- diagram)

    \usetikzlibrary {shapes.symbols}
    \begin{tikzpicture}
    \node [correct forbidden sign,line width=1ex,draw=red,fill=white] {Smoking};
    \end{tikzpicture}

    The shape inherits all anchors from the circle shape.

71.5 Arrow Shapes
71.6 Shapes with Multiple Text Parts
  • Shape circle split

  • This shape is a multi-part shape consisting of a circle with a line in the middle. The upper part is the main part (the text part), the lower part is the lower part.

    (-tikz- diagram)

    \usetikzlibrary {shapes.multipart}
    \begin{tikzpicture}
    \node [circle split,draw,double,fill=red!20]
    {
    $q_1$
    \nodepart{lower}
    $00$
    };
    \end{tikzpicture}

    The shape inherits all anchors from the circle shape and defines the lower anchor in addition. See also the following figure:

    (-tikz- diagram)

    \usetikzlibrary {shapes.multipart}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=circle split,shape example] {text\nodepart{lower}lower};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/below, east/right,
    mid west/right, mid/above, mid east/left,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, lower/left, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape ellipse split

  • This shape is a multi-part shape consisting of an ellipse with a line in the middle. The upper part is the main part (the text part), the lower part is the lower part. The anchors for this shape are shown below. Anchor 60 is a border anchor.

    (-tikz- diagram)

    \usetikzlibrary {shapes.multipart}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=ellipse split,shape example] {text\nodepart{lower}lower};
    \foreach \anchor/\placement in
    {center/below, text/left, lower/left, 60/above right,
    mid/above, mid east/above, mid west/above,
    base/right, base east/left, base west/right,
    north/above, south/below, east/below, west/below,
    north east/above, south east/below, south west/below, north west/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape diamond split

  • This shape is a multi-part shape consisting of a diamond with a line in the middle. The upper part is the main part (the text part); the lower part is the lower part.

    (-tikz- diagram)

    \usetikzlibrary {shapes.multipart}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=diamond split,shape example,inner xsep=1cm] {text\nodepart{lower}lower};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/below, east/right, mid/above, base/below,
    south west/below left, south/below, south east/below right,
    text/left, lower/left, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
71.7 Callout Shapes

Callouts consist of a main shape and a pointer (which is part of the shape) which points to something in (or outside) the picture. The position on the border of the main shape to which the pointer is connected is determined automatically. However, the pointer is ignored when calculating the minimum size of the shape, and also when positioning anchors.

(-tikz- diagram)

\usetikzlibrary {shapes.callouts}
\begin{tikzpicture}[remember picture]
\node[ellipse callout, draw] (hallo) {Hallo!};
\end{tikzpicture}

There are two kinds of pointer: the “relative” pointer and the “absolute” pointer. The relative pointer calculates the angle of a specified coordinate relative to the center of the main shape, locates the point on the border to which this angle corresponds, and then adds the coordinate to this point. This seemingly over-complex approach means than you do not have to guess the size of the main shape: the relative pointer will always be outside. The absolute pointer, on the other hand, is much simpler: it points to the specified coordinate absolutely (and can even point to named coordinates in different pictures).

(-tikz- diagram)

\usetikzlibrary {shapes.callouts}
\begin{tikzpicture}[remember picture, note/.style={rectangle callout, fill=#1}]
\draw [help lines] grid(3,2);
\node [note=red!50, callout relative pointer={(0,1)}] at (3,1) {Relative};
\node [note=blue!50, callout absolute pointer={(0,1)}] at (1,0) {Absolute};
\node [note=green!50, opacity=.5, overlay,
callout absolute pointer={(hallo.south)}] at (1,2) {Outside};
\end{tikzpicture}

The following keys are common to all callouts. Please remember that the callout relative pointer, and callout absolute pointer keys take a different format for their value depending on whether they are being used in pgf or TikZ.

  • /pgf/callout relative pointer=coordinate (no default, initially \pgfpointpolar{315}{.5cm})

  • Sets the vector of the callout pointer ‘relative’ to the callout shape.

  • /pgf/callout absolute pointer=coordinate(no default)

  • Sets the vector of the callout pointer absolutely within the picture.

  • /tikz/callout absolute pointer=coordinate(no default)

  • The TikZ version of the callout absolute pointer key. Here, coordinate can be specified using the TikZ format for coordinates.

It is also possible to shorten the pointer by some distance, using the following key:

  • Shape cloud callout

  • This shape is a callout whose main shape is a cloud which fits the node contents. The pointer is segmented, consisting of a series of shrinking ellipses. This callout requires the shapes.callouts library (for the cloud shape). If this library is not loaded an error will result.

    (-tikz- diagram)

    \usetikzlibrary {shapes.callouts}
    \begin{tikzpicture}
    \node[cloud callout, cloud puffs=15, aspect=2.5, cloud puff arc=120,
    shading=ball,text=white] {\bf Imagine...};
    \end{tikzpicture}

    The cloud callout supports the absolute callout pointer, relative callout pointer and callout pointer shorten keys, as described above. The main shape can be modified using the same keys as the cloud shape. The following keys are also supported:

    • /pgf/callout pointer start size=value (no default, initially .2 of callout)

    • Sets the size of the first segment in the pointer (i.e., the segment nearest the main cloud shape). There are three possible forms for value:

      • A single dimension (e.g., 5pt), in which case the first ellipse will have equal diameters of 5pt.

      • Two dimensions (e.g., 10pt and 2.5pt), which sets the \(x\) and \(y\) diameters of the first ellipse.

      • A decimal fraction (e.g., .2 of callout), in which case the \(x\) and \(y\) diameters of the first ellipse will be set as fractions of the width and height of the main shape. The keyword of callout cannot be omitted.

    • /pgf/callout pointer end size=value (no default, initially .1 of callout)

    • Sets the size of the last ellipse in the pointer.

    • /pgf/callout pointer segments=number (no default, initially 2)

    • Sets the number of segments in the pointer. Note that pgf will happily overlap segments if too many are specified.

    The anchors for this shape are shown below (anchor 70 is an example of a border anchor). The pointer direction is ignored when placing anchors and the pointer anchor can only be used to position the shape when the relative anchor is specified. Note that the center of the last segment is drawn at the pointer anchor.

    (-tikz- diagram)

    \usetikzlibrary {shapes.callouts}
    \Huge
    \begin{tikzpicture}
    \node[name=s, shape=cloud callout, style=shape example, cloud puffs=11, aspect=1.5,
    cloud puff arc=120,inner xsep=.5cm, callout pointer start size=.25 of callout,
    callout pointer end size=.15 of callout, callout relative pointer={(315:4cm)},
    callout pointer segments=2] {Cloud Callout\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {puff 1/above, puff 2/above, puff 3/above, puff 4/below,
    puff 5/left, puff 6/below, puff 7/below, puff 8/right,
    puff 9/below, puff 10/above, puff 11/above, 70/right,
    center/above, base/below, mid/right, text/left,
    north/below, south/below, east/above, west/above,
    north west/left, north east/right,
    south west/below, south east/below,pointer/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
71.8 Miscellaneous Shapes
  • Shape cross out

  • This shape “crosses out” the node. Its foreground path are simply two diagonal lines between the corners of the node’s bounding box. Here is an example:

    (-tikz- diagram)

    \usetikzlibrary {shapes.misc}
    \begin{tikzpicture}
    \draw [help lines] (0,0) grid (3,2);
    \node [cross out,draw=red] at (1.5,1) {cross out};
    \end{tikzpicture}

    A useful application is inside text as in the following example:

    Cross (-tikz- diagram) out!

    \usetikzlibrary {shapes.misc}
    Cross \tikz[baseline] \node [cross out,draw,anchor=text] {me}; out!

    This shape inherits all anchors from the rectangle shape, see also the following figure:

    (-tikz- diagram)

    \usetikzlibrary {shapes.misc}
    \Huge
    \begin{tikzpicture}
    \node[name=s,shape=cross out,shape example] {cross out\vrule width 1pt height 2cm};
    \foreach \anchor/\placement in
    {north west/above left, north/above, north east/above right,
    west/left, center/above, east/right,
    mid west/right, mid/above, mid east/left,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, 10/right, 130/above}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
    \end{tikzpicture}
  • Shape strike out

  • This shape is identical to the cross out shape, only its foreground path consists of a single line from the lower left to the upper right.

    Strike (-tikz- diagram) out!

    \usetikzlibrary {shapes.misc}
    Strike \tikz[baseline] \node [strike out,draw,anchor=text] {me}; out!

    See the cross out shape for the anchors.