TikZ basics

TikZ is the standard system for drawing vector graphics directly inside LaTeX, in code. You write coordinates, lines, circles, arrows, and nodes in the same source as your prose, and the figure is set with the same fonts and the same quality as the surrounding text. No round-trips to a separate drawing program; your figures become a reproducible part of the document — that is the subject of this page.

PGF and TikZ

First, the names. At the bottom sits PGF (Portable Graphics Format), a low-level graphics engine that actually draws the lines while smoothing over driver differences (PDF vs. DVI). Writing raw PGF is laborious. So TikZ is the friendly front-end (a macro layer) laid on top of PGF to make drawing pleasant. Both were created by Till Tantau.

The name TikZ is a recursive, GNU-style acronym for the German “TikZ ist kein Zeichenprogramm” (“TikZ is not a drawing program”). Its pronunciation is not officially fixed — you will hear /tiks/, /tikts/, or /tik-zee/. In practice people speak of “PGF/TikZ” as one thing, and what we actually write is almost always the TikZ side.

Getting started is easy: just put \usepackage{tikz} in your preamble, and PGF is loaded along with it. It works out of the box with pdfLaTeX, LuaLaTeX, and XeLaTeX. Only when you go through DVI — as with pLaTeX or upLaTeX — do you also specify a driver (usually dvipdfmx) as a class option.

document.tex
\documentclass{article}   % pLaTeX なら \documentclass[dvipdfmx]{jsarticle}
\usepackage{tikz}
\begin{document}
We are working on
\begin{tikzpicture}
  \draw (-1.5,0) -- (1.5,0);
  \draw (0,-1.5) -- (0,1.5);
\end{tikzpicture}.
\end{document}

This sets the text “We are working on,” then draws a small cross — a horizontal and a vertical line through the origin — right in the line, and finishes with a period. That blending of a figure into the text line is the feel of TikZ.

The tikzpicture environment

Every figure is drawn inside the **tikzpicture environment** (\begin{tikzpicture}\end{tikzpicture}). This is the “canvas” that holds one picture; you list drawing commands inside it. In the brackets after the environment name you can put options that apply to the whole picture (e.g. [scale=2] to double its size, [thick] to make everything heavier).

For a tiny one-line figure you need not open the environment at all: there are the inline forms **\tikz{...} and, for a single command, \tikz \draw ...;. Whichever form you use, there is one rule you must never forget: every drawing command ends with a semicolon ;**. The semicolon marks “this path ends here,” and forgetting it is an error — the single most common beginner stumble.

latex
% 行内(インライン)
\tikz \draw (0,0) -- (1.5,0);

% 環境(複数のコマンド)
\begin{tikzpicture}
  \draw (-1.5,0) -- (1.5,0) -- (0,-1.5) -- (0,1.5);
\end{tikzpicture}

The path model — coordinates and operations

Drawing in TikZ revolves around the path — a route describing how you move from point to point — and the command name chooses how that route is shown. **\draw strokes the route as a line, \fill fills the inside of a closed route, \filldraw fills and then strokes the outline, and \path** merely defines the route without drawing anything (useful for placing coordinates or nodes you will refer to later).

There are three ways to specify a point. Cartesian (x,y) defaults to centimeters, so (1,2) means 1 cm right and 2 cm up; you can give units explicitly, as in (1cm,2pt). Polar (angle:radius) reads as “in this direction, this far,” e.g. (30:1cm). And if you name a point (a named coordinate) you can recall it later by name. Relative moves exist too: ++(1,0) steps 1 cm right *and* updates the current point, while +(1,0) is relative to the start point without updating it.

Path operations decide how the route proceeds. **-- draws a straight line** from the previous point to the next (chain them like (a) -- (b)). **rectangle makes a rectangle from two opposite corners, circle a circle about a center, ellipse an ellipse, arc a circular arc, and grid a grid of lines. Curves come from .. controls ..** (a Bézier curve) or, as we will see, to[bend]. To close a shape, end with -- cycle to return to the start.

The modern way to pass a shape’s arguments is as key–value pairs in brackets [...]. A circle is circle [radius=10pt], an ellipse ellipse [x radius=20pt, y radius=10pt], an arc arc [start angle=0, end angle=30, radius=3mm] (from 0° to 30°, radius 3 mm). For a grid, [step=.5cm] sets the spacing. The next example draws a faint grid, then a circle of radius 1 cm at the origin, and a straight line from the origin to (1,1).

latex
\begin{tikzpicture}
  \draw[step=.5cm, gray, very thin] (-1.4,-1.4) grid (1.4,1.4);
  \draw (0,0) circle [radius=1cm];
  \draw (0,0) -- (1,1);
  \fill[blue!20] (0,0) rectangle (0.5,0.5);
\end{tikzpicture}

The picture lays a faint gray grid in 0.5 cm steps from (-1.4,-1.4) to (1.4,1.4), then over it a circle centered at the origin, a diagonal line from the origin up to (1,1), and a small 0.5 cm square at the origin filled in pale blue. blue!20 means “20% blue” — blue mixed with white, hence light.

Nodes — placing text and shapes

A node places text or a shape at a given coordinate; it is the workhorse for labeling figures. The basic form is \node[options] (name) at (coordinate) {contents};. Options include draw (stroke a border), circle or rectangle (the shape), fill=blue!20 (a fill), and so on. The name in parentheses becomes the node’s handle, which you can later reference for position as (name) or (name.north).

A pattern you will use constantly is to place named nodes first, then connect the names with lines. Because you join by node name instead of raw coordinates, the connecting lines follow automatically when you move things around.

latex
\begin{tikzpicture}
  \node (a) at (0,0)   [draw, circle, fill=blue!20] {A};
  \node (b) at (2.5,0) [draw, circle, fill=blue!20] {B};
  \draw[->] (a) -- (b);
\end{tikzpicture}

This draws two pale-blue circular nodes labeled “A” and “B,” set 2.5 cm apart, and an arrowed line from A to B. The line stops cleanly at each node’s border rather than poking into the circles.

Nodes can also sit along a path. Writing \draw (a) -- (b) node[midway] {label}; puts a label at the midpoint (midway) of the line from a to b; add above or below to shift it off the line. This is the standard way to annotate a connection.

Options and styles

You shape a line’s appearance with options in brackets. Common ones: the weights thin / thick / very thick; colors like red or blue!50; dashed or dotted; rounded corners; and arrows. Arrows are **-> (a tip at the end), <- (at the start), <->** (both). In fact TikZ takes the pragmatic view that *any* unknown option containing a - is an arrow specification.

OptionMeaning
thick / very thickHeavier lines (up to ultra thick)
red, fill=blue!50Stroke or fill color (!n sets intensity)
dashed / dottedDashed or dotted lines
rounded cornersRound the corners of a path
->, <-, <->Arrow tip at end, start, or both
scale=2Scale that picture (or path) by 2

If you repeat the same decoration, the idiom is to name it as a style and reuse it. Define it in the preamble with \tikzset{name/.style={...}}, or define it on the spot in the tikzpicture options. For instance, the next snippet defines a help lines style and applies it to a grid.

latex
\tikzset{help lines/.style={color=blue!50, very thin}}

\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw[thick, red, ->, rounded corners] (0,0) -- (1,2) -- (3,2);
\end{tikzpicture}

This draws a faint blue grid from (0,0) to (3,2), then a thick red line bending from the origin to (1,2) and on to (3,2), with rounded corners and an arrow tip at the end. Because the grid uses a style, changing very thin to thin restyles the whole grid in one place.

Libraries — loading the features you need

TikZ’s core is deliberately kept small, with specialized features split into libraries that you load with \usetikzlibrary{...} in the preamble (comma-separated for several). The most useful ones:

  • arrows.meta** — a rich, tunable set of arrow tips (-{Stealth}, -{Latex}, …). The older arrows library is deprecated; use this one now.
  • positioning** — relative node placement, so you can write right=of a or below=1cm of b (“to the right of that node,” “1 cm below it”).
  • calc** — coordinate arithmetic: ($(a)+(1,0)$) is “1 cm right of a,” and ($(a)!0.5!(b)$) is “the midpoint of a and b.”
  • shapes** — many node shapes beyond circle and rectangle (diamonds, stars, callouts). Subdivided, e.g. shapes.geometric.
  • decorations** — decorate a path as a wave, zigzag, brace, and more (e.g. decorations.pathmorphing).
  • patterns** — fill with a pattern such as hatching (lines or dots).
  • fit** — automatically build a box that encloses several nodes.
  • backgrounds** — draw a frame or backdrop on a background layer behind the picture.
  • matrix** — align nodes in a grid (matrix); graphdrawing does automatic layout (requires LuaTeX).

The next example is a small flowchart combining arrows.meta and positioning. Only the first node is given a coordinate; the rest are placed relatively (“to the right,” “below”), and joined with Stealth-style arrow tips.

document.tex
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\begin{tikzpicture}[node distance=1cm, every node/.style={draw, rounded corners}]
  \node (start)            {Start};
  \node (proc)  [right=of start] {Process};
  \node (end)   [right=of proc]  {End};
  \draw[-{Stealth}] (start) -- (proc);
  \draw[-{Stealth}] (proc)  -- (end);
\end{tikzpicture}

This produces a left-to-right flowchart: three rounded-corner boxes labeled “Start,” “Process,” and “End,” spaced 1 cm apart and joined in order by sharp Stealth arrow tips. The trick is every node/.style, which gives every node a common look at once.

Compile cost and externalize

Because LaTeX computes and draws TikZ figures one element at a time, complex pictures or many of them make compilation noticeably slow — plots and dense grids are especially heavy.

The standard remedy is the externalize library. Load \usetikzlibrary{external} and declare \tikzexternalize, and each tikzpicture is compiled just once to its own PDF and cached. On later runs, if a figure’s contents are unchanged, TikZ skips the computation and simply includes the finished PDF. The first run is slow, but subsequent edit cycles become dramatically faster (figures regenerate automatically when you change them). Alternatively, split a big figure into its own file and compile it alone with the standalone class.