It is known that tikzexternalize and tikz-cd don't play well with each other: Are the TikZ libraries cd and external incompatible with one another?. Now, I have a tikzcd whose entries are tikzpictures. Can I somehow externalize the pictures in the tikz-cd?
\documentclass{scrartcl}
\usepackage[svgnames]{xcolor}
\usepackage{tikz,tikz-cd,xstring}
\newcommand{\FilteredBall}[1][abcfghxy]{
\begin{tikzpicture}[every node/.append style={inner sep=1pt}, baseline={(0,-0.2)}]%(current bounding box.center)]
%\fill[white] (0,0) circle (2);
\begin{scope}[gray]
\draw[dashed] (2,0) arc (0:180:2 and 0.6);
\draw (0,0) circle (2) ;
\draw (-2,0) arc (180:360:2 and 0.6);
\end{scope}
\begin{scope}[opacity=.5]
\IfSubStr{#1}{f}{
\begin{scope}
\clip (0,2) arc (90:-90:2) arc (-90:90:.6 and 2);
\shade[ball color=red!70] (0,0) circle[radius=2];
\end{scope}
\begin{scope}
\clip (0,2) arc (90:-90:2) arc (-90:90:1.2 and 2);
\shade[ball color=red!70] (0,0) circle[radius=2];
\end{scope}
}{}
\IfSubStr{#1}{g}{
\begin{scope}
\clip (0,2) arc (90:-90:-2 and 2) arc (-90:90:.6 and 2);
\shade[ball color=blue!60] (0,0) circle[radius=2];
\end{scope}
\begin{scope}
\clip (0,2) arc (90:-90:-2 and 2) arc (-90:90:-1.3 and 2);
\shade[ball color=blue!60] (0,0) circle[radius=2];
\end{scope}
}{}
\IfSubStr{#1}{h}{
\begin{scope}
\clip (0,2) arc (90:-90:-1.2 and 2) arc (-90:90:1.2 and 2);
\shade[ball color=green!70] (0,0) circle[radius=2];
\end{scope}
}{}
\end{scope}
\begin{scope}[thick]
\IfSubStr{#1}{a}{\draw[red] (0,2) arc (90:-90:-1.3 and 2) node[below left, pos=.9] {$a$};}{}
\IfSubStr{#1}{b}{\draw[blue] (0,2) arc (90:-90:1.2 and 2) node[below right, pos=.9] {$b$};}{}
\IfSubStr{#1}{c}{
\IfSubStr{#1}{h}{
\draw[Aqua, densely dotted] (0,2) arc (90:270:-0.6 and 2) node[left=-2pt, midway] {$c$};
}{
\draw[Aqua] (0,2) arc (90:270:-0.6 and 2) node[left=-2pt, midway] {$c$};
}
}{}
\end{scope}
\IfSubStr{#1}{x}{\fill (0,2) circle (0.15);}{}
\IfSubStr{#1}{y}{\fill (0,-2) circle (0.15);}{}
\path
(0,2) arc (90:-90:2) node[text=red, pos=.4, above right] {\IfSubStr{#1}{f}{$f$}{\phantom{{$f$}}}}
(0,2) arc (90:-90:-2 and 2) node[text=blue!50!black, pos=.6,, below left] {\IfSubStr{#1}{g}{$g$}{\phantom{{$g$}}}}
(-.4, .65) node[text=green!50!black] {\IfSubStr{#1}{h}{$h$}{\phantom{{$h$}}}}
(0,2) node[above] {\IfSubStr{#1}{x}{$x$}{\phantom{{$x$}}}}
(0,-2) node[below] {\IfSubStr{#1}{y}{$y$}{\phantom{{$y$}}}};
\end{tikzpicture}
}
\begin{document}
\begin{equation}
\tikzset{every node/.append style={outer sep=2pt}, x=2mm, y=2mm}
\begin{tikzcd}[row sep=tiny, column sep=tiny]
\FilteredBall[xya] \rar["\subseteq", phantom] &
\FilteredBall[xyabh] \rar["\subseteq", phantom] &
\FilteredBall[xyabchg] \rar["\subseteq", phantom] &
\FilteredBall[xyabcfgh]
\\
\FilteredBall[xya] \rar["\subseteq", phantom] &
\FilteredBall[xyab] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyabcg] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyabcfg] \uar["\subseteq", phantom, sloped] &
\\
\FilteredBall[xy] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyb] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xybc] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xybcf] \uar["\subseteq", phantom, sloped] \\
\FilteredBall[xy] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xy] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped]&
\FilteredBall[xyc] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyc] \uar["\subseteq", phantom, sloped].
\end{tikzcd}
\end{equation}
\end{document}
Edit The following code, based on Qrrbrbirlbel's answer, tries to mix pictures with math. The red horizontal arrow is obviously wrong, and should be the same as the black one:
\documentclass{scrartcl}
\usepackage[svgnames]{xcolor}
\usepackage{tikz,tikz-cd,xstring}
\usetikzlibrary{fit, external}
%\tikzexternalize
\makeatletter
\pgfkeys{% https://tex.stackexchange.com/a/665688/16595
/utils/temp/.initial/.expand once=\tikzcd,
/utils/temp/.prefix=\ifx\path\tikz@command@path % we're inside a tikzpicture
\let\tikzpicture\scope
\let\endtikzpicture\endscope
\else % we're not inside a tikzpicture, disable externalizing
\tikzexternaldisable
\fi,/utils/temp/.get=\tikzcd}
\makeatother
\tikzset{
cd vcenter/.style={baseline={([yshift=-axis_height]\tikzcdmatrixname)}},
matrix bb node/.style={
fit=(current bounding box), inner sep=0pt,%inner sep=+.3333em,
outer sep=+0pt, shape=rectangle, draw,%path only,
name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn},
add bb node/.style={execute at end cell={\node[draw,matrix bb node]{};}}}
\tikzcdset{
no matrix of nodes/.style={
/tikz/add bb node,
/tikz/matrix of nodes/.code=,
/tikz/matrix of math nodes/.code=}}
\newcommand{\FilteredBall}[1][abcfghxy]{
% \tikzset{}
%\fill[white] (0,0) circle (2);
\begin{scope}[every node/.append style={inner sep=2pt}]
\begin{scope}[gray]
\draw[dashed] (2,0) arc (0:180:2 and 0.6);
\draw (0,0) circle (2) ;
\draw (-2,0) arc (180:360:2 and 0.6);
\end{scope}
\begin{scope}[opacity=.5]
\IfSubStr{#1}{f}{
\begin{scope}
\clip (0,2) arc (90:-90:2) arc (-90:90:.6 and 2);
\shade[ball color=red!70] (0,0) circle[radius=2];
\end{scope}
\begin{scope}
\clip (0,2) arc (90:-90:2) arc (-90:90:1.2 and 2);
\shade[ball color=red!70] (0,0) circle[radius=2];
\end{scope}
}{}
\IfSubStr{#1}{g}{
\begin{scope}
\clip (0,2) arc (90:-90:-2 and 2) arc (-90:90:.6 and 2);
\shade[ball color=blue!60] (0,0) circle[radius=2];
\end{scope}
\begin{scope}
\clip (0,2) arc (90:-90:-2 and 2) arc (-90:90:-1.3 and 2);
\shade[ball color=blue!60] (0,0) circle[radius=2];
\end{scope}
}{}
\IfSubStr{#1}{h}{
\begin{scope}
\clip (0,2) arc (90:-90:-1.2 and 2) arc (-90:90:1.2 and 2);
\shade[ball color=green!70] (0,0) circle[radius=2];
\end{scope}
}{}
\end{scope}
\begin{scope}[thick]
\IfSubStr{#1}{a}{\draw[red] (0,2) arc (90:-90:-1.3 and 2) node[below left, pos=.9] {$a$};}{}
\IfSubStr{#1}{b}{\draw[blue] (0,2) arc (90:-90:1.2 and 2) node[below right, pos=.9] {$b$};}{}
\IfSubStr{#1}{c}{
\IfSubStr{#1}{h}{
\draw[Aqua, densely dotted] (0,2) arc (90:270:-0.6 and 2) node[left=-2pt, midway] {$c$};
}{
\draw[Aqua] (0,2) arc (90:270:-0.6 and 2) node[left=-2pt, midway] {$c$};
}
}{}
\end{scope}
\IfSubStr{#1}{x}{\fill (0,2) circle (0.15);}{}
\IfSubStr{#1}{y}{\fill (0,-2) circle (0.15);}{}
\path
(0,2) arc (90:-90:2) node[text=red, pos=.4, above right] {\IfSubStr{#1}{f}{$f$}{\phantom{{$f$}}}}
(0,2) arc (90:-90:-2 and 2) node[text=blue!50!black, pos=.6,, below left] {\IfSubStr{#1}{g}{$g$}{\phantom{{$g$}}}}
(-.4, .65) node[text=green!50!black] {\IfSubStr{#1}{h}{$h$}{\phantom{{$h$}}}}
(0,2) node[above] {\IfSubStr{#1}{x}{$x$}{\phantom{{$x$}}}}
(0,-2) node[below] {\IfSubStr{#1}{y}{$y$}{\phantom{{$y$}}}};
\end{scope}
}
\begin{document}
\begin{equation}
\begin{tikzpicture}[x=2mm, y=2mm, cd vcenter]
\begin{tikzcd}[row sep=tiny, column sep=tiny, no matrix of nodes]
\FilteredBall[xya] \rar["\subseteq", phantom] &
\FilteredBall[xyabh] \rar["\subseteq", phantom] &
\FilteredBall[xyabchg] \rar["\subseteq", phantom] &
\FilteredBall[xyabcfgh]
\\
\FilteredBall[xya] \rar["\subseteq", phantom] &
\FilteredBall[xyab] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyabcg] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyabcfg] \uar["\subseteq", phantom, sloped] &
\\
\FilteredBall[xy] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyb] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xybc] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xybcf] \uar["\subseteq", phantom, sloped] \\
\node[name=A]{$0_{0_{0_0}}$}; \rar[red] \uar &
\node[name=B]{$0^{0^{0^0}}$}; \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped]&
% \FilteredBall[xy] \draw (current bounding box.base) node (B){} circle(2pt); \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped]&
\FilteredBall[xyc] \rar["\subseteq", phantom] \uar["\subseteq", phantom, sloped] &
\FilteredBall[xyc] \uar["\subseteq", phantom, sloped].
\ar[from=A, to=B]
\end{tikzcd}
\end{tikzpicture}
\end{equation}
\end{document}

\FilteredSphere. So, why the downvote? – Bubaya Mar 20 '23 at 09:15\paths in it. TikZ-CD usesmatrix of (math) nodeswhich still can detect when a cell doesn't start with normal node text, though it can be finnicky. Since you only put normal diagrams in your cells, you can just not usematrix of (math) nodes. – Qrrbrbirlbel Mar 20 '23 at 10:35