TikZ in LaTeX: Draw Diagrams, Shapes and Number Lines
Quick answer
- Load:
\usepackage{tikz}, then draw inside\begin{tikzpicture} ... \end{tikzpicture}. - Line, rectangle, circle:
\draw (0,0) -- (2,1);,\draw (0,0) rectangle (3,2);,\draw (0,0) circle (1);. - Text and arrows:
\node[draw] (a) {Text};then\draw[->] (a) -- (b);. - Styles:
\tikzset{box/.style={draw, fill=blue!10}}(\tikzstyleis deprecated). Every command ends with;.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (3,2); % rectangle from corner to corner
\draw[blue, thick] (4.5,1) circle (1); % circle: center and radius
\draw[->, red] (0,-0.5) -- (5.5,-0.5); % line with an arrow
\node at (1.5,1) {Hello, TikZ}; % text placed at a point
\end{tikzpicture}
\end{document}TikZ is how you draw in LaTeX. Diagrams, shapes, number lines, flowcharts and trees are written as code inside your document, so they use the same fonts as the text, scale without blurring and live under version control with everything else. This guide starts from a complete picture and covers rectangles, circles and lines, a full number line, nodes and arrows, flowcharts and trees, styles with \tikzset (and why \tikzstyle is deprecated), the libraries worth loading, and the errors TikZ throws most. Every snippet compiles as shown.
What is TikZ?
TikZ (pronounced "teeks") stands for "TikZ ist kein Zeichenprogramm" (German for "TikZ is not a drawing program"). It is a LaTeX package, built on the PGF graphics layer, that turns short commands into vector graphics. It ships with every TeX distribution and works with pdfLaTeX, XeLaTeX and LuaLaTeX alike.
- Vector graphics – Crisp at any zoom level and in print.
- Consistent styling – Labels use the document's fonts and math.
- Programmable – Loops and calculations generate repetitive drawings.
- Reproducible – A diagram is text, so it diffs, merges and reviews like code.
How to Draw in LaTeX: the tikzpicture Environment
Everything TikZ draws goes inside a tikzpicture. A drawing is a list of paths: a command such as \draw, optional settings in square brackets, then coordinates joined by operations, and a closing semicolon. Coordinates are (x,y) in centimetres by default; -- joins two points with a straight line and cycle closes the shape:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines, step=0.5] (0,0) grid (4,3); % a light grid to see coordinates
\draw (0,0) -- (4,0) -- (2,3) -- cycle; % a triangle
\fill[red] (2,3) circle (2pt); % a dot at the top corner
\end{tikzpicture}
\end{document}The path commands differ only in what they do with the path:
| Command | What it does |
|---|---|
\draw | Strokes the path (draws its outline) |
\fill | Fills the path, no outline |
\filldraw | Fills and strokes |
\node | Places text, optionally in a shape |
\coordinate | Names a point for later use |
\path | Builds a path without drawing it (for nodes and coordinates) |
To scale a whole picture, pass scale=0.8 to the environment; to put a drawing next to the text, use \tikz inline, as in \tikz \draw (0,0) circle (1ex);. A picture inside a figure float gets a caption and number like any image.
Draw a Rectangle, Circle and Line
A rectangle is given by two opposite corners. ++ makes the second coordinate relative to the first, which is the easiest way to say “2 cm wide, 1 cm high”:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% Rectangle from corner (0,0) to corner (3,2)
\draw (0,0) rectangle (3,2);
% Filled rectangle, size given relative to its corner
\draw[draw=blue, fill=blue!20, thick] (4,0) rectangle ++(2,1);
% Rounded corners and a dashed outline
\draw[rounded corners=6pt, dashed] (7,0) rectangle (9,2);
% A rectangle around text: a node with a minimum size
\node[draw, minimum width=3cm, minimum height=1cm] at (1.5,-1.5) {Text in a box};
\end{tikzpicture}
\end{document}Circles, ellipses and arcs follow the same pattern, and lines take arrow tips and dash patterns as options:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle (1); % circle, radius 1 cm
\draw[fill=orange!30] (3,0) circle [radius=0.6]; % current key syntax, same result
\draw (6,0) ellipse (1.2 and 0.6); % x radius and y radius
\draw (9,-0.5) arc (0:180:1); % start angle : end angle : radius
\draw (0,-2) -- (3,-2); % straight line
\draw[->, thick] (4,-2) -- (6,-1.5); % arrow
\draw[<->, dashed, red] (7,-2) -- (9.5,-2); % double arrow, dashed
\draw[line width=2pt, dotted] (0,-3) -- (9.5,-3);
\end{tikzpicture}
\end{document}| Shape | Syntax |
|---|---|
| Line | \draw (0,0) -- (3,1); |
| Polyline / polygon | \draw (0,0) -- (2,0) -- (1,2) -- cycle; |
| Rectangle | \draw (0,0) rectangle (3,2); |
| Square of side 2 | \draw (0,0) rectangle ++(2,2); |
| Circle | \draw (0,0) circle [radius=1]; (or the older, still valid circle (1)) |
| Ellipse | \draw (0,0) ellipse (2 and 1); |
| Arc | \draw (1,0) arc (0:90:1); |
| Grid | \draw[step=1] (0,0) grid (4,3); |
| Curve | \draw (0,0) .. controls (1,2) and (2,2) .. (3,0); |
Without TikZ, \fbox{text} draws a rectangle around text (see boxes and frames) and \rule{3cm}{1cm} a filled black rectangle.
Styling Lines and Fills
Options in square brackets set color, thickness, dash pattern and fill. Colors use the xcolor syntax, so blue!20 is 20% blue mixed with white:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[blue, thick, fill=blue!20] (0,0) rectangle (2,1); % colored and filled
\draw[dashed, red] (3,0) -- (5,1); % dashed line
\draw[dotted, ->, thick] (6,0) -- (8,1); % dotted with arrow
\draw[line width=2pt] (9,0) -- (11,1); % custom line width
\fill[green!50!black, opacity=0.5] (12,0.5) circle (0.5); % semi-transparent fill
\end{tikzpicture}
\end{document}Line widths have names: ultra thin, very thin, thin (the default), semithick, thick, very thick and ultra thick.
Number Line in TikZ
A number line is an axis, a tick at each integer and a label under each tick. The \foreach loop draws all the ticks in one line, and the node at the end of each tick places its label:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% The axis, with arrows at both ends
\draw[<->, thick] (-5.5,0) -- (5.5,0);
% Ticks and labels from -5 to 5
\foreach \x in {-5,...,5}
\draw (\x,0.15) -- (\x,-0.15) node[below] {$\x$};
% The interval -2 <= x < 3: closed dot at -2, open dot at 3
\draw[blue, ultra thick] (-2,0) -- (3,0);
\fill[blue] (-2,0) circle (3pt);
\draw[blue, thick, fill=white] (3,0) circle (3pt);
\node[above, blue] at (0.5,0.25) {$-2 \le x < 3$};
\end{tikzpicture}
\end{document}For fractions or decimals, stretch the x unit and loop over value/label pairs. Minor ticks are a second, shorter loop:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=8cm] % 1 unit = 8 cm
\draw[->, thick] (0,0) -- (1.1,0);
\foreach \x in {0,0.125,...,1} % minor ticks every 1/8
\draw (\x,0.08) -- (\x,-0.08);
\foreach \x/\lab in {0/0, 0.25/\frac{1}{4}, 0.5/\frac{1}{2}, 0.75/\frac{3}{4}, 1/1}
\draw[thick] (\x,0.15) -- (\x,-0.15) node[below] {$\lab$};
\fill[red] (0.625,0) circle (2.5pt) node[above=3pt] {$\frac{5}{8}$};
\end{tikzpicture}
\end{document}For a number line with a data plot on top, or axes in two dimensions, the pgfplots package computes ticks and labels for you.
Nodes and Arrows
A node is text placed at a point, optionally inside a shape. Give it a name in parentheses and you can connect it to other nodes; TikZ starts and ends the arrows at the node border, not at its center:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[box/.style={draw, rounded corners, minimum width=2cm, minimum height=8mm}]
\node[box] (in) {Input};
\node[box, right=2cm of in] (model) {Model};
\node[box, right=2cm of model] (out) {Output};
\draw[-{Stealth}] (in) -- node[above] {data} (model);
\draw[-{Latex[length=3mm]}, thick, blue] (model) -- node[below] {prediction} (out);
\draw[-{Stealth}, dashed] (out.south) -- ++(0,-1) -| node[near start, below] {feedback} (in.south);
\node[draw, circle, label=above:{a label}] at (0,-2.5) {A};
\end{tikzpicture}
\end{document}right=2cm of in comes from the positioning library and measures the gap between node borders. Anchors such as in.south pick a point on the border: north, south, east, west, the four corners like north east, and center. -| draws a horizontal then a vertical segment, and |- the reverse, which is how flowchart arrows turn corners.
| Option | Arrow |
|---|---|
->, <-, <-> | Default tip at the end, start or both ends |
-{Stealth} | Filled, swept tip (needs arrows.meta) |
-{Latex} | Filled triangular tip, as in many papers |
-{Triangle[open]} | Hollow triangle, as in UML |
-{Stealth[length=4mm, width=3mm]} | Tip with an explicit size |
>={Stealth} on the picture | Makes every -> use that tip |
Diagrams in LaTeX: Flowcharts and Trees
Flowcharts combine nodes, styles and the positioning library. Define one style per kind of box, place each node relative to the one before, then connect them:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric, arrows.meta}
\begin{document}
\begin{tikzpicture}[
node distance=8mm,
>={Stealth},
startstop/.style={rectangle, rounded corners, draw, fill=red!20, minimum width=2.5cm, minimum height=8mm},
process/.style={rectangle, draw, fill=blue!20, minimum width=2.5cm, minimum height=8mm},
decision/.style={diamond, draw, fill=green!20, aspect=2, inner sep=2pt},
]
\node[startstop] (start) {Start};
\node[process, below=of start] (step) {Process};
\node[decision, below=of step] (check) {Decision?};
\node[process, below=of check] (act) {Action};
\node[startstop, below=of act] (stop) {End};
\draw[->] (start) -- (step);
\draw[->] (step) -- (check);
\draw[->] (check) -- node[right] {Yes} (act);
\draw[->] (act) -- (stop);
\draw[->] (check.east) -- ++(1,0) node[above, near start] {No} |- (step.east);
\end{tikzpicture}
\end{document}Trees are built from child operations; each level can have its own spacing:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
level distance=1.5cm,
level 1/.style={sibling distance=3cm},
level 2/.style={sibling distance=1.5cm},
every node/.style={draw, circle, minimum size=8mm},
]
\node {R}
child { node {A}
child { node {A1} }
child { node {A2} } }
child { node {B} }
child { node {C}
child { node {C1} } };
\end{tikzpicture}
\end{document}The graphdrawing library can lay trees and graphs out automatically, but it needs LuaLaTeX. For chemistry diagrams see molecular orbital diagrams.
Coordinates and Loops
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (2,1); % Cartesian
\draw (0,0) -- (45:2); % polar: angle:radius
\draw (3,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle; % relative steps
\coordinate (A) at (6,0); % named coordinates
\coordinate (B) at (8,1);
\draw (A) -- (B);
\foreach \x in {0,1,2,3,4} % repeated shapes
\draw (\x,-1.5) circle (0.3);
\foreach \i in {1,...,5} % a pentagon
\draw ({7+cos(90+72*\i)},{-1.5+sin(90+72*\i)})
-- ({7+cos(90+72*(\i+1))},{-1.5+sin(90+72*(\i+1))});
\end{tikzpicture}
\end{document}Braces around a coordinate, as in ({cos(30)},1), let TikZ evaluate math; angles are in degrees.
\tikzstyle vs \tikzset
Older examples define styles with \tikzstyle. It still works, but the TikZ manual marks it as deprecated: it cannot take arguments cleanly and it does not follow the key syntax used everywhere else. Use \tikzset in the preamble for document-wide styles, or /.style in the picture options for local ones:
\documentclass{article}
\usepackage{tikz}
% Deprecated form (avoid in new documents):
% \tikzstyle{box}=[draw, rectangle, fill=blue!10]
% Current form, for every picture in the document:
\tikzset{
box/.style={draw, rectangle, fill=blue!10, minimum height=8mm},
tinted/.style={box, fill=#1!20}, % a style with a parameter
tinted/.default=green, % value used when none is given
}
\begin{document}
\begin{tikzpicture}[
big/.style={minimum width=3cm}, % local to this picture only
]
\node[box] at (0,0) {Box};
\node[tinted] at (2.5,0) {Green};
\node[tinted=orange, big] at (6,0) {Orange and wide};
\end{tikzpicture}
\end{document}| Old | Current |
|---|---|
\tikzstyle{box}=[draw] | \tikzset{box/.style={draw}} |
\tikzstyle{box}+=[fill=red] | \tikzset{box/.append style={fill=red}} |
\tikzstyle{every node}=[font=\small] | \tikzset{every node/.style={font=\small}} |
| No clean parameters | box/.style={fill=#1}, used as box=red |
Libraries
Many features live in libraries that you load in the preamble with \usetikzlibrary{...}, separated by commas:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta, shapes.geometric, calc, decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\node[draw, ellipse] (a) {Ellipse};
\node[draw, regular polygon, regular polygon sides=6, right=of a] (b) {Hex};
\draw[-{Stealth}] (a) -- (b);
\coordinate (m) at ($(a)!0.5!(b)$); % calc: midpoint
\fill[red] (m) circle (1.5pt);
\draw[decorate, decoration={brace, amplitude=5pt}] (a.north west) -- (b.north east)
node[midway, above=6pt] {a brace};
\end{tikzpicture}
\end{document}| Library | Adds |
|---|---|
positioning | below=of a, right=1cm of a: placement relative to other nodes |
arrows.meta | Arrow tips Stealth, Latex, Triangle with size options |
shapes.geometric | diamond, ellipse, trapezium, regular polygon, star |
shapes.misc | rounded rectangle, cross out, strike out |
calc | Coordinate math: ($(a)!0.5!(b)$), ($(a)+(1,0)$) |
decorations.pathreplacing | Curly braces along a path |
patterns | Hatched and dotted fills |
matrix | Grids of nodes, commutative diagrams |
fit, backgrounds | A box around several nodes, drawn behind them |
babel | Fixes conflicts with languages whose babel shorthands change <, > or : |
Common Errors and Fixes
| Message | Cause | Fix |
|---|---|---|
| Package tikz Error: Giving up on this path. Did you forget a semicolon? | A \draw, \node or other path has no closing ; | Add the semicolon at the end of the path |
| Package pgf Error: Unknown arrow tip kind 'Stealth' | Arrow tip from arrows.meta without the library | \usetikzlibrary{arrows.meta} |
| Package pgfkeys Error: I do not know the key '/tikz/diamond' | Shape from a library that is not loaded, or a misspelled option (e.g. fil=red) | \usetikzlibrary{shapes.geometric}, or fix the spelling |
| Package PGF Math Error: Unknown function `of' (in 'of a') | right=of a without the positioning library | \usetikzlibrary{positioning} |
| Package pgf Error: No shape named `b' is known | A node name used before it is defined, misspelled, or a coordinate typed with ; instead of , | Define the node first; write coordinates as (x,y) |
| You need to run LuaTeX to use the graph drawing library | graphdrawing only works under LuaLaTeX | Compile with LuaLaTeX, or build the tree with child as shown above |
Arrows or -| fail only after loading a language | babel shorthands (Spanish, French, Turkish and others) redefine <, >, : or ! | \usetikzlibrary{babel}; see languages in LaTeX |
Frequently Asked Questions
How do I draw in LaTeX?
Load TikZ with \usepackage{tikz} and put drawing commands inside a tikzpicture environment. \draw (0,0) -- (2,1); draws a line, \draw (0,0) rectangle (3,2); a rectangle and \draw (0,0) circle (1); a circle. Coordinates are in centimetres by default, and every command ends with a semicolon.
How do I draw a rectangle in LaTeX?
With TikZ, give two opposite corners: \draw (0,0) rectangle (3,2); draws a 3 cm by 2 cm rectangle. Add options for style, such as \draw[fill=blue!20, thick] or rounded corners. For a rectangle around text, use a node: \node[draw, minimum width=3cm, minimum height=1cm] {Text};. Outside TikZ, \fbox{text} draws a box around text and \rule{3cm}{2cm} a filled black rectangle.
How do I draw a number line in LaTeX?
Draw the axis with \draw[<->] (-5,0) -- (5,0); then add ticks and labels in a loop: \foreach \x in {-5,...,5} \draw (\x,0.15) -- (\x,-0.15) node[below] {$\x$};. Mark points with \fill (2,0) circle (3pt); for a closed dot or \draw[fill=white] for an open one.
What is the difference between \tikzstyle and \tikzset?
Both define a reusable style. \tikzstyle{box}=[draw, fill=blue!10] is the old syntax and is deprecated in the TikZ manual; it still works but cannot take parameters cleanly. The current form is \tikzset{box/.style={draw, fill=blue!10}}, or box/.style={...} in the options of one tikzpicture to keep the style local.
Which TikZ libraries do I need?
For most diagrams: positioning (below=of a placement), arrows.meta (Stealth and Latex arrow tips) and shapes.geometric (diamond, ellipse, regular polygon). Add calc for coordinate arithmetic, decorations.pathreplacing for braces, and matrix for grids of nodes. Load them in the preamble with \usetikzlibrary{positioning, arrows.meta, shapes.geometric}.
Why does TikZ say "Giving up on this path. Did you forget a semicolon?"
Every TikZ path command (\draw, \fill, \node, \path, \coordinate) must end with a semicolon. Without it, TikZ keeps reading into the next command or the end of the picture and stops with this error. Add the missing ; at the end of the line the error points to.
Next Steps
- Learn PGFPlots – Plots and charts from data and functions
- Using Colors in LaTeX – The xcolor package for colorful diagrams
- Molecular Orbital Diagrams – Chemistry diagrams drawn in TikZ
- Beamer in LaTeX – Diagrams that build up step by step on slides
- The TikZ Package (blog) – A longer walk through drawing graphics
- Learn LaTeX Basics – Review fundamentals
Free TikZ Tools
- TikZ Generator – Generate TikZ code with AI from text descriptions
- Image to TikZ – Convert images and diagrams to TikZ code
- LaTeX Preview – Test your TikZ code with live preview
Create TikZ diagrams with AI
Octree's AI can help you generate TikZ code from descriptions. Just describe what you want!
Try Octree Free →