TikZ Package: Drawing Graphics in LaTeX
Date Published

TikZ: Graphics in LaTeX
TikZ lets you create publication-quality diagrams directly in LaTeX.
Quick answer: How do you use TikZ in LaTeX? Add \usepackage{tikz} to the preamble and draw inside a tikzpicture environment, ending every command with a semicolon: \begin{tikzpicture} \draw (0,0) -- (2,1); \end{tikzpicture} draws a line from (0,0) to (2,1) in centimetres. Load extras with \usetikzlibrary{arrows.meta, positioning}.
TikZ in LaTeX: Draw Graphics
The smallest complete TikZ document:
1\documentclass{article}2\usepackage{tikz}3\begin{document}4\begin{tikzpicture}5 \draw (0,0) -- (2,1);6\end{tikzpicture}7\end{document}
Try this in Octree — free LaTeX editor with AI, compiles in your browser
A cheat sheet with lines, rectangles, circles, nodes, arrows and libraries. It compiles as written:
1\documentclass{article}2\usepackage{tikz}3\usetikzlibrary{arrows.meta, positioning, shapes.geometric} % arrow tips, below=of, diamond4\begin{document}5\begin{tikzpicture}6 % lines and shapes7 \draw (0,0) -- (2,0); % line8 \draw[thick, dashed, blue] (0,-0.5) -- (2,-0.5); % styled line9 \draw (3,-0.5) rectangle (4.5,0.5); % rectangle10 \draw[fill=red!20] (6,0) circle (0.5); % filled circle11 % arrows12 \draw[-{Stealth}] (0,-1.5) -- (2,-1.5); % arrow at the end13 \draw[{Stealth}-{Stealth}] (3,-1.5) -- (5,-1.5); % arrows at both ends14 % nodes (text boxes) placed relative to each other15 \node[draw, rounded corners] (a) at (0,-3) {Start};16 \node[draw, diamond, aspect=2, right=of a] (b) {Test?};17 \node[draw, circle, right=of b] (c) {End};18 \draw[-{Stealth}] (a) -- (b);19 \draw[-{Stealth}] (b) -- node[above] {yes} (c);20\end{tikzpicture}21\end{document}
Skip the coordinates. Describe a diagram in plain English and get TikZ code with the free TikZ generator, or upload a sketch or screenshot to Image to TikZ and get editable TikZ back.
Getting Started
\usepackage{tikz}
Basic drawing environment:
\begin{tikzpicture} ...drawing commands... \end{tikzpicture}
Basic Shapes
Lines
\draw (0,0) -- (2,0); — horizontal line
\draw (0,0) -- (2,2); — diagonal line
Rectangles
\draw (0,0) rectangle (2,1);
Circles
\draw (0,0) circle (1cm);
Ellipses
\draw (0,0) ellipse (2cm and 1cm);
Coordinates
Cartesian
(x, y) — x units right, y units up
Relative
++(1,1) — relative to current position
Named
\coordinate (A) at (0,0); \draw (A) -- (B);
Nodes
Nodes are points with optional labels:
Basic Node
\node at (0,0) {Text};
Positioned Node
\node[above] at (0,0) {Label above};
\node[below right] at (0,0) {Below right};
Styled Node
\node[circle, draw, fill=blue!20] {A};
Paths
Draw (stroked path)
\draw (0,0) -- (1,1) -- (2,0);
Fill (filled path)
\fill[blue] (0,0) rectangle (1,1);
Filldraw (both)
\filldraw[fill=yellow, draw=black] (0,0) circle (1);
Styling
Line Styles
\draw[dashed] (0,0) -- (2,0);
\draw[dotted] (0,0) -- (2,0);
\draw[thick] (0,0) -- (2,0);
\draw[line width=2pt] (0,0) -- (2,0);
Colors
\draw[red] (0,0) -- (1,1);
\draw[blue!50] (0,0) -- (1,1); — 50% blue
Arrows
\draw[->] (0,0) -- (2,0); — arrow at end
\draw[<->] (0,0) -- (2,0); — arrows both ends
\draw[->, thick] (0,0) -- (2,0);
Curved Paths
Bezier Curves
\draw (0,0) .. controls (1,1) .. (2,0);
Smooth Curves Through Points
\draw plot[smooth] coordinates {(0,0) (1,1) (2,0)};
Arcs
\draw (0,0) arc (0:90:1cm); — start:end:radius
Grids
\draw[step=1cm, gray, thin] (0,0) grid (4,4);
Transformations
Shift
\draw[shift={(1,1)}] (0,0) circle (0.5);
Scale
\draw[scale=2] (0,0) rectangle (1,1);
Rotate
\draw[rotate=45] (0,0) rectangle (1,1);
TikZ Libraries
Load additional features:
\usetikzlibrary{shapes, arrows, positioning}
Common libraries: - shapes — more shapes - arrows — arrow styles - positioning — relative positioning - calc — coordinate calculations - patterns — fill patterns
Flowcharts
With shapes and arrows:
\node[rectangle, draw] (start) {Start}; \node[rectangle, draw, below=of start] (process) {Process}; \draw[->] (start) -- (process);
Mathematical Plots
\draw[domain=0:2*pi, samples=100] plot (\x, {sin(\x r)});
Common Patterns
Labeled Diagram
\node[circle, draw] (A) at (0,0) {A}; \node[circle, draw] (B) at (2,0) {B}; \draw[->] (A) -- node[above] {edge} (B);
Coordinate System
\draw[->] (-0.5,0) -- (3,0) node[right] {$x$}; \draw[->] (0,-0.5) -- (0,3) node[above] {$y$};
Common TikZ Errors and Fixes
"Package tikz Error: Giving up on this path. Did you forget a semicolon?" Every \draw, \node, \fill and \path command must end with ;. The error points at the line after the missing semicolon.
"I do not know the key '/tikz/...'" The option is misspelled or comes from a library you have not loaded: -{Stealth} needs arrows.meta, diamond and ellipse shapes need shapes.geometric, and below=of a needs positioning (without it TikZ reports "Unknown function 'of'").
"No shape named a is known" A path refers to a node name before the node is defined, or the name is misspelled. Define nodes first, then draw the edges between them.
"Dimension too large" in plots. A plotted function grows beyond TeX's limit (about 16384 pt). Restrict the domain, for example domain=0:2, or use pgfplots with \pgfplotsset{compat=1.18} for function plots.
"Undefined control sequence \usetikzlibrary" \usetikzlibrary is used before \usepackage{tikz}. Load tikz first. The old arrows library still works but is deprecated; use arrows.meta.
TikZ FAQ
What is TikZ in LaTeX?
TikZ is a LaTeX package (built on the PGF graphics layer) for drawing vector graphics with code: diagrams, flowcharts, graphs, circuits and plots. Load it with \usepackage{tikz} and draw inside a tikzpicture environment.
Do I need to install TikZ?
No. TikZ is part of the pgf package, which ships with TeX Live, MacTeX and MiKTeX (MiKTeX installs it on first use). Online editors such as Octree have it ready.
How do I export a TikZ picture as a PDF or PNG?
Put the picture in its own file with \documentclass[tikz]{standalone}; the PDF is cropped to the drawing and can be included elsewhere with \includegraphics or converted to PNG.
Can I convert an image to TikZ code?
Yes: Image to TikZ turns a sketch, screenshot or photo of a diagram into editable TikZ, and the TikZ generator writes TikZ from a text description.
What is the difference between TikZ and pgfplots?
TikZ draws general graphics; pgfplots is built on TikZ and is designed for data and function plots with axes, legends and ticks (\begin{axis}...\end{axis}).
Create TikZ Diagrams with Octree
Octree helps with TikZ:
- Preview diagrams in real-time - AI suggests correct syntax - Visual coordinate feedback - Export vector graphics
Try it at https://useoctree.com
Try this in Octree — free LaTeX editor with AI, compiles in your browser

Dot product in LaTeX: \vec{a} \cdot \vec{b}; cross product: \vec{a} \times \vec{b}. Vectors in LaTeX with \vec, \mathbf, the physics package, and common fixes.

Draw molecular orbital (MO) diagrams in LaTeX. Learn how to create energy level diagrams for chemistry with TikZ and modiagram.

What the phi symbol means: the golden ratio 1.618, φ vs ϕ, magnetic flux, the normal CDF, Euler's totient. How to type φ and Φ on Mac, Windows, LaTeX.