[Tex/LaTex] Tikzcd pullback square issue

tikz-cdtikz-styles

I have a tikzcd diagram

\begin{center}
\begin{tikzcd}
F \arrow[r] \arrow[d] 
& * \arrow[d] \\
X \arrow[r, "f"] & Y \\
\end{tikzcd}
\end{center}

which looks like this:

Pullback square

I wish to add a symbol which indicates that this is a pullback square. Having looked both here and here, I have found the solution to create my own tikz-cd symbol, and insert that into the diagram.

The code for my symbol is this:

\newcommand{\foo}[1]{%
\begin{tikzpicture}[#1]%
\draw (0,0) -- (1ex,0ex);%
\draw (1ex,0ex) -- (1ex,1ex);%
\end{tikzpicture}%
 }

This is basically the bottom and right hand edge of a square.

To add this symbol into the diagram, I am changing the code for the diagram to:

\begin{center}
\begin{tikzcd}
F \arrow[r] \arrow[d] 
\arrow[dr, phantom, "\foo{} " , very near start, color=black]
& * \arrow[d] \\
X \arrow[r, "f"] & Y \\
\end{tikzcd}
\end{center}

As far as I know, this should be creating an invisible arrow, and putting my symbol at its head. Indeed, something does appear, but its not my what I want:

enter image description here

What should appear is something like this (designed in MSpaint):

enter image description here

Which is the same thing except the symbol with the correct symbol.

What's going wrong? Should I be approaching this in a different way?

Best Answer

Your approach nests tikzpictures. The standard way to avoid that is to use \saveboxes.

\documentclass{article}
\usepackage{tikz-cd}
\newsavebox{\pullback}
\sbox\pullback{%
\begin{tikzpicture}%
\draw (0,0) -- (1ex,0ex);%
\draw (1ex,0ex) -- (1ex,1ex);%
\end{tikzpicture}}
\begin{document}
\begin{center}
\begin{tikzcd}
F \arrow[r] \arrow[d] 
\arrow[dr, phantom, "\usebox\pullback" , very near start, color=black]
& * \arrow[d] \\
X \arrow[r, "f"] & Y \\
\end{tikzcd}
\end{center}
\end{document}

enter image description here

You defined your command with a parameter, which you did not use. Let's assume that there is a need for such parameters. Then \saveboxes are unhandy. Therefore I add an alternative: a path picture with as many things fixed as you can think of. E.g. - is to say that there should not be an arrow, etc. (I understand that it is very unlikely that you want the symbol to be red, this is just to illustrate that parameters work here.)

\documentclass{article}
\usepackage{tikz-cd}
\tikzset{pullback/.style={minimum size=1.2ex,path picture={
\draw[opacity=1,black,-,#1] (-0.5ex,-0.5ex) -- (0.5ex,-0.5ex) -- (0.5ex,0.5ex);%
}}}
\begin{document}
\begin{center}
\begin{tikzcd}
F \arrow[r] \arrow[d] 
\arrow[dr, phantom," " {pullback=red}, very near start, color=black]
& * \arrow[d] \\
X \arrow[r, "f"] & Y \\
\end{tikzcd}
\end{center}
\end{document}

enter image description here

Related Question