Setup and Installation
TikZ comes bundled with most LaTeX distributions (TeX Live, MiKTeX, MacTeX). To start using TikZ, simply add the package to your preamble:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% Your drawing code here
\end{tikzpicture}
\end{document}
Loading TikZ Libraries
TikZ provides specialized libraries for specific functionalities. Load them with \usetikzlibrary{}:
\usepackage{tikz}
\usetikzlibrary{
shapes, % Advanced shapes
arrows, % Arrow styles
positioning, % Relative positioning
calc, % Coordinate calculations
decorations, % Path decorations
patterns, % Fill patterns
shadows % Shadow effects
}
| Library |
Purpose |
Common Use Cases |
shapes |
Advanced node shapes |
Flowcharts, diagrams, organizational charts |
arrows |
Arrow tip styles |
Directed graphs, flowcharts, process diagrams |
positioning |
Relative node placement |
Complex layouts, hierarchical diagrams |
calc |
Coordinate calculations |
Mathematical diagrams, precise positioning |
decorations |
Path decorations |
Wavy lines, spring effects, ornamental paths |
patterns |
Fill patterns |
Hatching, cross-hatching, textured fills |
Basic Shapes and Lines
TikZ uses the \draw command to create shapes. Every TikZ command ends with a semicolon (;). The basic syntax is \draw [options] (coordinates) -- (coordinates) ...
Lines and Paths
The most fundamental TikZ operation is drawing lines between coordinates:
\begin{tikzpicture}
% Straight line from (0,0) to (2,0)
\draw (0,0) -- (2,0);
% Line path with multiple points
\draw (0,1) -- (1,2) -- (2,1) -- (3,2);
% Curved path (smooth curve)
\draw (0,2) .. controls (1,3) and (2,3) .. (3,2);
% Right angle path (horizontal then vertical)
\draw (0,0) -| (2,2);
% Right angle path (vertical then horizontal)
\draw (0,0) |- (2,2);
\end{tikzpicture}
💡 Coordinate Tip: TikZ uses Cartesian coordinates by default. The unit is centimeters: (2,3) means 2cm right and 3cm up from the origin.
Rectangles and Circles
\begin{tikzpicture}
% Rectangle: specify opposite corners
\draw (0,0) rectangle (2,1.5);
% Circle: specify center and radius
\draw (4,0.75) circle (0.75);
% Ellipse: specify center and radii (x-radius and y-radius)
\draw (7,0.75) ellipse (1.5 and 0.75);
% Rounded rectangle
\draw[rounded corners=5pt] (0,2) rectangle (2,3.5);
% Arc: start angle, end angle, and radius
\draw (4,2) arc (0:270:0.75);
\end{tikzpicture}
Polygons and Custom Shapes
\begin{tikzpicture}
% Triangle (using cycle to close path)
\draw (0,0) -- (2,0) -- (1,1.7) -- cycle;
% Pentagon
\draw (3,0) -- (4,0) -- (4.5,0.9) -- (3.5,1.5) -- (2.5,0.9) -- cycle;
% Hexagon (using regular polygon)
\draw (6,0.75) circle (1);
\foreach \angle in {0,60,...,300}
\draw (6,0.75) -- ++(\angle:1);
% Star shape
\draw (9,0.75)
-- ++(18:1) -- ++(162:0.4)
-- ++(90:1) -- ++(234:0.4)
-- ++(162:1) -- ++(306:0.4)
-- ++(234:1) -- ++(18:0.4)
-- ++(306:1) -- ++(90:0.4) -- cycle;
\end{tikzpicture}
Coordinate Systems
TikZ supports multiple coordinate systems for flexible positioning. Understanding these systems is crucial for creating complex diagrams.
Cartesian Coordinates
The default system using (x,y) where x and y are in centimeters:
\begin{tikzpicture}
% Absolute coordinates
\draw (0,0) -- (3,2);
% Relative coordinates (++ moves current position)
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
% Relative coordinates (+ doesn't move current position)
\draw (2,0) -- +(1,1) (2,0) -- +(1,-1);
\end{tikzpicture}
Polar Coordinates
Specify points using angle and distance: (angle:radius)
\begin{tikzpicture}
% Draw lines at different angles
\draw (0,0) -- (0:2); % 0 degrees, 2cm
\draw (0,0) -- (45:2); % 45 degrees, 2cm
\draw (0,0) -- (90:2); % 90 degrees, 2cm
\draw (0,0) -- (135:2); % 135 degrees, 2cm
% Circle using polar coordinates
\foreach \angle in {0,30,...,330}
\draw (0,0) -- (\angle:1.5);
\end{tikzpicture}
Named Coordinates
Assign names to coordinates for reuse and clarity:
\begin{tikzpicture}
% Define named coordinates
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (1.5,2.6);
% Use them to draw a triangle
\draw (A) -- (B) -- (C) -- cycle;
% Calculate midpoint using calc library
\coordinate (M) at ($(A)!0.5!(B)$);
\draw[red, thick] (C) -- (M);
\end{tikzpicture}
Colors, Fills, and Styling
TikZ provides extensive styling options to make your diagrams professional and visually appealing.
Line Styles
\begin{tikzpicture}
% Line width
\draw[thin] (0,3) -- (2,3);
\draw[thick] (0,2.5) -- (2,2.5);
\draw[very thick] (0,2) -- (2,2);
\draw[ultra thick] (0,1.5) -- (2,1.5);
\draw[line width=5pt] (0,1) -- (2,1);
% Line styles
\draw[dashed] (3,3) -- (5,3);
\draw[dotted] (3,2.5) -- (5,2.5);
\draw[dash dot] (3,2) -- (5,2);
\draw[dash dot dot] (3,1.5) -- (5,1.5);
% Custom dash pattern
\draw[dash pattern=on 5pt off 3pt on 1pt off 3pt] (3,1) -- (5,1);
\end{tikzpicture}
Colors and Fills
\begin{tikzpicture}
% Basic colors
\draw[red] (0,0) circle (0.5);
\draw[blue] (1.5,0) circle (0.5);
\draw[green] (3,0) circle (0.5);
% Fill colors
\draw[fill=yellow] (5,0) circle (0.5);
\draw[fill=cyan] (6.5,0) circle (0.5);
% Different border and fill
\draw[draw=red, fill=blue!20] (0,1.5) rectangle (1,2.5);
% Color mixing (50% blue + 50% white)
\draw[fill=blue!50] (2,1.5) rectangle (3,2.5);
% Opacity
\draw[fill=red, opacity=0.5] (4,1.5) rectangle (5,2.5);
\draw[fill=blue, opacity=0.5] (4.5,1.5) rectangle (5.5,2.5);
% Fill and draw separately
\filldraw[fill=green!30, draw=green!50!black, thick]
(7,1.5) rectangle (8,2.5);
\end{tikzpicture}
Patterns and Gradients
\usetikzlibrary{patterns}
\begin{tikzpicture}
% Hatch patterns
\draw[pattern=horizontal lines] (0,0) rectangle (1.5,1.5);
\draw[pattern=vertical lines] (2,0) rectangle (3.5,1.5);
\draw[pattern=north east lines] (4,0) rectangle (5.5,1.5);
\draw[pattern=grid] (6,0) rectangle (7.5,1.5);
\draw[pattern=crosshatch] (8,0) rectangle (9.5,1.5);
% Gradient shading
\shade[left color=blue, right color=red] (0,2) rectangle (3,3);
\shade[inner color=yellow, outer color=red] (4,2.5) circle (1);
\shade[top color=blue, bottom color=white] (7,2) rectangle (10,3);
\end{tikzpicture}
Nodes and Text
Nodes are TikZ's way of placing text and creating labeled elements. They're essential for flowcharts, diagrams, and annotations.
Basic Node Syntax
\begin{tikzpicture}
% Simple text node
\node at (0,0) {Hello TikZ!};
% Named node for later reference
\node (A) at (3,0) {Node A};
\node (B) at (6,0) {Node B};
\draw[->] (A) -- (B);
% Node with shape and style
\node[draw, circle] at (0,2) {Circle};
\node[draw, rectangle] at (3,2) {Rectangle};
\node[draw, ellipse] at (6,2) {Ellipse};
% Styled node
\node[draw, fill=blue!20, rounded corners, thick] at (3,4) {Styled Node};
\end{tikzpicture}
Node Shapes
TikZ provides many predefined shapes (requires \usetikzlibrary{shapes}):
\usetikzlibrary{shapes.geometric, shapes.misc}
\begin{tikzpicture}
% Geometric shapes
\node[draw, circle] at (0,0) {Circle};
\node[draw, ellipse] at (2.5,0) {Ellipse};
\node[draw, rectangle] at (5,0) {Rectangle};
\node[draw, diamond] at (7.5,0) {Diamond};
% More shapes
\node[draw, trapezium] at (0,2) {Trapezium};
\node[draw, star] at (2.5,2) {Star};
\node[draw, regular polygon, regular polygon sides=6] at (5,2) {Hexagon};
\node[draw, cloud] at (7.5,2) {Cloud};
% Decision and process nodes
\node[draw, diamond, aspect=2] at (0,4) {Decision};
\node[draw, rectangle, rounded corners] at (3,4) {Process};
\node[draw, ellipse] at (6,4) {Terminal};
\end{tikzpicture}
Node Positioning
\usetikzlibrary{positioning}
\begin{tikzpicture}[node distance=2cm]
% Base node
\node[draw] (center) {Center};
% Relative positioning
\node[draw, above=of center] (top) {Above};
\node[draw, below=of center] (bottom) {Below};
\node[draw, left=of center] (left) {Left};
\node[draw, right=of center] (right) {Right};
% Diagonal positioning
\node[draw, above right=of center] (topright) {Above Right};
% Connect them
\draw[->] (center) -- (top);
\draw[->] (center) -- (bottom);
\draw[->] (center) -- (left);
\draw[->] (center) -- (right);
\end{tikzpicture}
Text Formatting in Nodes
\begin{tikzpicture}
% Text alignment
\node[draw, text width=3cm, align=left] at (0,0)
{Left aligned text in a node with fixed width};
\node[draw, text width=3cm, align=center] at (4,0)
{Center aligned text in a node with fixed width};
\node[draw, text width=3cm, align=right] at (8,0)
{Right aligned text in a node with fixed width};
% Font styles
\node[draw, font=\large\bfseries] at (2,3) {Large Bold};
\node[draw, font=\small\itshape] at (6,3) {Small Italic};
% Colors
\node[draw, text=red] at (2,2) {Red Text};
\node[draw, fill=yellow, text=blue] at (6,2) {Blue on Yellow};
\end{tikzpicture}
Arrows and Connectors
Arrows are essential for creating flowcharts, diagrams, and showing relationships between elements.
Arrow Styles
\usetikzlibrary{arrows.meta}
\begin{tikzpicture}
% Basic arrows
\draw[->] (0,3) -- (2,3) node[right] {Standard};
\draw[<-] (0,2.5) -- (2,2.5) node[right] {Reversed};
\draw[<->] (0,2) -- (2,2) node[right] {Bidirectional};
% Arrow tip styles
\draw[-stealth] (0,1.5) -- (2,1.5) node[right] {Stealth};
\draw[-latex] (0,1) -- (2,1) node[right] {LaTeX};
\draw[-triangle 45] (0,0.5) -- (2,0.5) node[right] {Triangle 45};
\draw[-angle 90] (0,0) -- (2,0) node[right] {Angle 90};
% Custom arrow size
\draw[-{Stealth[length=5mm]}] (4,3) -- (6,3) node[right] {Large Stealth};
\draw[-{Latex[length=3mm, width=5mm]}] (4,2.5) -- (6,2.5) node[right] {Wide LaTeX};
% Double arrows
\draw[<->, >=stealth] (4,2) -- (6,2) node[right] {Double Stealth};
% Thick arrows
\draw[->, line width=2pt] (4,1.5) -- (6,1.5) node[right] {Thick};
\end{tikzpicture}
Connecting Nodes
\begin{tikzpicture}[node distance=3cm]
% Define nodes
\node[draw, circle] (A) {A};
\node[draw, circle, right of=A] (B) {B};
\node[draw, circle, below of=A] (C) {C};
\node[draw, circle, below of=B] (D) {D};
% Connect with different styles
\draw[->] (A) -- (B);
\draw[->, red] (A) -- (C);
\draw[->, blue, dashed] (B) -- (D);
\draw[->, green, thick] (C) -- (D);
% Curved connections
\draw[->, bend left] (A) to (D);
\draw[->, bend right=45] (C) to (B);
% Loop (self-reference)
\draw[->] (B) to [out=45, in=90, looseness=5] (B);
\end{tikzpicture}
Labeled Arrows
\begin{tikzpicture}
\node[draw] (A) at (0,0) {Start};
\node[draw] (B) at (4,0) {End};
% Label above arrow
\draw[->] (A) -- node[above] {Process} (B);
% Label below arrow
\draw[->, blue] (A) to[bend right] node[below] {Alternative} (B);
% Multiple labels
\draw[->, red] (0,2) -- node[above] {Label 1}
node[below] {Label 2} (4,2);
% Positioned labels
\draw[->] (0,3) -- node[pos=0.25, above] {25%}
node[pos=0.75, above] {75%} (4,3);
\end{tikzpicture}
Creating Flowcharts
Flowcharts are one of the most common applications of TikZ. Let's create a professional flowchart from scratch.
Basic Flowchart Example
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}
\begin{tikzpicture}[
node distance=1.5cm,
startstop/.style={
rectangle, rounded corners,
minimum width=3cm, minimum height=1cm,
text centered, draw=black, fill=red!30
},
process/.style={
rectangle, minimum width=3cm, minimum height=1cm,
text centered, draw=black, fill=blue!30
},
decision/.style={
diamond, minimum width=3cm, minimum height=1cm,
text centered, draw=black, fill=green!30,
aspect=2
},
arrow/.style={thick,->,>=stealth}
]
% Nodes
\node (start) [startstop] {Start};
\node (input) [process, below=of start] {Read Input};
\node (decide) [decision, below=of input] {Valid Input?};
\node (process1) [process, below=of decide] {Process Data};
\node (output) [process, below=of process1] {Display Output};
\node (stop) [startstop, below=of output] {Stop};
\node (error) [process, right=3cm of decide] {Show Error};
% Arrows
\draw [arrow] (start) -- (input);
\draw [arrow] (input) -- (decide);
\draw [arrow] (decide) -- node[right] {Yes} (process1);
\draw [arrow] (decide) -- node[above] {No} (error);
\draw [arrow] (process1) -- (output);
\draw [arrow] (output) -- (stop);
\draw [arrow] (error) |- (input);
\end{tikzpicture}
💡 Flowchart Best Practice: Define custom styles at the beginning with [startstop/.style={...}] to maintain consistency and make your code easier to modify.
Advanced Flowchart with Parallel Processes
\begin{tikzpicture}[
node distance=1.5cm and 2cm,
process/.style={rectangle, draw, fill=blue!20, minimum width=2.5cm, minimum height=1cm},
decision/.style={diamond, draw, fill=green!20, aspect=2},
startstop/.style={ellipse, draw, fill=red!20},
arrow/.style={->,>=stealth, thick}
]
\node [startstop] (start) {Start};
\node [process, below=of start] (init) {Initialize};
\node [decision, below=of init] (split) {Split?};
% Left branch
\node [process, below left=of split] (proc1) {Process A};
\node [process, below=of proc1] (result1) {Result A};
% Right branch
\node [process, below right=of split] (proc2) {Process B};
\node [process, below=of proc2] (result2) {Result B};
% Merge
\node [process, below=4cm of split] (merge) {Merge Results};
\node [startstop, below=of merge] (end) {End};
\draw [arrow] (start) -- (init);
\draw [arrow] (init) -- (split);
\draw [arrow] (split) -| node[near start, above left] {Yes} (proc1);
\draw [arrow] (split) -| node[near start, above right] {Yes} (proc2);
\draw [arrow] (proc1) -- (result1);
\draw [arrow] (proc2) -- (result2);
\draw [arrow] (result1) |- (merge);
\draw [arrow] (result2) |- (merge);
\draw [arrow] (merge) -- (end);
\end{tikzpicture}
Technical Diagrams
TikZ excels at creating precise technical diagrams for education, documentation, and presentations.
Tree Diagram
\usetikzlibrary{trees}
\begin{tikzpicture}[
level 1/.style={sibling distance=4cm, level distance=1.5cm},
level 2/.style={sibling distance=2cm},
edge from parent/.style={draw, -latex},
every node/.style={draw, rectangle, rounded corners,
minimum width=2cm, minimum height=0.8cm,
align=center, fill=blue!20}
]
\node {Root}
child {node {Child 1}
child {node {Leaf 1.1}}
child {node {Leaf 1.2}}
}
child {node {Child 2}
child {node {Leaf 2.1}}
child {node {Leaf 2.2}}
}
child {node {Child 3}
child {node {Leaf 3.1}}
};
\end{tikzpicture}
Network Diagram
\begin{tikzpicture}[
server/.style={rectangle, draw, fill=blue!30, minimum width=2cm, minimum height=1cm},
client/.style={circle, draw, fill=green!30, minimum size=1cm},
router/.style={diamond, draw, fill=red!30, minimum size=1.5cm, aspect=1}
]
% Central router
\node[router] (router) at (0,0) {Router};
% Servers
\node[server, above left=2cm of router] (s1) {Server 1};
\node[server, above right=2cm of router] (s2) {Server 2};
% Clients
\node[client, below left=2cm of router] (c1) {PC 1};
\node[client, below=2cm of router] (c2) {PC 2};
\node[client, below right=2cm of router] (c3) {PC 3};
% Connections
\draw[thick] (s1) -- (router);
\draw[thick] (s2) -- (router);
\draw[thick] (router) -- (c1);
\draw[thick] (router) -- (c2);
\draw[thick] (router) -- (c3);
% Labels
\draw[<->, dashed, red] (s1) -- node[above] {100 Mbps} (s2);
\end{tikzpicture}
Mathematical Diagram
\begin{tikzpicture}[scale=2]
% Triangle
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (1.5,2.6);
% Draw triangle
\draw[thick] (A) -- (B) -- (C) -- cycle;
% Mark angles
\draw (A) +(0.5,0) arc (0:60:0.5);
\draw (B) +(150:0.5) arc (150:180:0.5);
\draw (C) +(-60:0.5) arc (-60:-120:0.5);
% Labels
\node[below left] at (A) {$A$};
\node[below right] at (B) {$B$};
\node[above] at (C) {$C$};
% Side lengths
\node[below] at ($(A)!0.5!(B)$) {$c = 3$};
\node[above right] at ($(B)!0.5!(C)$) {$a$};
\node[above left] at ($(A)!0.5!(C)$) {$b$};
% Height
\coordinate (M) at ($(A)!0.5!(B)$);
\draw[dashed, red] (C) -- (M);
\node[right, red] at ($(C)!0.5!(M)$) {$h$};
% Right angle marker
\draw (M) rectangle +(0.2,0.2);
\end{tikzpicture}
Advanced Techniques
Using Loops
\begin{tikzpicture}
% Draw grid using loops
\foreach \x in {0,1,...,5}
\foreach \y in {0,1,...,5}
\draw (\x,\y) circle (0.1);
% Pattern with colors
\foreach \angle/\col in {0/red, 45/orange, 90/yellow,
135/green, 180/blue, 225/purple,
270/violet, 315/pink}
\draw[\col, thick] (0,0) -- (\angle:2);
% Spiral
\foreach \i in {1,...,30}
\draw[blue] (0,0) -- (\i*30:\i*0.1);
\end{tikzpicture}
Clipping and Scoping
\begin{tikzpicture}
% Clip to circle
\begin{scope}
\clip (0,0) circle (2);
\fill[blue!20] (-3,-3) rectangle (3,3);
\foreach \x in {-3,-2.5,...,3}
\draw[red] (\x,-3) -- (\x,3);
\end{scope}
% Draw circle border
\draw[very thick] (0,0) circle (2);
% Separate scope with different style
\begin{scope}[xshift=5cm, rotate=45]
\draw[fill=green!30] (0,0) rectangle (2,2);
\node at (1,1) {Rotated};
\end{scope}
\end{tikzpicture}
Layers and Backgrounds
\usetikzlibrary{backgrounds}
\begin{tikzpicture}
% Draw main content
\node[draw, circle, fill=white] (A) at (0,0) {Foreground};
\node[draw, circle, fill=white] (B) at (2,0) {Content};
\draw[thick] (A) -- (B);
% Add background layer
\begin{pgfonlayer}{background}
\fill[yellow!30] (-1,-1) rectangle (3,1);
\draw[dashed] (-1,-1) rectangle (3,1);
\end{pgfonlayer}
\end{tikzpicture}
Further Learning Resources
TikZ has extensive documentation. Check the TikZ & PGF manual (over 1000 pages!) for comprehensive coverage. The texdoc tikz command opens the manual in most LaTeX distributions.
Frequently Asked Questions
How do I install TikZ in LaTeX?
TikZ comes pre-installed with most LaTeX distributions (TeX Live, MiKTeX, MacTeX). Simply add \usepackage{tikz} to your document preamble. If you need to install it manually, use your distribution's package manager: tlmgr install pgf for TeX Live or the MiKTeX Package Manager.
What's the difference between TikZ and PGF?
PGF (Portable Graphics Format) is the low-level layer providing basic drawing commands. TikZ (TikZ ist kein Zeichenprogramm) is the high-level, user-friendly layer built on top of PGF. When you load TikZ, you automatically get PGF. Most users work exclusively with TikZ syntax as it's more intuitive and powerful.
Can I export TikZ pictures as standalone images?
Yes! Use the standalone document class to compile TikZ pictures into standalone PDFs or images. This is perfect for creating reusable graphics.
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
% Your drawing here
\end{tikzpicture}
\end{document}
Compile with pdflatex to get a cropped PDF. Convert to PNG/SVG using external tools like ImageMagick or pdf2svg.
How do I make TikZ pictures responsive in different document sizes?
Use the scale option or set x and y unit vectors. For example: \begin{tikzpicture}[scale=0.8] or \begin{tikzpicture}[x=0.5cm, y=0.5cm]. You can also use \resizebox from the graphicx package to scale the entire picture.
Why is my TikZ compilation slow?
Complex TikZ diagrams can slow compilation. Solutions: (1) Use the externalize library to cache compiled pictures, (2) Compile TikZ pictures separately as PDFs and include them with \includegraphics, (3) Simplify complex paths, (4) Avoid excessive decorations and shadows.
Can I use TikZ with Beamer presentations?
Absolutely! TikZ works seamlessly with Beamer. You can even animate TikZ elements using \only, \visible, and overlay specifications. TikZ's overlay and remember picture options enable advanced positioning across slides.
How do I draw curved arrows between nodes?
Use the bend left, bend right, or in/out options:
\draw[->] (A) to[bend left=30] (B);
\draw[->] (C) to[out=45, in=135] (D);
What are the best practices for complex TikZ projects?
Best practices: (1) Define custom styles early in your preamble, (2) Use named coordinates and nodes for clarity, (3) Comment your code extensively, (4) Break complex diagrams into multiple TikZ pictures, (5) Use scopes to organize related elements, (6) Externalize time-consuming diagrams, (7) Keep a library of reusable components.