TikZ is a LaTeX package for programically drawing pictures. It is a macro extension over PGF. The drawings can be of high quality and the integration with LaTeX provides advantages that are hard to find in other approaches, namely:

  • The drawing uses the same font as the docuemnt,
  • it can reference and link to parts of the text,
  • it can be parameterized when called.

A note about this page / How to use / Page overview

This page is meant to work as a tutorial and as a quick reference. All the examples have full compilable codes made as a standalone document in LaTeX. It is aimed to provide all the necessary detail to be able to draw efficiently in TikZ. Everything is on a single page so use Ctrl+f to navigate. The section headings are often split with / to catch a concept with many names.

The document is split into several major parts

  • Basics – Introduces basic notation of TikZ and shows how to just draw some basic geometry on the screen and how to manipulate the drawing.
  • Decoration – More advanced styles to draw lines, fills, arrows, and more.
  • Automation – We can build loops and functions to draw intricate designs. Concepts in this section will be easily understood by programmers.
  • Libraries – List of most usefull functionalities provided by various tikzlibraries.

The picture examples are compilable on their own but for clarity we often include guiding lines and other marks. The setup is divided from the example command by blank lines. Example codes have the following structure.

general setup
things we draw for clarity of the picture

the example command

general setup

All the examples with pictures are standalone LaTeX files that are compiled with latexmk -pdf main.tex, converted to svg files with inkscape -o main.svg main.pdf, and scaled up to be well visible.

Contents

Basics & Drawing simple geometric images

In this section, we give an exhaustive introduction to TikZ. We show how to make TikZ work in the LaTeX document, how to draw basic shapes, and rudimentary styles such as color, thickness, and dashing.

  • to find advanced styling optoins see section Decoration
  • to draw more complex pictures, see section Automation

Add a TikZ image to a document

In your LaTeX document include \usepackage{tikz} before \begin{document}.

\usepackage{tikz}

Some functionality requires loading additional packages or TikZ libraries with \usetikzlibrary{<lib>}. As our examples always show complete source code, you may notice these commands in some of them.

A TikZ image is represented with a TikZ source code which is its own language. Single-command pictures can be drawn with \tikz command, however, we shall exclusively use tikzpicture environment as follows.

\begin{tikzpicture}
    \node[gray] (A) at (0,2) {learn};
    \node[blue] (B) at (0,0) {TikZ};
    \draw[|->]  (A) -- (B);
\end{tikzpicture}

One can either include this code directly in a LaTeX document (which provides the advantages mentioned at the beginning). For example, if the file was in images/tikz_test.tex, then we would paste the file’s contents with input command as follows.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\input{images/tikz_test.tex} % the file would include source code from the previous example
\end{document}

Or we can compile the source code as a standalone document and include it as PDF (which sacrifices many advantages of TikZ). We use this method to present single pictures that are shown in this tutorial.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\node[gray] (A) at (0,2) {learn};
\node[blue] (B) at (0,0) {TikZ};
\draw[|->]  (A) -- (B);

\end{tikzpicture}
\end{document}
standalone

It is common to enclose the image into figure to give it caption and label. So within a LaTeX document we would see the following code.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[scale=0.6]
        <tikz code here>
    \end{tikzpicture}
    \caption{Example picture in TikZ}%
    \label{fig:tikz_example}
\end{figure}

What will we learn

The example below shows simple image that uses all of the basic concepts.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\fill[blue!20] (0,0) circle (1);
\draw[->] (0,0) -- (45:1);
\node[below] at (0,0) {drawing};

\end{tikzpicture}
\end{document}
drawing_simple
  • \fill, \draw, and \node are drawing commands
  • blue!20, ->, and below alter styles of how things are drawn
  • (0,0), (1), and (45:1) denote positions or lengths
  • {drawing} contains the displayed text

We shall now delve deep into each of these concepts.

Lengths

The numbers (like 0,1,2,3.14) are in default units if not mentioned explicitly. Lengths can be also in millimeters, points, ex, em, and inches (e.g. 1cm, 2mm, 3pt, 4.5in).

  • cm is the default length unit
  • angles (+-360) are the default rotation unit
  • in some parameters (like xshift) default unit is weird; mention it explicitly to get correct result

We compare different units in the following example. There is no need to understand the code completely.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-2.7,-6.3) grid (0.7,-0.7);
\foreach[count=\i] \len in {mm,cm,pt,ex,em,in} {
    \node[anchor=west] at (0,-\i) {\len};
    \draw[|-|,blue] (0,-\i) -- +(-1\len,0);
}
\end{tikzpicture}
\end{document}
lengths

Cartesian and Polar coordinates

Cartesian coordinates are denoted as (x,y), polar as (angle:distance). There are defaults one should know.

  • positive axis are right and up
  • angles are in degrees (not radians)
  • angle 0 is to the right
  • angle increases in counter-clockwise direction

These defaults are clearly visible in the following examples. Ignore the first and the last part of the code that is only for setup and clear explanation.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[help lines]
\draw (-0.5,-0.5) grid (2.5,1.5);
\node at (0,0) {o};
\draw[|-|] (0,-0.1) --node[below]{x=2} (2,-0.1);
\draw[|-|] (2.1,0) --node[right]{y=1} (2.1,1);
\end{scope}

% note the direction of positive x and y
\node at (2,1) {u};

\end{tikzpicture}
\end{document}
coord_cartesian
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[help lines]
\draw (-0.5,-0.5) grid (2.5,1.5);
\draw (0,0) -- (30:2);
\node at (0,0) {o};
\draw[|-|] (0,-0.1) --node[below]{dist=2} (2,-0.1);
\draw[|->|] (2.1,0) arc (0:30:2.1);
\node at (1.45,0.2) {deg=30};
\end{scope}

% angle 30, distance 2
\node at (30:2) {u};

\end{tikzpicture}
\end{document}
coord_polar

Relative coordinates

Coordinates can be given in

  • absolute value as (x,y) (origin at (0,0)),
  • relative value +(x,y) (origin is the first point),
  • additive relative value ++(x,y) (origin at the last point).

See the difference between relative and accumulative coordinates in the examples below.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (13.9,5.9) grid (18.1,8.1);

\node (R) at (14,6) {rel}; % relative coordinates
\draw[green] (R) --  +(0,1) --  +(1,1) --  +(1,0);
\node (A) at (16,6) {acc}; % accumulative relative
\draw[blue]  (A) -- ++(0,1) -- ++(1,1) -- ++(1,0);

\end{tikzpicture}
\end{document}
coord_cartesian_comparison
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (13.7,16.9) grid (15.6,19.1);

\node (R) at (14,18) {rel}; % relative polar
\draw[green] (R) --  +(60:1) --  +(-90:1);
\node (A) at (15,18) {acc}; % accumulative polar
\draw[blue]  (A) -- ++(60:1) -- ++(-90:1);

\end{tikzpicture}
\end{document}
coord_polar_comparison

Draw and fill command

To put something on the screen the base command is \path[<options>] <path syntax> ; which has many draw modes that are usually shortcutted.

  • \draw = \path[draw]
  • \fill = \path[fill]
  • \filldraw = \path[fill,draw]

(For advanced stuff, there also exists \shade, \shadedraw, \clip, \pattern, and \useasboundingbox.)

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[fill=gray,thick]
\draw[help lines] (0.9,0.9) grid (4.1,3.1);

\draw      (1,1) -- +(1,2) -- +(1,1);
\fill      (2,1) -- +(1,2) -- +(1,1);
\filldraw  (3,1) -- +(1,2) -- +(1,1);

\end{tikzpicture}
\end{document}
path

Lines and curves

We may connect two endpoints with (A) <connection> (B) in various types of lines.

  • -- – simple line segment
  • -| and |- – orthogonal lines
  • to[<style>] – allows bending and many other features
  • .. controls (A) [and (B)]* .. – Bezier curves
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
\draw[help lines] (-0.1,-0.1) grid (3.1,6.1);

% line segment
\draw (0,5) -- +(3,1);
% polyline
\draw (0,4) -- ++(1,0) -- ++(1,1) -- ++(1,0);
% orthogonal line
\draw (0,3) -| ++(1.5,0.5) -| ++(1.5,0.5);
% bent line
\draw (0,2) to[bend right=20] +(3,1);
% by in- and out-degrees
\draw (0,1) to[out=45,in=225] +(3,1);
% Bezier curve
\draw (0,0) .. controls +(1,0) and +(-1,0) .. +(3,1);

\end{tikzpicture}
\end{document}
curves

Basic shapes

We can draw straight lines, open and closed polylines, rectangles, circles, ellipses, and curves.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[fill=gray,thick]
\draw[help lines] (-0.1,-0.1) grid (3.1,5.1);

% draws a polyline
\filldraw (0,5) -- (1,4) -- (2,5) -- (3,4);
% draws a closed polyline
\filldraw (0,3) -- (1,3) -- (2,4) -- cycle;
% rectangle, circle, ellipse
\filldraw (2,2) rectangle (3,3);
\filldraw (1,2) circle (0.45);
\filldraw (1,1) ellipse (0.2 and 0.45);

\end{tikzpicture}
\end{document}
shapes

Colors and opacity

  • [color=<color>] – general color (contains everything)
  • [draw=<color>] – line color
  • [fill=<color>] – fill color
  • [text=<color>] – text color

There are many ways to define a <color>.

  • predefined color name <name> – e.g. red, see palette below
  • whitened color <name>!<percent>red!60 is 60% red and 40% white
  • 2-color mix <name>!<percent>!<name>red!60!blue is 60% red and 40% blue
  • weighted mix {rgb:<name>,<weight>[;<n>,<w>]*}rgb:red,3;blue,2 is 3 parts red and 2 parts blue
  • classical RGB {rgb,255:red,<r>;green,<g>;blue,<b>}{rgb,255:red,153;green,0;blue,102}
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
\draw[help lines] (-0.1,-0.1) grid (3.1,7.1);

\draw (0,6) -- +(3,1); % black by default
\draw[red] (0,5) -- +(3,1); % shortcut for draw=red
\draw[draw=red] (0,4) -- +(3,1);
\draw[draw opacity=0.2] (0,3) -- +(3,1);
% different ways to mix: 60% red and 40% blue
\draw[draw=red!60!blue] (0,2) -- +(3,1);
\draw[draw={rgb:red,3;blue,2}] (0,1) -- +(3,1);
\draw[draw={rgb,255:red,153;green,0;blue,102}] (0,0) -- +(3,1);

\end{tikzpicture}
\end{document}
color_draw

For <name> we can use predefined colors from the following palette.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\foreach[count=\i] \x in {red, brown,
    orange, yellow, olive, green, lime,
    teal, cyan, blue, violet, purple,
    magenta, pink, white, lightgray,
    gray, darkgray, black}{
    \filldraw[fill=\x] (0,-.5*\i+.5) rectangle (-1,-.5*\i);
    \node[anchor=south west] at (0,-.5*\i) {\x};
}

\end{tikzpicture}
\end{document}
color_palette

Custom color names can be defined with \colorlet as

\colorlet{mycustomcolor}{<color>}

Each part is accompanied with respective opacity parameter. Use transparency group to turn off opacity/transparency interaction between objects (e.g. object’s line and fill).

  • draw opacity
  • fill opacity
  • text opacity
  • opacity is a shortcut for draw opacity and fill opacity
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[draw=green,fill=blue,very thick]
\draw[black] (0,-.1) -- (.3,1.2);

\filldraw[opacity=1]  (0,.8) rectangle ++(.3,.3);
\filldraw[opacity=.5] (0,.4) rectangle ++(.3,.3);
\begin{scope}[opacity=.5,transparency group]
    \filldraw (0,0) rectangle ++(.3,.3);
\end{scope}

\end{tikzpicture}
\end{document}
opacity

Thickness

  • line width=0.1pt = ultra thin
  • line width=0.2pt = very thin
  • line width=0.4pt = thin
  • line width=0.6pt = semithick
  • line width=0.8pt = thick
  • line width=1.2pt = very thick
  • line width=1.6pt = ultra thick
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-0.1,-0.1) grid (3.1,7.1);

\draw[ultra thin]  (0,6) -- +(3,1);
\draw[very thin]   (0,5) -- +(3,1);
\draw[thin]        (0,4) --node[sloped,below]{default} +(3,1);
\draw[semithick]   (0,3) -- +(3,1);
\draw[thick]       (0,2) -- +(3,1);
\draw[very thick]  (0,1) -- +(3,1);
\draw[ultra thick] (0,0) -- +(3,1);

\end{tikzpicture}
\end{document}
thickness

Dash style / Dash patterns

Aside from the default solid there are also dotted, dashed, and dashdotted dash patterns. Each can be modified with densly and loosely.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]

\foreach[count=\i] \x in {
    solid,
    dotted, densely dotted, loosely dotted,
    dashed, densely dashed, loosely dashed,
    dashdotted, densely dashdotted, loosely dashdotted}{
    \draw[\x] (0,-\i) --node[sloped,below]{\x} +(4,.25);
}
% custom dash pattern
\draw[dash pattern=on \the\pgflinewidth
    off 2pt on 2pt off 2pt on 3pt off 2pt on 4pt off 2pt
] (0,-11.5)
--node[sloped,below]{custom (see source code)}
+(4,.25);

\end{tikzpicture}
\end{document}
dash_patterns

Custom dash style can be achieved with

dash pattern=on <length> off <length> [on <length> off <length>]*

where \the\pgflinewidth is the line width.

Arrows

Selecting arrow types is done with <arrow>-<arrow> parameter. Default is without arrows -.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw[gray!50,very thin] (-0.1,-0.1) grid (2.1,4.1);

\draw[-]      (0,3) -- +(2,1);
\draw[->]     (0,2) -- +(2,1);
\draw[|-|]    (0,1) -- +(2,1);
\draw[><<->>] (0,0) -- +(2,1);

\end{tikzpicture}
\end{document}
arrows

Find more complex arrows in Decoration section.

Bending edges

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-0.1,-0.1) grid (6.1,4.1);

\foreach \i in {0,10,...,90}{
    \draw (0,2) edge[bend left=\i]node[pos=.75-\i/160]{\i} +(6,0);
}
\node[below] at (3,2){bend left};

\foreach \i in {0,10,...,90}{
    \draw (0,0) to[out=\i]node[pos=.7-\i/180]{\i} +(6,0);
}
\node[below] at (3,0){out};

\end{tikzpicture}
\end{document}
bending

Labels / Node basics

We can place text into a picture with using nodes or labels. Nodes are quite powerful and can be placed as follows.

  • single node \node[<options>] at (<position>) {<label>};
  • on an edge – (a) --<node> (b)

Most useful options are

  • below, above, right, and left – positions the node
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\node at (1,3) {$\times$};
\node[text=blue,right] at (1,3) {node text};
\node[text=red,below] at (1,3) {below};
\draw (0,1.6) --node{edge text} +(3,1);
\draw (0,1) --node[sloped,below]{sloped text} +(3,1);

\end{tikzpicture}
\end{document}
labels

Intersection points

We can get a list of intersection of two arbitrary paths using intersections library. To do so, we need to name the paths.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    every node/.style={fill=red,circle,inner sep=1pt,}]
\clip (.8,.8) rectangle (3.4,5.2);
\draw[help lines] (0,0) grid (4,6);

\usetikzlibrary{intersections}

\draw[name path=P] (1,4) -- +(2,1);
\draw[name path=Q] (2,4) -- +(-1,1);
\path[name intersections={of=P and Q,by=W}];
\node[label=90:$E$] at (W) {};

\draw[name path=A] (0,1)..controls ++(6.5,0) and ++(-7,0)..(4,3);
\draw[name path=B] (1,0)..controls ++(0,9) and ++(0,-9)..(3,4);
\fill[name intersections={of=A and B, name=C, total=\t}]
    \foreach \s in {1,...,\t}{
        (C-\s) node[label=45:\s] {}
    };

\end{tikzpicture}
\end{document}
intersections

Nodes, coordinates, and how to position everything

In general, nodes can be either defined separately or they can be expanded from a coordinate. Nodes on coordinates are drawn directly over the position. Explicit nodes with edges drawn after are joined on their border. Syntax is:

  • (<coordinate>) node[<style>] (<name>) {<label>}
  • \node[<style>] (<name>) at (<coordinate>) {<label>};

Nodes are also commonly used to place labels.

Node \node is an abbreviation of \path node. For a position without graphics use \coordinate (abbreviation of \path coordinate).

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\draw[<->] (0.0,2.0) node[draw=red,circle] () {}
        -- (+.1,1.5) node[draw=red,circle] () {}
        -- (-.1,1.0) node[draw=red,circle] () {}
        -- (+.1,0.5) node[draw=red,circle] () {}
        -- (0.0,0.0) node[draw=red,circle] () {};

\node[draw=red,fill=yellow] at (1,2) (c) {color};
\node[draw,circle]          at (1,0) (d) {d};
\draw[<->] (d) --node[sloped,below]{label} (c);

\end{tikzpicture}
\end{document}
nodes

Positioning nodes

Each node is placed on a concrete place in space. There are three things that play role in final placement of a node.

  • node position – gives a position in space
  • node anchor – the node is placed so that its anchor is on the position
  • transformations – moves/rotates/scales a node that is already placed
\node[<style>] (<name>) at (<position>) {<label>}; % a standalone node
\node {<label>}; % minimal node; <name> and <position> may be placed anywhere within the command
\draw (a) --node[<style>] (<name>) at (<position>) {<label>} (b); % node placed on an edge (a)--(b)
\draw (a) -- (b) node[<style>] (<name>) at (<position>) {<label>}; % node placed on a coordinate (b)

Node position

To place a node we define its position. The basic way is to define its absolute or relative position – see Cartesian and Polar coordinates section. We also present some advanced ways to define positions.

  • (1,2), +(1,2), and ++(1,2) – absolute and relative positions (see their section)
  • ($(a)!.8!(b)$) – defines position 80% of a way from (a) to (b); requires calc library
  • ($ ... computation ... $)calc library allows arbitrary computation
  • \node[left of=a] – relative to other node
  • (x) -- node[below] (y) – relative to an edge

Node anchors

Nodes can be positioned nicely by using anchors. Anchors are named as:

  • north, south, east, west and their combinations
  • angles (ranges -360 to 360 and beyond)

E.g. anchor=west sets up the node to be alligned to the left. See anchors visually in the following example.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[fill opacity=.3,text opacity=1]

\node[draw,fill=blue] (A) at (0,4) {Anchors};

\foreach \a in {north,east,south,west,
    north east,north west,south west,south east}{
    \node[fill=red,circle,label=\a:\a] at (A.\a) {};
}

\node[draw,fill=green] (B) at (0,0) {Anchors};

\foreach[count=\i] \a in {0,20,...,360}{
    \draw (B) -- +(\a:1.4+.03*\i) node[anchor=180+\a]{\a};
    \node[fill=red,circle] at (B.\a) {};
}

\end{tikzpicture}
\end{document}
nodes_anchors

Transformations

The manipulations are made with respect to the origin which is by default (0,0).

Shift / Move

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[gray!50]
\draw[very thin] (-0.1,-1.1) grid (2.1,1.6);
\draw[dotted] (0,-1) -- (1,1) -- (1,0);
\draw[->] (0,-1) -- (1,-0.5);
\draw[->] (1,1)  -- (2,1.5);
\draw[->] (1,0)  -- (2,0.5);
\end{scope}

\draw[shift={(1,0.5)}] (0,-1) -- (1,1) -- (1,0);

\end{tikzpicture}
\end{document}
shift

Scale

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[gray!50]
\node at (0,0) {o};
\draw[very thin] (-0.1,-1.5) grid (1.5,1.5);
\draw[dotted] (0,-1) -- (1,1) -- (1,0);
\draw[->] (0,-1) -- (0,-1.4);
\draw[->] (1,1) -- (1.4,1.4);
\draw[->] (1,0) -- (1.4,0);
\end{scope}

\draw[scale=1.4] (0,-1) -- (1,1) -- (1,0);

\end{tikzpicture}
\end{document}
scale

Rotate

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[gray!50]
\node at (0,0) {o};
\draw[very thin] (-0.1,-1.1) grid (1.1,1.5);
\draw[dotted] (0,-1) -- (1,1) -- (1,0);
\draw[->] (0,-1) arc (270:300:1);
\draw[->] (1,1) arc (45:75:1.41421);
\draw[->] (1,0) arc (0:30:1);
\end{scope}

\draw[rotate=30] (0,-1) -- (1,1) -- (1,0);

\end{tikzpicture}
\end{document}
rotate

Decoration / Style / Styling

Style is included in square brackets [<styles>].

Style scopes

Overall, all styles are cascading. There are many levels where style can be defined.

  • \tikzset{<styles>} – document-wide settings
  • \begin{tikzpicture}[<styles>] – picture scope
  • \begin{scope}[<styles>] – explicit scope
  • \draw[<styles>] – single operation scope
  • <edge/node/arrow/...>[<styles>] – small part scope

Lower style overrides higher ones as in the following example.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\tikzset{color=green}
\begin{document}

\begin{tikzpicture}[framed]
    \node at (0,0) {global};
\end{tikzpicture}
\begin{tikzpicture}[framed,color=blue]
    \node at (0,2) {picture};
    \begin{scope}[color=orange]
        \node at (0,1) {scope};
        \node[color=red] at (0,0) {part};
    \end{scope}
\end{tikzpicture}

\end{document}
scopes

Line caps / Line ending styles

We can style line ending with line cap property.

  • line cap can be round, butt, or rect
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{custom/.style={
    black,line width=2mm, postaction={draw=white,thin}}
}

\foreach[count=\y] \cap in {round,butt,rect}{
    \draw[custom,line cap=\cap] (0,.4*\y-.2) -- +(.5,.2);
    \node[right] at (.6, .4*\y) {\cap};
}

\end{tikzpicture}
\end{document}
caps

Line joins / Corners

There are several parameters that style how path joins consecutive segments.

  • line join can be round, bevel, or miter
  • rounded corners
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{custom/.style={
    black,line width=2mm, postaction={draw=white,thin}}
}

\foreach[count=\y] \join in {miter,bevel,round}{
    \draw[custom,line join=\join]
        (0,2.1-.7*\y)--++(.4,.2)--++(-.4,.2);
    \node[right] at (.5,2.1-.7*\y+.23) {\join};
}

\draw[custom,rounded corners](0,-.7)--++(.4,.2)--++(-.4,.2);
\node[right] at (.5,-.7+.23) {rounded corners};

% {rounded corners=10mm}

\end{tikzpicture}
\end{document}
joins

Intermediate arrows

We can style arrows individually as {<arrow head>[<arrow style>]} (e.g. [-{Stealth[red]}]). One can also

  • define line ending with dots .
  • have various arrow tips
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\draw[help lines] (-0.1,-0.1) grid (2.1,3.1);

\usetikzlibrary{arrows.meta}
\draw[>.>->.>>] (0,2) -- +(2,1);
\draw[{Stealth[right]}-{Stealth[left]}] (0,1) -- +(2,1);
\draw[-{Bracket[reversed]}] (0,0) -- +(2,1);

\end{tikzpicture}
\end{document}
arrows_advanced

We can use beinding library to put arrows correctly on a bent edge.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.3]
\draw[help lines] (-0.1,-0.2) grid (2.1,4.2);


\draw[->>>>>>] (0,3)
    .. controls +(2,0) and +(-.6,0) .. +(2,1);

% including bending lib changes arrow behavior
\usetikzlibrary{bending}
\draw[->>>>>>] (0,2)
    .. controls +(2,0) and +(-.6,0) .. +(2,1);

\draw[-{[flex]>>>>>>}] (0,1)
    .. controls +(2,0) and +(-.6,0) .. +(2,1);
\draw[-{[bend]>>>>>>}] (0,0)
    .. controls +(2,0) and +(-.6,0) .. +(2,1);

\end{tikzpicture}
\end{document}
arrows_bending

Arrowhead styles can be modified as follows.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{arrows.meta}
\begin{tikzpicture}[scale=0.4]
\draw[help lines] (-2.1,-11.1) grid (0.1,0.1);

\foreach[count=\i] \style in {
    , red, fill=red, open, round, length=2.5mm, width=2.5mm,
    angle=90:4pt, inset=.2mm, slant=.5, harpoon
}{
    \draw[-{Stealth[\style]}] (-2,-\i) -- (0,-\i+1);
    \node[anchor=west] at (0,-\i+1) {\style};
}

\end{tikzpicture}
\end{document}
arrows_parameters
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}

\newcommand{\bracket}[3]{
    \draw[decorate,decoration=brace]
      let
        \p1 = ($(#2)-(#1)$),
        \n{angle} = {atan2(\y1,\x1)+90}
      in
        (#1.\n{angle}) --#3 (#2.\n{angle});
}

\node[draw,circle] (a) at (0,0) {$a$};
\node[draw,circle] (b) at (2,1) {$b$};

\bracket{a}{b}{node[label=\n{angle}:left]{}}
\bracket{b}{a}{node[sloped,below]{right}}

\end{tikzpicture}
\end{document}
brackets

Intermediate shapes

Arcs

Arcs are by default terrible – they start in a given position and have a weird notation. However, this approach allows us to define arcs as part of a continuous path.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\x{3}
\def\y{1}
\def\start{45}
\def\stop{70}
\def\radius{2}
\coordinate (start) at (\x,\y);
\coordinate (center) at ({\x-\radius*cos(\start)},
                         {\y-\radius*sin(\start)});
\coordinate (end) at ({\x-\radius*cos(\start)
                         +\radius*cos(\stop)},
                      {\y-\radius*sin(\start)
                         +\radius*sin(\stop)});
\draw[help lines,dashed] (center) -- (start) (center) -- (end);
\draw[circle,blue] (start) circle (.1) node[below]{start};
\draw[circle,red] (center) circle (.1) node[right]{center};
\draw[circle,green] (end)  circle (.1) node[left]{end};

\draw (\x,\y) arc (\start:\stop:\radius);

\end{tikzpicture}
\end{document}
arc

Automation / Programming

Functionality of TikZ is built upon PGF which was created to work with basic geometric structures.

Variables / Saving a coordinate

\coordinate (A) at (4,2);
\path (A) ++(1,0) coordinate (B);

Loops / Foreach / Repetition

\foreach \x in {1,...,10} {
    \draw[blue] (v\x) -- (v\xx);
}
\foreach[count=\i] \txt in {20,18,...,2}{
    \node at (\i,0) {\txt};
}
\foreach[evaluate=\y using int(22-2*\x)] \x in {1,...,10}{
    \node at (\x,0) {\y};
}

Computing values / Assignment

\pgfmathsetmacro\result{10*2} % computes the result as a decimal
\pgfmathtruncatemacro\result{10*2} % makes the result integer
\pgfmathtruncatemacro{\nodessum}{\nodessum+\len};
\xdef\nodessum{\nodessum};

Let-in / Extracting angles or distances

Library calc allows us to use let-in within the drawing commands to compute various values.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\node[draw,circle] (a) at (0,0) {};
\node[draw,circle] (b) at (2,1) {};

\draw let
    \p1 = ($(b)-(a)$),
    \n{distance} = {veclen(\y1,\x1)}
in
    (a) -- node[above,sloped]{distance is}
           node[below,sloped]{\n{distance}}
           (b) {};

\end{tikzpicture}
\end{document}
getdistance
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\node[draw,circle] (a) at (0,0) {};
\node[draw,circle] (b) at (2,1) {};

\draw let
    \p1 = ($(b)-(a)$),
    \n{angle} = {atan2(\y1,\x1)}
in
    (a) -- node[above,sloped]{angle is}
           node[below,sloped]{\n{angle}}
           (b) {};

\end{tikzpicture}
\end{document}
getangle

Extracting coordinates

Using calc tikzpackage one can calculate positions of nodes inside their notation.

Using coordinates of other nodes

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \draw [help lines] (0,0) grid (4,4);
        \node (A) at (2,1) {A};
        \path let \p1 = (A) in node  at (\x1,3) {B};
    \end{tikzpicture}
\end{document}

Libraries

Other

Set style of all nodes in the drawing.

\tikzstyle{every node}=[main]

Layers

Draw into background

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
...
\begin{pgfonlayer}{bg}    % select the background layer
    \draw (foo) -- (baz);
\end{pgfonlayer}

TikZ Syntax / Formally

In this short section we use angle brackets <> to describe what should be written in its place to describe the syntax formally.

The whole TikZ picture is enclosed in tikzpicture environment that has optional [options]. The options apply to every element (such as draw=red makes everything by default red). Popular option is scaling (e.g. scale=1.2) to make the whole picture bigger or smaller.

\begin{tikzpicture}[<options>]
\end{tikzpicture}

Main way to draw basic shapes is with \path command. Commands like \draw and \fill are simply shorthands for \path[draw] and \path[fill]. The <path syntax> contains a list of points that are optionally connected with edges of various types. Many of these options are shown in further sections.

\path[<options>] <path syntax> ;

Todos

  • custom and parameterizable tikzstyle
  • marking elements with fit anf blobs
  • using calc
  • scalebox
  • trees
    • classical tcs - bvs, avl, rb
    • file structure
  • cool curve styles
  • drawing data
  • patterns

Sources

Tutorials

Examples

Other sources