Commutative diagram

Commutative diagrams can become complicated. They can have many columns, many rows, and a lot of arrow and labels. In such cases, for loops and calculation options can be a relief.

Here, we position the elements in a matrix, and use a loop for drawing the arrows.

The code is fully explained in the LaTeX Cookbook, Chapter 10, Advanced Mathematics, Drawing commutative diagrams.

Edit and compile if you like:
% Commutative diagram
% Author: Stefan Kottwitz
% https://www.packtpub.com/hardware-and-creative/latex-cookbook
\documentclass[border = 10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}[-stealth,
  label/.style = { font=\footnotesize }]
  \matrix (m)
    [
      matrix of math nodes,
      row sep    = 4em,
      column sep = 4em
    ]
    {
      A_0 & A_1 & A_2 & A_3 & A_4 \\
      B_0 & B_1 & B_2 & B_3 & B_4 \\
    };
  \foreach \i in {1,...,4} {
    \path
      let \n1 = { int(\i+1) } in
        (m-1-\i) edge node [above, label] {$f_\i$} (m-1-\n1)
        (m-2-\i) edge node [below, label] {$f^\prime_\i$} (m-2-\n1)
        (m-1-\i) edge node [left,  label] {$g_\i$} (m-2-\i);
  }
  \path (m-1-5) edge node [left, label] {$g_5$} (m-2-5);
\end{tikzpicture}
\end{document}
Click to download: five-lemma.texfive-lemma.pdf
Open in Overleaf: five-lemma.tex