
Vector graphics in LaTeX. PGF / TikZ Package

At one time, I needed to find and study some flexible tool for creating high-quality vector images, because I already got crookedly scaled, raster-formatted pictures inserted with a terrible extension, spoiling the whole impression of the document, and doubling its size twice for one large picture with a rectangle and several captions to it. Available Embedded Features picturevery scarce; the PStricks package is oriented to the PostScript language (it doesn’t work with pdflatex, which I need), although there may be something that PGF cannot; MetaPost system is perhaps the most powerful of all in this area, but it operates using a separate interpreter with all the ensuing consequences. Thus, the choice fell on PGF / TikZ .

The main purpose of this article is the presence of a sufficient number of links and a small number of examples with descriptions in Russian. In most cases, this is enough to understand whether you need it or not.
It’s very nice when for an instrument there is an exhaustive list of examples of what it can do and howto achieve this. For the package under discussion, this list can be viewed by typing “pgf examples” in the search engine and clicking on the first link. Here all the examples are in one list: www.texample.net/tikz/examples/all . It makes no sense to try to describe everything a package is capable of. Perhaps I will focus on the basic concepts.
The article discusses the use of pgf / tikz together with the LaTeX macro package, as the most common, but pgf / tikz also works with others. The text of the article is mainly based on the TikZ & PGF manual . Manual for Version 2.00(560 p.). Some examples are taken from it. The book also has a small but useful chapter, Guidelines on Graphics (p. 65), devoted to general principles for working with documents with vector graphics, including examples of how to and how not to do it; it should be addressed regardless of whether you will use pgf / tikz or something else in the future.
Installation
On GNU / Linux, you must install the pgf package (and the dependent xcolor version> = 2.00). If this is not the case, it is enough to unzip the source (locally or globally) and make texhash (see the manual for details). On Windows, pgf / tikz is part of the MiKTeX distribution , so nothing extra is required.
Broadcast
To get pdf use
pdflatex file.tex
To get ps, we use two commands:
latex file.tex
dvips file.dvi
Also interesting is the ability to convert to HTML and SVG.
Using
To use the basic features of the package, you must connect it in the preamble with the command
\usepackage{tikz}
To insert pgf / tikz commands, either a command
\tikz
with one argument is used (usually to insert a picture within a line) or the environment . If you need to specify several drawing commands in the argument k , they are surrounded by curly braces ( ). Each command ends with a semicolon.\begin[options]{tikzpicture}...\end{tikzpicture}
\tikz
\tikz[options]{...}
\documentclass{memoir}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\tikz{\draw (-1,-1) -- (1,1); \path[fill=green!80!blue,draw=red] (0,0) circle (7mm);}
\end{document}

It is very convenient that you do not need to specify the size of the canvas. The program itself will calculate it for you.
Points
Points are given absolutely or relatively. Examples of Cartesian and polar coordinates:
(2,0)
- a point with the given coordinates in the current units of measurement (by default - 1 cm);(2cm,-3pt)
- 2 cm in the direction of Ox, –3 points in the direction of Oy;(30:5cm)
- a point at a distance of 5 cm from the current position in the direction of 30 degrees;+(2,0)
- a point 2 units to the right from the current position (the current position does not change);++(2,0)
- a point 2 units to the right from the current position, which becomes the new current position.
\tikz \draw (0,0) -- +(1,0) -- +(0,1) -- +(1,1);
\tikz \draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(1,1);
\tikz \draw (1,0) -- (0,0) -- (30:1);

Barycentric coordinates, a coordinate system of nodes (see below), and a tangential system can also be used.
The intersection coordinate system allows you to operate with intersection coordinates (however, so far only for combinations of lines and circles). To do this, the first and second objects (or lines with their coordinates) and the intersection number (when there are several) are set.
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) coordinate (A) -- (3,2) coordinate (B)
(1,2) -- (3,0);
\fill[red] (intersection of A--B and 1,2--3,0) circle (2pt);
\end{tikzpicture}

You can specify coordinates using relative or absolute length or projection modifiers. This is done in the following ways:
coordinate!number!angle:second_coordinate
coordinate!dimension!angle:second_coordinate
coordinate!projection_coordinate!angle:second_coordinate
For instance,
(1,2)!.25!(3,4)
means the coordinate that is one quarter of the way from (1,2) to (3,4)(1,2)!1cm!(3,4)
means the coordinate that is 1 cm from the point (1,2) on the line (1,2) - (3,4)(1,2)!(0,5)!(3,4)
means the coordinate, which is the projection of the point (0,5) on the line (1,2) - (3,4)
Trajectories
A trajectory is a sequence of straight and curved lines. They are created by the team
\path
. If it is called without arguments, then nothing will be drawn, which is not very interesting. The arguments may be draw
, fill
, shade
, clip
or any combination thereof. Instead \path[draw]
, \path[fill]
, \path[shade,draw]
, ... you can use abbreviations \draw
, \fill
, \shadedraw
. The basic command for creating lines is
(a) -- (b)
where (a)
and (b)
are some points. The cubic Bezier curve is defined by the command
(a) .. controls (x) and (y) .. (b)
, where (a)
and (b)
are some points, (x)
and (y)
are reference points that affect the shape of the curve. If you and (y)
skip a part , it is considered that (y)
= (x)
.Graphics Options
They are indicated in square brackets (as optional arguments to LaTeX) in the form of sets of key-value:
key=value
. Eg \tikz \draw[line width=2pt,color=red] (1,0) -- (0,0) -- (1,0) -- cycle;
. Sometimes a part key=
can be omitted if the key for a given value is interpreted unambiguously.Knots
Text and captions are added to the picture as nodes. They are attached to the current position on the plane (in space), or explicitly attached to some point. As parameters, you can specify the anchor of the node’s binding (where it will be relative to the point: right, left, top right, etc.), the shape of the node (circle, square, ellipse, ...), the way it is displayed (draw, fill) other.
\tikz \draw (1,1) node {text} -- (2,2);

\begin{tikzpicture}[line width=2pt]
\draw (0,0) node [below left] {$A$} --
(1,0) node [below right] {$B$} --
(1,1) node [above right] {$C$} --
(0,1) node [above left] {$D$} --
cycle;
\end{tikzpicture}

Scopes
Using the environment,
scope
you can create scope with your own parameters; the rest are inherited from containing this. In fact, the environment tikzpicture
behaves similarly scope
. With certain limitations, scope can also be used when drawing a path.\tikz \draw (0,0) -- (1,1)
{[rounded corners] -- (2,0) -- (3,1)} --
(3,0) -- (2,1);

Styles
Styles are indicated when listing graphic parameters. There is a set of already defined ones that can be redefined. You can define your own.
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid +(2,2);
\draw[help lines] (3,0) grid +(2,2);
\end{tikzpicture}

Calculations
When connecting the calc library with the command in the preamble
\usetikzlibrary{calc}
You can use some mathematical calculations to determine the coordinates, for example,
\begin{tikzpicture}[scale=0.5]
\draw[help lines] (0,0) grid (4,3);
\fill [red] ($2*(1,1)$) circle (2pt);
\fill [green] (${1+1}*(1,.5)$) circle (2pt);
\fill [blue] ($cos(0)*sin(90)*(1,1)$) circle (2pt);
\fill [black] (${3*(4-3)}*(1,0.5)$) circle (2pt);
\end{tikzpicture}

Conclusion
The features of the package can be listed for a very long time (for example, I did not mention anything about graphs, trees, and chains), but such a listing does not belong in one article, especially since there is the aforementioned site and an excellent guide with comprehensive examples of using each goodness of the pgf / package tikz. Whether or not to use it is up to you. I have been working with him for less than six months (and I learned about him not much earlier), but I already actively use and do not regret it; in many cases, he greatly saved time and effort. If the people have an interest, the story can be continued ;-)
PS.
Images were made as follows:
$ latex qq.tex && dvips -K -o qq.ps qq.dvi && convert -antialias -seed 0 -density 200 -trim qq.ps qq.png
The highlighting was done by exporting from gvim ( emacs theme ).