# How to put a square inside a commutative diagram?

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

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}

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Apr 24, 2017 at 8:59

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}


The right way with tikz-cd is to use a "phantom" arrow as in

\arrow[dr, phantom, "\square"]


MWE (I've used some code to reproduce the picture and not yours):

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

\begin{document}

\begin{tikzcd}
X'' \arrow[r, "g'"] \arrow[d] & X' \arrow[r, "g"] \arrow[d] \arrow[dr, phantom, "\square"] & X \arrow[d] \\
S'' \arrow[r, "f'"']          & S' \arrow[r, "f"']                                         & S
\end{tikzcd}

\end{document}


Another solution could be to use execute at end picture.
It is less elegant than karlkoeller's answer, but it allows you to position the node wherever you like, just changing its coordinates.

\documentclass[a4paper,12pt]{article}
\usepackage{amssymb}
\usepackage{tikz-cd}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzcd}[
every matrix/.append style={name=mycd},
execute at end picture={
\node at ($(mycd-1-1)!0.5!(mycd-2-2)$) {$\square$};
}
]
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}