- name
- tikz
- description
- LaTeX TikZ/PGF package for programmatic vector graphics and diagrams. Use when helping users draw flowcharts, trees, graphs, automata, circuits, geometric figures, or any custom diagram in LaTeX.
# TikZ/PGF — Vector Graphics & Diagrams
**CTAN:** https://ctan.org/pkg/pgf
**Manual:** `texdoc tikz` (~1300 pages)
## Setup
```latex
\usepackage{tikz}
% Load libraries as needed:
\usetikzlibrary{arrows.meta, calc, positioning, decorations.pathmorphing,
patterns, shapes.geometric, shapes.misc, fit, backgrounds,
automata, trees, mindmap, circuits.logic.US}
```
## Basic Drawing
### Minimal Example
```latex
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- cycle; % triangle
\end{tikzpicture}
```
### Path Operations
| Operation | Syntax | Example |
|-----------|--------|---------|
| Line | `--` | `(0,0) -- (1,1)` |
| Horizontal-vertical | <code>-\|</code> | `(0,0) -| (1,1)` (go right then up) |
| Vertical-horizontal | <code>\|-</code> | `(0,0) |- (1,1)` (go up then right) |
| Curve | `.. controls .. ..` | `(0,0) .. controls (0.5,1) .. (1,0)` |
| Smooth curve | `to[out=..,in=..]` | `(0,0) to[out=90,in=180] (2,1)` |
| Arc | `arc` | `(0,0) arc (0:90:1cm)` start:end:radius |
| Circle | `circle` | `(0,0) circle[radius=1]` |
| Ellipse | `ellipse` | `(0,0) ellipse[x radius=2, y radius=1]` |
| Rectangle | `rectangle` | `(0,0) rectangle (2,1)` |
| Grid | `grid` | `(0,0) grid (3,3)` |
| Parabola | `parabola` | `(0,0) parabola (2,2)` |
| Sin/Cos | `sin`/`cos` | `(0,0) sin (1,1)` |
| Plot | `plot` | `plot[domain=0:3] (\x, {\x^2})` |
| Cycle | `cycle` | close path back to start |
### Path Actions
| Command | Effect |
|---------|--------|
| `\draw` | Stroke the path |
| `\fill` | Fill the path |
| `\filldraw` | Fill and stroke |
| `\path` | Invisible path (for coordinates, nodes) |
| `\clip` | Clip subsequent drawing to path |
| `\shade` | Gradient fill |
| `\shadedraw` | Gradient fill + stroke |
| `\node` | Shorthand for `\path node` |
| `\coordinate` | Shorthand for `\path coordinate` |
### Common Draw Options
```latex
\draw[
color=blue, % or just: blue
line width=1pt, % or: thin, thick, very thick, ultra thick
dashed, % or: dotted, dash dot, dash dot dot
dash pattern={on 3pt off 2pt},
rounded corners=5pt,
-> % arrow tip (see arrows section)
opacity=0.5,
double,
double distance=2pt,
] (0,0) -- (2,2);
```
### Common Fill Options
```latex
\fill[
fill=blue!20, % 20% blue
fill opacity=0.5,
pattern=north lines, % requires patterns library
pattern color=gray,
] (0,0) rectangle (2,1);
```
## Coordinate Systems
```latex
% Cartesian (default)
(2, 3)
% Polar (angle:radius)
(45:2cm)
% Relative (shift from last point)
++(1,0) % move reference point
+(1,0) % don't move reference point
% Named
\coordinate (A) at (1,2);
\draw (A) -- (2,3);
% Intersection (calc library)
\usetikzlibrary{calc}
($(A)!0.5!(B)$) % midpoint of A and B
($(A)!0.5!90:(B)$) % midpoint rotated 90°
($(A) + (1,2)$) % A shifted by (1,2)
($(A)!1cm!(B)$) % 1cm from A toward B
% Perpendicular coordinates (A -| B) = (x of B, y of A)
(A |- B) % (x of A, y of B)
% Barycentric
(barycentric cs:A=1,B=1,C=1) % centroid
```
## Nodes
### Basic Nodes
```latex
\node (name) at (0,0) {Text};
% Node options
\node[
draw, % draw border
fill=yellow!20,
rectangle, % shape (default)
circle,
ellipse,
rounded corners,
minimum width=2cm,
minimum height=1cm,
inner sep=5pt, % padding
outer sep=2pt, % margin
text=red,
font=\bfseries\small,
align=center, % for multi-line: use \\ in text
text width=3cm,
anchor=north, % positioning anchor
rotate=45,
] (mynode) at (1,2) {Hello\\World};
```
### Node Anchors
```
north west north north east
┌──────────┐
west │ center │ east
└──────────┘
south west south south east
```
Also: `base`, `mid`, `text`, and angle anchors like `45`, `135`.
### Nodes on Paths
```latex
\draw (0,0) -- node[above] {label} (3,0);
\draw (0,0) -- node[midway, sloped, above] {sloped text} (3,2);
\draw (0,0) -- node[pos=0.3, below] {at 30\%} (3,0);
\draw (0,0) to[out=90,in=0] node[near start, left] {A} (2,2);
```
### Common Shapes (requires libraries)
```latex
\usetikzlibrary{shapes.geometric, shapes.misc}
\node[diamond, draw] {D};
\node[star, draw, star points=5] {S};
\node[regular polygon, regular polygon sides=6, draw] {Hex};
\node[trapezium, draw] {T};
\node[cylinder, draw, shape border rotate=90] {DB};
\node[cloud, draw, cloud puffs=10] {Cloud};
\node[cross out, draw] at (0,0) {};
\node[strike out, draw] at (0,0) {};
```
## Arrows
```latex
\usetikzlibrary{arrows.meta}
% Arrow tips (arrows.meta syntax)
\draw[->] (0,0) -- (1,0); % default
\draw[-Stealth] (0,0) -- (1,0); % filled triangle
\draw[-Latex] (0,0) -- (1,0); % larger filled
\draw[-{Stealth[length=5mm]}] (0,0) -- (1,0);
\draw[{Latex[red]}-{Latex[blue]}] (0,0) -- (1,0); % colored
\draw[-{>[scale=2]}] (0,0) -- (1,0);
% Common tips: >, Stealth, Latex, To, Circle, Square, |, Hooks
% Modifiers: [length=..], [width=..], [open], [fill=..], [scale=..]
```
## Styles
```latex
% Define in preamble or tikzpicture options
\tikzset{
mybox/.style = {draw, fill=blue!20, rounded corners, minimum width=2cm},
myarrow/.style = {-Stealth, thick, red},
highlight/.style = {fill=yellow, draw=orange, line width=2pt},
}
% Use
\node[mybox] {Box};
\draw[myarrow] (0,0) -- (1,1);
% Style with parameters
\tikzset{
box/.style = {draw, fill=#1!20, minimum width=1.5cm},
box/.default = blue,
}
\node[box] {Blue}; % default
\node[box=red] {Red}; % override
% every node/.style applies to all nodes
\begin{tikzpicture}[every node/.style={font=\small}]
```
## Foreach Loops
```latex
% Basic
\foreach \x in {0,1,2,3}
\draw (\x, 0) circle (0.3);
% With step
\foreach \x in {0,0.5,...,3}
\fill (\x,0) circle (1pt);
% Multiple variables
\foreach \x/\y in {0/A, 1/B, 2/C}
\node at (\x, 0) {\y};
% Counter
\foreach \x [count=\i] in {a,b,c,d}
\node at (\i, 0) {\x};
% Evaluate
\foreach \x [evaluate=\x as \y using \x*\x] in {1,...,5}
\fill (\x, \y/5) circle (2pt);
% Remember
\foreach \x [remember=\x as \lastx (initially 0)] in {1,...,5}
\draw (\lastx, 0) -- (\x, 0);
```
## Scopes and Transformations
```latex
\begin{tikzpicture}
\draw (0,0) -- (1,0);
\begin{scope}[shift={(2,0)}, rotate=45, scale=0.5, red, thick]
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\end{scope}
% Transformations
% shift={(x,y)}, xshift=1cm, yshift=2cm
% rotate=45, rotate around={45:(1,1)}
% scale=2, xscale=2, yscale=0.5
% xslant=0.5, yslant=0.5
\end{tikzpicture}
```
## Clipping
```latex
\begin{tikzpicture}
\clip (0,0) circle (1.5cm);
% Everything below is clipped to the circle
\fill[blue!30] (-2,-2) rectangle (2,2);
\draw[step=0.5, gray] (-2,-2) grid (2,2);
\end{tikzpicture}
```
## Layers (backgrounds library)
```latex
\usetikzlibrary{backgrounds}
\begin{tikzpicture}
\node[draw, fill=white] (A) {Foreground};
\begin{pgfonlayer}{background}
\fill[yellow!30] (A.south west) rectangle (A.north east);
\end{pgfonlayer}
\end{tikzpicture}
```
## Pics
```latex
\tikzset{
myshape/.pic = {
\draw (-0.5,-0.5) rectangle (0.5,0.5);
\draw (0,0) circle (0.3);
}
}
\begin{tikzpicture}
\pic at (0,0) {myshape};
\pic[rotate=45, scale=1.5] at (2,0) {myshape};
\end{tikzpicture}
```
## Decorations
```latex
\usetikzlibrary{decorations.pathmorphing, decorations.markings, decorations.text}
% Wavy/zigzag lines
\draw[decorate, decoration={zigzag, amplitude=2mm, segment length=5mm}]
(0,0) -- (4,0);
Voir sur GitHub