TikZ is the drawing language behind most of the clean diagrams you see in LaTeX papers and theses. It looks intimidating for about ten minutes, then it clicks. Every figure below is rendered from the code shown (open "TikZ source" under a figure to copy it).
1. A minimal, compilable figure#
Use the standalone class so the page is cropped to your drawing, which is also what you want when you export an image later.
\documentclass[tikz,border=4pt]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (3,0);
\end{tikzpicture}
\end{document}
TikZ source
\begin{tikzpicture}
\draw (0,0) -- (3,0);
\end{tikzpicture}Coordinates are (x,y) and default to centimetres. \draw strokes a path, and every statement ends with a semicolon. The rest of this post shows only the tikzpicture, so wrap it as above.
2. Lines, rectangles and circles#
TikZ source
\begin{tikzpicture}
\draw (0,0) -- (2,1) -- (4,0); % polyline
\draw (0,-2) rectangle (3,-0.5); % corner to corner
\draw (5,-1.25) circle (0.75); % centre and radius
\draw (7.5,-1.25) ellipse (1 and 0.6); % x-radius and y-radius
\end{tikzpicture}Read each line left to right: a start point, then what to do next. -- goes straight to the next point, rectangle takes the opposite corner, circle takes a radius.
3. Arrows#
Put the arrow tip in square brackets before the path.
TikZ source
% libraries: arrows.meta
\begin{tikzpicture}
\draw[->] (0,0) -- (3,0); % arrow at the end
\draw[<->] (0,-1) -- (3,-1); % both ends
\draw[-{Stealth}] (0,-2) -- (3,-2); % nicer tip
\draw[-{Latex[length=3mm]}, thick] (0,-3) -- (3,-3);
\end{tikzpicture}The Stealth and Latex tips need \usetikzlibrary{arrows.meta} in the preamble (the first line of the snippet above is how this blog loads it).
4. Style: colour, thickness, fill, dashes#
TikZ source
\begin{tikzpicture}
\draw[red, thick] (0,0) rectangle (2,1);
\filldraw[fill=blue!20, draw=blue!70!black, very thick] (3.5,0.5) circle (0.6);
\draw[dashed, gray] (0,-0.7) -- (4.5,-0.7);
\draw[dotted, very thick, teal] (0,-1.2) -- (4.5,-1.2);
\end{tikzpicture}blue!20 means "20% blue mixed with white". Chain them: blue!70!black is 70% blue and 30% black.
5. Nodes: text and boxes#
A node is a labelled shape you can name and connect.
TikZ source
\begin{tikzpicture}[every node/.style={draw, rounded corners, minimum width=2cm, minimum height=0.8cm}]
\node (a) at (0,0) {Input};
\node (b) at (3.5,0) {Process};
\node (c) at (7,0) {Output};
\draw[->] (a) -- (b);
\draw[->] (b) -- (c);
\end{tikzpicture}Naming nodes with (a), (b) is what makes diagrams maintainable: move a node and every arrow that touches it follows.
6. Curved paths#
Bézier curves use two control points: .. controls (c1) and (c2) ... Here the control points are drawn in red so you can see how they pull the curve.
TikZ source
\begin{tikzpicture}
\draw[thick] (0,0) .. controls (1,2) and (3,2) .. (4,0);
\draw[red, dashed] (0,0) -- (1,2);
\draw[red, dashed] (4,0) -- (3,2);
\fill[red] (1,2) circle (1.5pt) (3,2) circle (1.5pt);
\end{tikzpicture}The to[out=…, in=…] form is usually easier: you give the leaving and arriving angles instead of control points.
TikZ source
% libraries: arrows.meta
\begin{tikzpicture}
\draw[->, thick] (0,0) to[out=60, in=120] (4,0);
\draw[->, thick, teal] (0,-1) to[out=-60, in=-120] (4,-1);
\draw[->, thick, orange] (0,-2) to[out=0, in=180] (4,-2.8);
\end{tikzpicture}7. Putting it together#
A small flow with everything above: named nodes, styled arrows and one curved return path.
TikZ source
% libraries: arrows.meta
\begin{tikzpicture}[node distance=2.4cm, >={Stealth},
box/.style={draw, rounded corners, fill=blue!10, minimum width=2cm, minimum height=0.9cm}]
\node[box] (read) {Read};
\node[box, right of=read] (parse) {Parse};
\node[box, right of=parse, fill=green!15] (use) {Use};
\draw[->, thick] (read) -- (parse);
\draw[->, thick] (parse) -- (use);
\draw[->, thick, red!70!black, dashed] (use.south) to[out=-110, in=-70] node[below] {retry on error} (read.south);
\end{tikzpicture}Keep it tidy#
- Declare repeated styles once:
\tikzset{box/.style={draw, rounded corners}}, then write[box]. - Use named coordinates (
\coordinate (O) at (0,0);) instead of repeating numbers. - Compile often. A figure that grows in small steps is easy to debug; a 60-line figure written blind is not.
Writing coordinates by hand is fine for small figures and tedious for big ones. For those I use a visual editor, TikZpen, which writes the TikZ for you. If you need the result as an image, see how to export TikZ to PNG, SVG and PDF.