8.5 tikz - A general-purpose graphics system
In the final section of this chapter we take a brief look at tikz with which you can create a large variety of graphics. Unlike graphic systems such as pstricks or METAPOST discussed in [55], it does not require an external PostScript processor1 — all drawing is done with TEX commands. This has the advantage that it works with all engines and that it integrates seamlessly into the LaTEX compilation and so allows creation of not only stand-alone graphics but also decorations, e.g., the tcolorbox frames discussed in Section 8.4, page ornaments, or arrows between various parts of a text, etc. It has the drawbacks that the compilation of complex plots and graphics can be slow and that the accuracy of calculations is limited and can even fail if they lead to dimensions larger than the maximal size supported by TEX.
The tikz package was originally written by Till Tantau and is now maintained by Henri Menke. Since the first version in 2005 the tikz ecosystem has grown so much that it is quite impossible to do tikz justice on a few pages — already the official manual of the package [189] has more than 1300 pages, and CTAN lists more than 150 packages enhancing or making use of tikz — and so it is unavoidable that the following descriptions have to leave out many points and oversimplify syntax and concepts of tikz.
The functionality covered by tikz is huge, but not everything is loaded by default. Additional libraries to enable certain features can be loaded with one or more declarations. This command takes a list of library names as its argument. Some of the examples in this section require loading libraries and so do a few in other chapters; e.g., Example 11-3-14 on page →II 162 that implements commutative diagrams with tikz.
8.5.1 Basic objects
The three basic objects in tikz are coordinates, which describe a position on the canvas on which the picture is drawn, paths between coordinates, and nodes that allow adding text to a picture.
The tikz package supports various coordinate systems like Cartesian, polar, or spherical coordinates, and new coordinate systems can be defined. Coordinates are coordinates always given in parentheses. Their general syntax is:
8.5.2 Transformations and other operations
The package tikz offers options to scale, shift, and rotate graphics or parts of them. It is important to understand that these transformations do not affect all elements. As the following example shows, neither the line width nor the nodes are scaled and rotated. This is by design to avoid a inconsequent mix of different font sizes and normally gives the expected result. However, it means that scaling a graphic can require adapting the placement of nodes to fit into the new dimensions. Also it is not always immediately clear whether the transformation applies to a certain dimension. In such cases you should check the documentation. Note in the example the use of the scope environment to keep the transformation local and that a node is rotated by using the key rotate in the node options.
8.5.3 Going further
This section left out many topics that can be handled by tikz: matrices, shadows, animations, transparency, pics, decorations, plots, trees, circuits, page ornaments, and the key management to name only a few. The documentation of the package [189] and the CTAN topic page on tikz [41] are important resources here.
The number of options and keys in tikz can be overwhelming: a nice help is the documentation VisualTikZ [38] by Jean Pierre Casteleyn: The PDF file contains many small examples that helps to identify the needed key combination for different use cases.