How to Draw Commutative Diagrams with tikz-cd in LaTeX
Commutative diagrams are essential in category theory, homological algebra, and algebraic topology. The tikz-cd package provides an elegant, concise syntax for creating publication-quality commutative diagrams directly in LaTeX. It handles arrow positioning, label placement, and diagram layout automatically. Bibby AI supports tikz-cd out of the box with instant preview — you can see your diagram update as you type each arrow, which is invaluable for complex diagram chasing arguments.
Draw Basic Commutative Squares and Triangles
Use tikz-cd's matrix-like syntax to create fundamental commutative diagrams:
\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath, amssymb}
\begin{document}
% Commutative square
\[
\begin{tikzcd}
A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\
C \arrow[r, "k"'] & D
\end{tikzcd}
\]
% Commutative triangle
\[
\begin{tikzcd}
A \arrow[r, "f"] \arrow[dr, "g \circ f"'] & B \arrow[d, "g"] \\
& C
\end{tikzcd}
\]
% Short exact sequence
\[
\begin{tikzcd}
0 \arrow[r] & A \arrow[r, "\iota"] & B \arrow[r, "\pi"] & C \arrow[r] & 0
\end{tikzcd}
\]
\end{document}Add Special Arrows and Diagram Features
Use tikz-cd's arrow modifiers for injections, surjections, dashed arrows, and pullback/pushout corners:
\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath, amssymb}
\begin{document}
% Arrow types
\[
\begin{tikzcd}
A \arrow[r, hook, "\iota"] & B & \text{(injection/mono)} \\
C \arrow[r, two heads, "\pi"] & D & \text{(surjection/epi)} \\
E \arrow[r, dashed, "\exists!"] & F & \text{(unique existence)} \\
G \arrow[r, Rightarrow, "\alpha"] & H & \text{(natural transformation)} \\
I \arrow[r, maps to] & J & \text{(mapsto)}
\end{tikzcd}
\]
% Pullback diagram with corner symbol
\[
\begin{tikzcd}
P \arrow[r] \arrow[d] \arrow[dr, phantom, "\lrcorner", very near start]
& X \arrow[d, "f"] \\
Y \arrow[r, "g"'] & Z
\end{tikzcd}
\]
% Snake lemma diagram
\[
\begin{tikzcd}
& \ker f \arrow[r] \arrow[d] & \ker g \arrow[r] \arrow[d] & \ker h \arrow[d] \\
0 \arrow[r] & A \arrow[r] \arrow[d, "f"] & B \arrow[r] \arrow[d, "g"] & C \arrow[r] \arrow[d, "h"] & 0 \\
0 \arrow[r] & A' \arrow[r] \arrow[d] & B' \arrow[r] \arrow[d] & C' \arrow[r] \arrow[d] & 0 \\
& \text{coker}\,f \arrow[r] & \text{coker}\,g \arrow[r] & \text{coker}\,h
\end{tikzcd}
\]
\end{document}Customize Layout, Spacing, and Style
Control diagram spacing, arrow curvature, label positioning, and global styles:
\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath, amssymb}
\begin{document}
% Custom spacing (column sep, row sep)
\[
\begin{tikzcd}[column sep=large, row sep=large]
A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\
C \arrow[r, "k"'] & D
\end{tikzcd}
\]
% Curved arrows and crossing arrows
\[
\begin{tikzcd}
A \arrow[r, "f", bend left=30] \arrow[r, "g"', bend right=30]
& B \arrow[l, "\eta", dashed, description]
\end{tikzcd}
\]
% Crossing arrows (one passes over another)
\[
\begin{tikzcd}
A \arrow[r] \arrow[d] & B \arrow[d] \\
C \arrow[r] \arrow[ur, crossing over] & D
\end{tikzcd}
\]
% Global style customization
\[
\begin{tikzcd}[
every arrow/.append style={thick, blue},
every label/.append style={font=\scriptsize, red}
]
X \arrow[r, "\phi"] \arrow[d, "\psi"'] & Y \arrow[d, "\rho"] \\
Z \arrow[r, "\sigma"'] & W
\end{tikzcd}
\]
\end{document}💡 Tips
- •Use the prime notation ("f"') to place labels on the opposite side of an arrow — this is essential for commutative squares.
- •The 'description' option places a label on top of the arrow line, useful for natural transformations and 2-cells.
- •Use phantom arrows with \lrcorner for pullback corners and \ulcorner for pushout corners.
- •For very large diagrams, increase column sep and row sep to 'huge' or use explicit measurements like 'column sep=3em'.
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free