LabCanvas

TikZ: what it is and how to use it in LaTeX

Quick answer

TikZ is a LaTeX package for drawing pictures with code. You describe lines, shapes and text with commands such as \draw and \node, and LaTeX typesets the picture with the fonts of your document. TikZ is the easy-to-use layer of PGF, the graphics system written by Till Tantau. To use it, load \usepackage{tikz} in the preamble and put the drawing commands inside a tikzpicture environment.

A rectangle with the word Hello inside, an arrow pointing right and a light blue filled circle.
Four commands: a rectangle, a text node, an arrow and a filled circle.
Preamble: \usepackage{tikz}
\begin{tikzpicture}
  \draw (0,0) rectangle (2,1);
  \node at (1,0.5) {Hello};
  \draw[->, thick] (2,0.5) -- (3,0.5);
  \fill[blue!40] (3.5,0.5) circle [radius=0.5];
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) rectangle (2,1);
  \node at (1,0.5) {Hello};
  \draw[->, thick] (2,0.5) -- (3,0.5);
  \fill[blue!40] (3.5,0.5) circle [radius=0.5];
\end{tikzpicture}
\end{document}
Contents

What TikZ is: PGF and TikZ

PGF and TikZ are one package with two names. PGF (for “portable graphics format”) is the graphics system: a system layer that hides the differences between output drivers such as pdfTeX and dvips, and a basic layer of commands such as \pgfpathcircle. TikZ is the frontend on top: a compact syntax in which \draw (0,0) -- (1,0) -- (1,1) -- cycle; is a whole triangle. The manual calls TikZ the only serious frontend for PGF; almost everyone writes TikZ and never touches the layers below.

The name is a recursive acronym: TikZ ist kein Zeichenprogramm, German for “TikZ is not a drawing program”. Till Tantau began it as LaTeX macros for the graphics in his PhD thesis; Mark Wibrow (the maths engine, shapes, decorations, matrices) and Christian Feuersänger (floating-point library, externalisation) joined the main team. On CTAN the package is called pgf. The current version is 3.1.12, released on 1 August 2026 and maintained by the PGF/TikZ team. The code may be used under the LaTeX Project Public License 1.3c or the GNU GPL 2, the manual under the GNU FDL 1.2 or the LPPL.

The manual is honest about the trade-off: you get precise positioning, macros and the typography of TeX, and you pay with a steep learning curve, no WYSIWYG view and a recompile for every change. That is why people pair it with a live preview (Overleaf, an editor with SyncTeX, or the TikZ editor on this site).

Many packages draw with TikZ underneath: pgfplots for plots with axes, tikz-cd for commutative diagrams, circuitikz for electrical circuits and chemfig for molecules. Learning TikZ makes all of them easier.

How to use TikZ in a LaTeX document

Three things go into a document that uses TikZ:

  1. \usepackage{tikz} in the preamble. The package has no options, and loading it also loads PGF and the \foreach loop (pgffor).
  2. \usetikzlibrary{…}, also in the preamble and after \usepackage{tikz}, for any extra libraries: a comma-separated list such as \usetikzlibrary{positioning, arrows.meta}. Loading a library twice does nothing, so it is safe in shared preambles.
  3. A tikzpicture environment where the picture goes. All drawing commands live inside it (only \tikzset may stand outside), and each command ends with a semicolon. Options in square brackets after \begin{tikzpicture} apply to the whole picture. For a one-line picture there is the short form \tikz \draw (0,0) -- (1,0);.

A complete document, ready to compile with pdfLaTeX, LuaLaTeX or XeLaTeX:

A whole LaTeX file (article class)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}   % load only the libraries you use

\begin{document}
Text before the picture.

\begin{tikzpicture}
  \draw[-Stealth] (0,0) -- (2,0);
  \draw (3,0) circle [radius=0.5];
\end{tikzpicture}

Text after the picture.
\end{document}

The picture is set in the text like one large letter, so on a line of its own it starts where a paragraph would start. To centre it, number it and give it a caption, put it in a figure environment with \centering and \caption: see the tikzpicture environment.

For a figure on its own (to include with \includegraphics, or to upload separately), use the standalone class: \documentclass[border=4pt]{standalone} crops the page to the picture plus the border. Every “Full document” on these pages uses it, and the example below loads two libraries: positioning places a node right=of another, and arrows.meta provides the Stealth arrow tip.

Three rounded boxes labelled Sample, Measure and Plot in a row, joined left to right by arrows.
Nodes placed with positioning (right=of) and joined with Stealth arrow tips from arrows.meta; box is a style defined in the picture’s options.
Preamble: \usepackage{tikz} \usetikzlibrary{positioning, arrows.meta}
\begin{tikzpicture}[box/.style={draw, rounded corners, minimum height=8mm}]
  \node[box] (sample) {Sample};
  \node[box, right=of sample] (measure) {Measure};
  \node[box, right=of measure] (plot) {Plot};
  \draw[-Stealth] (sample) -- (measure);
  \draw[-Stealth] (measure) -- (plot);
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[box/.style={draw, rounded corners, minimum height=8mm}]
  \node[box] (sample) {Sample};
  \node[box, right=of sample] (measure) {Measure};
  \node[box, right=of measure] (plot) {Plot};
  \draw[-Stealth] (sample) -- (measure);
  \draw[-Stealth] (measure) -- (plot);
\end{tikzpicture}
\end{document}

How to draw your first TikZ figure, step by step

A small plot drawn by hand shows most of what you use every day. Build it in this order, compiling after each step:

  1. Start with a grid of help lines. \draw[help lines] (0,0) grid (4,3); draws faint lines every centimetre, so you can see where each coordinate is.
  2. Draw the axes. -- joins two points with a straight line and the option -> puts an arrow tip at the end.
  3. Add a curve and a point. .. controls (1.5,3) and (2.5,0) .. draws a Bézier curve from the current point to the end point, pulled towards the two control points (it does not pass through them); circle [radius=2pt] draws a circle, and \fill fills it instead of stroking it.
  4. Label it with nodes. node[right] {$x$} after a coordinate puts text at that point of the path; above left, below and the other placement options say on which side. Text in nodes is ordinary LaTeX, maths included.
  5. Remove the help lines (or keep them while you work) and compile the final figure.

The point (2, 1.5) in step 3 is exactly on the curve: a cubic Bézier curve is at ⅛·start + ⅜·first control + ⅜·second control + ⅛·end halfway along, which here is (2, 1.5).

A faint 4 by 3 centimetre grid with an x axis and a y axis ending in arrow tips.
Step 1 and 2: a grid of help lines and two axes with arrow tips.
Preamble: \usepackage{tikz}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,3);
  \draw[->] (0,0) -- (4.3,0);
  \draw[->] (0,0) -- (0,3.3);
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,3);
  \draw[->] (0,0) -- (4.3,0);
  \draw[->] (0,0) -- (0,3.3);
\end{tikzpicture}
\end{document}
The grid and axes with a thick blue S-shaped curve, a dashed vertical line at x = 2 and a red dot where it meets the curve.
Step 3: a Bézier curve, a dashed guide line and a filled point on the curve.
Preamble: \usepackage{tikz}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,3);
  \draw[->] (0,0) -- (4.3,0);
  \draw[->] (0,0) -- (0,3.3);
  \draw[thick, blue] (0,0.5) .. controls (1.5,3) and (2.5,0) .. (4,2.5);
  \draw[dashed] (2,0) -- (2,1.5);
  \fill[red] (2,1.5) circle [radius=2pt];
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,3);
  \draw[->] (0,0) -- (4.3,0);
  \draw[->] (0,0) -- (0,3.3);
  \draw[thick, blue] (0,0.5) .. controls (1.5,3) and (2.5,0) .. (4,2.5);
  \draw[dashed] (2,0) -- (2,1.5);
  \fill[red] (2,1.5) circle [radius=2pt];
\end{tikzpicture}
\end{document}
Axes labelled x and y, the blue curve, a dashed line from the tick labelled 2 up to a red dot labelled P.
Steps 4 and 5: axis labels, a tick label and a point label as nodes, with the help lines removed.
Preamble: \usepackage{tikz}
\begin{tikzpicture}
  \draw[->] (0,0) -- (4.3,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,3.3) node[above] {$y$};
  \draw[thick, blue] (0,0.5) .. controls (1.5,3) and (2.5,0) .. (4,2.5);
  \draw[dashed] (2,0) node[below] {$2$} -- (2,1.5);
  \fill[red] (2,1.5) circle [radius=2pt];
  \node[above left] at (2,1.5) {$P$};
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[->] (0,0) -- (4.3,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,3.3) node[above] {$y$};
  \draw[thick, blue] (0,0.5) .. controls (1.5,3) and (2.5,0) .. (4,2.5);
  \draw[dashed] (2,0) node[below] {$2$} -- (2,1.5);
  \fill[red] (2,1.5) circle [radius=2pt];
  \node[above left] at (2,1.5) {$P$};
\end{tikzpicture}
\end{document}

Coordinates and units at a glance

TikZ’s y axis points up, and a coordinate without units is measured in the picture’s unit vectors, which are 1 cm long unless you change them with the x= and y= options. The forms you will use most:

You writeIt means
(2,1)2 units right and 1 up from the origin: 2 cm and 1 cm by default
(2cm,5mm), (1in,10pt)Explicit lengths, in any TeX unit
(60:2), (60:2cm)Polar: 2 units (or 2 cm) from the origin at 60° (0° points right, 90° up)
++(1,0)1 unit right of the previous point, which then becomes the current point
+(1,0)1 unit right of the previous point, without moving the current point
(A), (A.north)A named coordinate (\coordinate (A) at (1,2);) or a node, or one of a node’s anchors
($(A)!0.5!(B)$)Halfway from A to B: a coordinate calculation, which needs \usetikzlibrary{calc}
A grid with dots at (0,0), (2,1) and (60:2), and a blue arrow from (2,1) going one unit right and then one unit up.
Cartesian (2,1), polar (60:2) and two relative steps with ++.
Preamble: \usepackage{tikz}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \fill (0,0) circle [radius=1.5pt] node[below left] {(0,0)};
  \fill (2,1) circle [radius=1.5pt] node[below right] {(2,1)};
  \fill (60:2) circle [radius=1.5pt] node[above left] {(60:2)};
  \draw[->, thick, blue] (2,1) -- ++(1,0) -- ++(0,1) node[right] {++(1,0) then ++(0,1)};
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \fill (0,0) circle [radius=1.5pt] node[below left] {(0,0)};
  \fill (2,1) circle [radius=1.5pt] node[below right] {(2,1)};
  \fill (60:2) circle [radius=1.5pt] node[above left] {(60:2)};
  \draw[->, thick, blue] (2,1) -- ++(1,0) -- ++(0,1) node[right] {++(1,0) then ++(0,1)};
\end{tikzpicture}
\end{document}

Libraries, and plots with pgfplots

TikZ keeps its core small and puts extras in libraries. The ones you meet first:

  • arrows.meta: arrow tips such as Stealth and Latex that you can resize and recolour (the older arrows library is deprecated).
  • positioning: right=of a, below=1cm of b and node distance, for diagrams built from boxes.
  • calc: computed coordinates such as midpoints and points part-way along a line.
  • shapes.geometric: node shapes such as ellipse, diamond and regular polygon.
  • backgrounds and fit: drawing behind other elements, and frames that fit around a group of nodes.
  • decorations.pathmorphing and decorations.pathreplacing: zigzag and snake lines, and curly braces.

TikZ can plot a function itself, but for anything with axes, ticks, legends or data files the usual choice is pgfplots, a separate package built on TikZ. An axis environment sits inside the tikzpicture, and \pgfplotsset{compat=1.18} in the preamble selects the behaviour of pgfplots 1.18. Its maths is PGF’s, so sin(x) takes degrees.

A plot of one period of a sine wave in blue, with x from 0 to 360 degrees and y from −1 to 1, axes labelled.
A pgfplots axis inside a tikzpicture: sin x from 0 to 360 degrees.
Preamble: \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.18}
\begin{tikzpicture}
  \begin{axis}[width=7cm, height=4.5cm, xlabel={$x$ (degrees)}, ylabel={$\sin x$}]
    \addplot[blue, thick, domain=0:360, samples=73] {sin(x)};
  \end{axis}
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[width=7cm, height=4.5cm, xlabel={$x$ (degrees)}, ylabel={$\sin x$}]
    \addplot[blue, thick, domain=0:360, samples=73] {sin(x)};
  \end{axis}
\end{tikzpicture}
\end{document}

TikZ on Overleaf and with a local TeX installation

Overleaf. Overleaf compiles with TeX Live, so TikZ and its libraries are already there: add \usepackage{tikz} and compile. New projects use the newest TeX Live on Overleaf’s servers, existing projects keep the version they were created with, and pdfLaTeX is the default compiler. You can change both in the project settings (the gear icon, then Compiler): switch to LuaLaTeX or XeLaTeX for system fonts, or to a newer TeX Live to get a newer PGF.

TeX Live and MacTeX. TeX Live’s default full scheme installs everything, TikZ included, and MacTeX is TeX Live for the Mac. PGF 3.1.12 needs at least TeX Live 2020. TeX Live 2026 came out on 1 March 2026; to bring a TeX Live installation up to date, run tlmgr update --all, or tlmgr update pgf for PGF alone.

MiKTeX. MiKTeX can install missing packages automatically: the first document that loads TikZ asks permission to install the pgf package.

To check which version you have, typeset \pgfversion (it expands to the version number), and compare it with the version on CTAN. To try code without installing anything, open it in the TikZ editor, which runs TeX Live 2026 with the common libraries, pgfplots, circuitikz, tikz-cd and chemfig loaded.

Every TikZ page on LabCanvas

Each guide follows the same pattern: a direct answer, then working code for every variant, each shown with the picture TeX Live made from it, the full document to copy, the errors you are likely to meet and the manual sections behind every statement.

Start here

Shapes, lines and text

Diagrams with TikZ packages

Examples

More TikZ guides

Examples by field

Common errors and how to fix them

LaTeX says (or you see)Cause and fix
Environment tikzpicture undefined.The preamble lacks \usepackage{tikz}.
Undefined control sequence. (at \usetikzlibrary)\usetikzlibrary comes before \usepackage{tikz}; move it after.
Undefined control sequence. (at \draw in the text)Drawing commands outside a tikzpicture. Put them inside one, or write \tikz \draw …; for a single command.
Package tikz Error: Giving up on this path. Did you forget a semicolon?The command before the line LaTeX names has no closing ;.
Package pgfkeys Error: I do not know the key '/tikz/colr' …A misspelt option, or an option from a library you have not loaded (add its \usetikzlibrary).
Package pgf Error: Unknown arrow tip kind 'Stealth'.Arrow tips with capital letters (Stealth, Latex) need \usetikzlibrary{arrows.meta}.
Package PGF Math Error: Unknown function `of' (in 'of a').right=of a without the positioning library: add \usetikzlibrary{positioning}.
Missing $ inserted.Maths in node text outside $…$, such as x_1: write {$x_1$}.

Frequently asked questions

What does TikZ stand for?

TikZ is a recursive acronym for the German “TikZ ist kein Zeichenprogramm”, which means “TikZ is not a drawing program”: you program the picture rather than draw it with the mouse.

What is the difference between PGF and TikZ?

PGF is the graphics system underneath (a system layer for the output drivers and a basic layer of commands); TikZ is the user-friendly frontend built on it. On CTAN and in TeX distributions the package is called pgf, and \usepackage{tikz} loads both.

Do I need to install TikZ?

Usually not: it is part of TeX Live, MacTeX and Overleaf, and MiKTeX installs it the first time a document needs it. PGF 3.1.12 needs at least TeX Live 2020.

Which LaTeX engine should I use for TikZ?

pdfLaTeX works for almost everything and is the default on Overleaf. LuaLaTeX and XeLaTeX work too, and you need LuaLaTeX for TikZ’s automatic graph drawing library (graphdrawing) and when you want system fonts with fontspec.

Should I use TikZ or pgfplots for a plot?

TikZ can plot a function with its plot operation, but pgfplots draws the axes, ticks, legends and data plots for you. Use pgfplots for anything that looks like a chart; it is built on TikZ, so you can still add TikZ drawing to the same picture.

How do I learn TikZ quickly?

Start with a small picture and change one thing at a time: the step-by-step figure on this page, then the tutorials in part I of the PGF/TikZ manual. Keep the manual for reference; it explains every option with examples. The manual guide says which section to read for each task.

How do I turn a TikZ picture into an image file?

Compile it in a standalone document: \documentclass[border=4pt]{standalone} gives a PDF cropped to the picture. The free TikZ editor typesets the code and downloads a PNG at 300 or 600 dpi.

References

  1. PGF/TikZ manual (version 3.1.12), §1 Introduction (what TikZ and PGF are, the name)tikz.dev
  2. PGF/TikZ manual (version 3.1.12), §1.1 The Layers Below TikZtikz.dev
  3. PGF/TikZ manual (version 3.1.12), §1.5 Authors and Acknowledgementstikz.dev
  4. PGF/TikZ manual (version 3.1.12), §8.1 Package and Driver Versions (TeX Live 2020 or later)tikz.dev
  5. PGF/TikZ manual (version 3.1.12), §9.1 Which License Applies?tikz.dev
  6. PGF/TikZ manual (version 3.1.12), §12.1 Loading the Package and the Librariestikz.dev
  7. PGF/TikZ manual (version 3.1.12), §12.2.1–12.2.2 Creating a Picture Using an Environment or a Commandtikz.dev
  8. PGF/TikZ manual (version 3.1.12), §13.2.1 Canvas, XYZ, and Polar Coordinate Systemstikz.dev
  9. PGF/TikZ manual (version 3.1.12), §13.4.1 Specifying Relative Coordinatestikz.dev
  10. PGF/TikZ manual (version 3.1.12), §13.5 Coordinate Calculationstikz.dev
  11. PGF/TikZ manual (version 3.1.12), §14 Syntax for Path Specifications (§14.2 line-to, §14.3 curve-to, §14.6 circle, §14.8 grid)tikz.dev
  12. PGF/TikZ manual (version 3.1.12), §15.1 Overview of actions on paths (\draw, \fill)tikz.dev
  13. PGF/TikZ manual (version 3.1.12), §16.1 Arrows: overview (arrows.meta; arrows deprecated)tikz.dev
  14. PGF/TikZ manual (version 3.1.12), §17.2 Nodes and Their Shapes; §17.5.3 Advanced Placement Options (positioning)tikz.dev
  15. PGF/TikZ manual (version 3.1.12), §25.2 The XY- and XYZ-Coordinate Systems (default 1 cm unit vectors)tikz.dev
  16. PGF/TikZ manual (version 3.1.12), §28 Using Graph Drawing in TikZ (requires LuaTeX)tikz.dev
  17. PGF/TikZ manual (version 3.1.12), §94.3.4 Trigonometric functions (degrees)tikz.dev
  18. CTAN: the pgf package (version 3.1.12, maintainers, licences)ctan.org
  19. CTAN announcement: pgf 3.1.12, 1 August 2026ctan.org
  20. standalone class manual (v1.5a), §5.1 Basic usage and §5.2 Class options (border)mirrors.ctan.org
  21. pgfplots manual (v1.18.3), §2.2 Upgrade remarks (the compat key)mirrors.ctan.org
  22. CTAN: pgfplots, tikz-cd, circuitikz and chemfig (built on TikZ)ctan.org
  23. Overleaf docs: TeX Live on Overleafdocs.overleaf.com
  24. Overleaf docs: Selecting a TeX Live version and LaTeX compilerdocs.overleaf.com
  25. TeX Live: TeX Live 2026, released 1 March 2026tug.org
  26. The TeX Live Guide 2026, §3.2 schemes and §5.2 tlmgr updatetug.org
  27. tlmgr manual: update [option...] [pkg...]tug.org
  28. MiKTeX manual: Automatic Package Installationdocs.miktex.org

The facts on this page were checked against these sources, and every picture is the code with it typeset with TeX Live 2026. Code on this page: CC0 1.0, copy it freely.

More TikZ guides

Tools

Try the code in your browserEvery snippet on this page opens in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the TikZ editor