Learn TikZ
TikZ is a powerful package for creating graphics directly in LaTeX. From simple diagrams to complex illustrations, TikZ can do it all.
What is TikZ?
TikZ (pronounced "teeks") stands for "TikZ ist kein Zeichenprogramm" (German for "TikZ is not a drawing program"). It's a LaTeX package that allows you to create high-quality graphics programmatically using a simple syntax.
Why Use TikZ?
- Vector Graphics – Produces crisp, scalable graphics that look perfect at any size.
- Consistent Styling – Graphics match your document's fonts and style automatically.
- Programmable – Use loops, conditionals, and calculations to generate complex graphics.
- Reproducible – Graphics are defined in code, making them easy to version control and modify.
Getting Started
First, include the TikZ package in your preamble:
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes, positioning}Your First TikZ Drawing
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\end{tikzpicture}This draws a simple square. The -- operator connects points, and cycle closes the path.
Basic Shapes
\begin{tikzpicture}
% Rectangle
\draw (0,0) rectangle (2,1);
% Circle
\draw (4,0.5) circle (0.5);
% Ellipse
\draw (6,0.5) ellipse (0.7 and 0.4);
% Arc
\draw (8,0) arc (0:180:0.5);
\end{tikzpicture}Styling Lines and Fills
\begin{tikzpicture}
% Colored and filled
\draw[blue, thick, fill=blue!20] (0,0) rectangle (2,1);
% Dashed line
\draw[dashed, red] (3,0) -- (5,1);
% Dotted with arrow
\draw[dotted, ->, thick] (6,0) -- (8,1);
% Custom line width
\draw[line width=2pt] (9,0) -- (11,1);
\end{tikzpicture}Nodes and Labels
Nodes are the building blocks for diagrams:
\begin{tikzpicture}
% Simple node
\node at (0,0) {Hello};
% Styled node
\node[draw, circle] at (2,0) {A};
% Rectangle node with fill
\node[draw, rectangle, fill=yellow!30] at (4,0) {Box};
% Named nodes for connections
\node[draw, circle] (a) at (0,-2) {1};
\node[draw, circle] (b) at (2,-2) {2};
\draw[->] (a) -- (b);
\end{tikzpicture}Flowcharts
\begin{tikzpicture}[
node distance=2cm,
startstop/.style={rectangle, rounded corners, draw, fill=red!30},
process/.style={rectangle, draw, fill=blue!30},
decision/.style={diamond, draw, fill=green!30, aspect=2}
]
\node[startstop] (start) {Start};
\node[process, below of=start] (process1) {Process};
\node[decision, below of=process1] (decision) {Decision?};
\node[process, below of=decision] (process2) {Action};
\node[startstop, below of=process2] (stop) {End};
\draw[->] (start) -- (process1);
\draw[->] (process1) -- (decision);
\draw[->] (decision) -- node[right] {Yes} (process2);
\draw[->] (process2) -- (stop);
\draw[->] (decision.east) -- ++(1,0) |- node[near start, above] {No} (process1.east);
\end{tikzpicture}Graphs and Trees
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{trees}
\begin{tikzpicture}
\graph[tree layout, grow=down, sibling distance=2cm] {
root -> {
child1 -> {grandchild1, grandchild2},
child2,
child3 -> grandchild3
}
};
\end{tikzpicture}Coordinate Systems
\begin{tikzpicture}
% Cartesian coordinates
\draw (0,0) -- (2,1);
% Polar coordinates (angle:radius)
\draw (0,0) -- (45:2);
% Relative coordinates
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
% Named coordinates
\coordinate (A) at (0,0);
\coordinate (B) at (2,1);
\draw (A) -- (B);
\end{tikzpicture}Loops and Calculations
\begin{tikzpicture}
% Draw multiple circles using foreach
\foreach \x in {0,1,2,3,4} {
\draw (\x,0) circle (0.3);
}
% Pentagon using calculations
\foreach \i in {1,...,5} {
\draw ({90+72*\i}:1) -- ({90+72*(\i+1)}:1);
}
\end{tikzpicture}Useful Libraries
| Library | Purpose |
|---|---|
arrows.meta | Advanced arrow tips |
positioning | Relative node positioning |
shapes | Additional node shapes |
calc | Coordinate calculations |
decorations | Path decorations |
patterns | Fill patterns |
Next Steps
- Learn PGFPlots – Create data visualizations
- Learn LaTeX Basics – Review fundamentals
Create TikZ diagrams with AI
Octree's AI can help you generate TikZ code from descriptions. Just describe what you want!
Try Octree Free →