Adding boxed texts on commutative diagram

commutative-diagramstikz-cd

I'm trying to draw this commutative diagram (source: Elements of Algebraic Topology by James Munkres). I know how to draw the diagram using tikzcd without the boxes. Can someone help me about how to add these boxes on the tikzcd diagram?

Best Answer

We can place the boxes in the middle as labels “near end”, but a bit lowered and with smashed depth. For the upper boxes, I use \overset, but making the boxes zero width for safety.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

\newcommand{\sboxed}[1]{%
  \,\raisebox{-1ex}[\height][0pt]{\fbox{$\scriptstyle\mathstrut#1$}}%
}
\newcommand{\uboxed}[1]{%
  \makebox[0pt]{\fbox{$\scriptstyle\mathstrut#1$}}%
}

\begin{document}

\begin{tikzcd}[row sep=huge,column sep=large]
0 \arrow[r] &
C_{p+1} \arrow[r,"\phi"] \arrow[d,"\partial_C"',"\sboxed{c_p}" near end] &
\overset{\uboxed{d_{p+1}}}{D_{p+1}}
  \arrow[r,"\psi"] \arrow[d,"\partial_D"',"\sboxed{d_p}" near end] &
\overset{\uboxed{e_{p+1}}}{E_{p+1}}
  \arrow[r] \arrow[d,"\partial_E"',"\sboxed{e_p}" near end] &
0 \\
0 \arrow[r] &
C_{p} \arrow[r,"\phi"] \arrow[d,"\partial_C"',"\sboxed{c_{p-1}}" near end] &
D_{p} \arrow[r,"\psi"] \arrow[d,"\partial_D"',] &
E_{p} \arrow[r] \arrow[d,"\partial_E"'] &
0 \\
0 \arrow[r] &
C_{p-1} \arrow[r,"\phi"] &
D_{p-1} \arrow[r,"\psi"] &
E_{p-1} \arrow[r] &
0
\end{tikzcd}

\end{document}

enter image description here

Related Question