← Back to blog

Blog & updates

Drawing graph-theory figures with TikZ: a complete tutorial from scratch

2026-04-19

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.

Drawing graph-theory figures with TikZ: a complete tutorial from scratch

Introduction

High-quality figures are essential in algorithm papers and graph-theory articles. TikZ is one of the most powerful drawing packages in the LaTeX ecosystem: it produces vector graphics that embed directly in your paper with no resolution worries. The learning curve is steep, though—even basic syntax can intimidate beginners. This article focuses on graph-theory figures, walks you through TikZ from zero, and shows how DrawFig can dramatically cut the cost of hand-written code.

1 — Setup: load TikZ

Add the following to your LaTeX preamble:
\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.

2 — Undirected graphs

2.1 Basic undirected graph

The simplest approach: set each node’s coordinates and draw edges by hand.
\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

2.2 Using the 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).

3 — Directed graphs

For directed graphs, use arrow styles on \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

4 — Weighted graphs

Edge labels for weights are among the most common graph-theory needs:
\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

5 — Graph-theory style cheat sheet

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)

6 — Auto-generate TikZ with DrawFig

Hand-written TikZ is powerful, but coordinate math and styling on complex graphs take a long time. DrawFig offers a faster path: Workflow:
  1. Generate or draw on canvas — Open drawfig.com/editor.html, describe the graph in natural language or drag nodes manually.
  2. Refine on canvas — Move nodes, edit labels, set colours and line styles.
  3. Export TikZ — Click Export TikZ to get full LaTeX code.
  4. Paste into your paper — Compile for publication-quality vector output.
Example DrawFig export:
\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.

7 — Advanced: layered layout for DAGs

Directed acyclic graphs (DAGs) are common in algorithm papers. The 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.

Closing

TikZ is powerful but the curve is real. A sensible path: master basic nodes and edges, then styling, then automatic layout libraries when you need them. If you are on a deadline, DrawFig visual editing + TikZ export is often the fastest route—no syntax to memorise, drag to get code. Try DrawFig TikZ export → drawfig.com/editor.html Canvas drag-and-drop editing and SVG/PNG/PDF export are free with no sign-in. TikZ import (5 credits/use), TikZ export (3 credits/use), and AI canvas generation (5 credits/use) require sign-in. You receive 30 credits daily (accumulated). See credit rules.
See also: AI dialog graph generation guide · Graph drawing tool comparison