[Tex/LaTex] How to put a square inside a commutative diagram

commutative-diagramstikz-cd

How to put a square inside a commutative diagram as in the following picture:

enter image description here

It would like to have a square in the following diagram:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz-cd}
\begin{document}

\begin{tikzcd}
X\times_S Y \arrow{r}{p_2} \arrow{d}[swap]{p_1} &Y \arrow{d}{g} \\   
X \arrow[swap]{r}{f} & Z
\end{tikzcd}

\end{document}}

Best Answer

Two solutions: one with pstricks and a psmatrix environment, the other with tikz-cd, in which I use the short syntax for arrows:

\documentclass{article}
\usepackage{mathtools, amssymb}
 \usepackage{pst-node, auto-pst-pdf}
\usepackage{tikz-cd}

\begin{document}

\texttt{psmatrix solution: }
 \[ \psset{arrows=->, arrowinset=0.15, linewidth=0.6pt, nodesep=3pt, rowsep=0.6cm, colsep = 1cm}
 \everypsbox{\scriptstyle}
 \begin{psmatrix}
%%% nodes
  X'' & X' & X \\%
S'' & S' & S
 %%% horizontal arrows
\ncline{1,1}{1,2}\naput{g'} \ncline{1,2}{1,3}\naput{g}
 \ncline{2,1}{2,2}\nbput{f'} \ncline{2,2}{2,3}\nbput{f}
 %%% vertical arrows
\ncline{1,1}{2,1} \ncline{1,2}{2,2}\ncline{1,3}{2,3}
\ncline[linestyle =none, arrows=-]{1,2}{2,3}\ncput{\textstyle\square}
 \end{psmatrix}
 \]
\vskip 1cm

\texttt{tikz-cd solution: }
\[ \begin{tikzcd}[arrows={-Stealth}]
  X''\rar["g'"]\dar & X'\rar["g"]\dar\drar[phantom, "\square"] & X\dar \\%
S''\rar[swap, "f'"] & S'\rar[swap, "f"] & S
\end{tikzcd}
\]

\end{document} 

enter image description here

Related Question