Step-by-step guide to drawing graph-theory figures in LaTeX with TikZ—undirected, directed, and weighted graphs—with examples and DrawFig one-click TikZ export.
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, arrows.meta, positioning}
\usegdlibrary{layered, force}
Notes:
- tikz is the core package
- graphs and graphdrawing provide graph-specific syntax
- arrows.meta gives nicer arrow styles
- positioning supports relative placement
- layered and force are automatic layout engines (require LuaLaTeX)
Note: The graphdrawing library needs LuaLaTeX. With pdfLaTeX you must specify node coordinates manually. Overleaf supports LuaLaTeX: Menu → Compiler → LuaLaTeX.
\begin{tikzpicture}
% Nodes
\node[circle, draw, minimum size=1cm] (A) at (0,0) {$v_1$};
\node[circle, draw, minimum size=1cm] (B) at (2,1) {$v_2$};
\node[circle, draw, minimum size=1cm] (C) at (4,0) {$v_3$};
\node[circle, draw, minimum size=1cm] (D) at (2,-1) {$v_4$};
% Edges
\draw (A) -- (B);
\draw (A) -- (D);
\draw (B) -- (C);
\draw (C) -- (D);
\draw (B) -- (D);
\end{tikzpicture}
What this does:
- \node[circle, draw] creates a bordered circular node
- at (x,y) sets absolute coordinates (centimetres)
- \draw (A) -- (B) draws a segment between nodes A and B
graphs syntax (more concise)\begin{tikzpicture}
\graph[spring layout, node distance=2cm] {
v1 -- v2 -- v3 -- v4 -- v1;
v1 -- v3;
};
\end{tikzpicture}
spring layout computes positions automatically—good for natural-looking layouts (LuaLaTeX required).
\draw:
\begin{tikzpicture}[
->, % global arrow direction
>=Stealth, % Stealth arrow tip
node distance=2.5cm,
every node/.style={circle, draw, minimum size=1cm}
]
\node (S) {$s$};
\node (A) [right of=S] {$a$};
\node (B) [above right of=A] {$b$};
\node (C) [below right of=A] {$c$};
\node (T) [right of=B] {$t$};
\draw (S) -> (A);
\draw (A) -> (B);
\draw (A) -> (C);
\draw (B) -> (T);
\draw (C) -> (T);
\end{tikzpicture}
Key options:
- -> sets global arrow direction
- >=Stealth picks the arrow shape
- right of=S is relative positioning from the positioning library
\begin{tikzpicture}[
every node/.style={circle, draw, minimum size=0.9cm},
node distance=3cm
]
\node (A) {A};
\node (B) [right of=A] {B};
\node (C) [below of=A] {C};
\node (D) [right of=C] {D};
\draw (A) -- node[midway, above, draw=none, fill=white] {5} (B);
\draw (A) -- node[midway, left, draw=none, fill=white] {3} (C);
\draw (B) -- node[midway, right, draw=none, fill=white] {7} (D);
\draw (C) -- node[midway, below, draw=none, fill=white] {2} (D);
\draw (B) -- node[midway, above right, draw=none, fill=white] {4} (C);
\end{tikzpicture}
Tips:
- node[midway, above] places a label node on the edge
- draw=none, fill=white removes the label border and keeps a clean look
- Replace midway with near start or near end when needed
| Need | TikZ |
|---|---|
| Circular node | \node[circle, draw] |
| Square node | \node[rectangle, draw] |
| Red fill | \node[circle, draw, fill=red!20] |
| Dashed edge | \draw[dashed] (A) -- (B) |
| Curved edge | \draw (A) to[bend left=30] (B) |
| Self-loop | \draw (A) to[loop above] (A) |
| Bidirectional | \draw[<->] (A) -- (B) |
| Thick edge | \draw[line width=2pt] (A) -- (B) |
\begin{tikzpicture}[
->, >=Stealth,
node distance=2cm,
every node/.style={circle, draw, minimum size=1cm, font=\small}
]
\node (v1) at (0,0) {$v_1$};
\node (v2) at (2,1) {$v_2$};
\node (v3) at (4,0) {$v_3$};
\node (v4) at (2,-1) {$v_4$};
\draw[->] (v1) -- (v2);
\draw[->] (v2) -- (v3);
\draw[->] (v3) -- (v4);
\draw[->] (v4) -- (v1);
\draw[->, bend left=20] (v1) -- (v3);
\end{tikzpicture}
The output works with TeX Live, MiKTeX, and Overleaf.
layered layout stacks nodes by level:
\begin{tikzpicture}
\graph[layered layout, grow=right, sibling distance=1.5cm, level distance=2.5cm]{
"Input" -> {"Preprocess", "Feature extraction"};
"Preprocess" -> "Normalise";
"Feature extraction" -> {"CNN features", "Hand-crafted features"};
{"Normalise", "CNN features", "Hand-crafted features"} -> "Fusion";
"Fusion" -> "Classifier" -> "Output";
};
\end{tikzpicture}
grow=right grows left to right; sibling distance and level distance control spacing.