I've been following the solution here to get tikzcd and the externalisation library to play nicely with each other.
One step of this redefines &. However, at the moment, it does this for the whole document, while I'd like it to do it only for the mytikzcd environment. I've attempted to move the code into the mytikzcd definition and, depending on where exactly I put it, I seem to get a variety of different errors. Do you have any suggestions?
MWE lifted almost straight from the previous answer:
\documentclass{article}
%\url{https://tex.stackexchange.com/q/171931/86}
\usepackage{tikz}
\usepackage{environ}
%we don't actually need externalisation to demonstrate the problem, so turn it off for now
\usetikzlibrary{cd}%,external}
%\tikzexternalize
\usepackage{tabularray}
\makeatletter
\def\tikzcd@[#1]{%
\begin{tikzpicture}[/tikz/commutative diagrams/.cd,every diagram,#1]%
\ifx\arrow\tikzcd@arrow%
\pgfutil@packageerror{tikz-cd}{Diagrams cannot be nested}{}
\fi%
\let\arrow\tikzcd@arrow%
\let\ar\tikzcd@arrow%
\def\rar{\tikzcd@xar{r}}%
\def\lar{\tikzcd@xar{l}}%
\def\dar{\tikzcd@xar{d}}%
\def\uar{\tikzcd@xar{u}}%
\def\urar{\tikzcd@xar{ur}}%
\def\ular{\tikzcd@xar{ul}}%
\def\drar{\tikzcd@xar{dr}}%
\def\dlar{\tikzcd@xar{dl}}%
\global\let\tikzcd@savedpaths\pgfutil@empty%
\matrix[/tikz/matrix of \iftikzcd@mathmode math \fi nodes,
/tikz/every cell/.append code={\tikzcdset{every cell}},
/tikz/commutative diagrams/.cd,every matrix]%
\bgroup}
\def\endtikzcd{%
\pgfmatrixendrow\egroup%
\pgfextra{\global\let\tikzcdmatrixname\tikzlastnode};%
\tikzcdset{\the\pgfmatrixcurrentrow-row diagram/.try}%
\begingroup%
\pgfkeys{% quotes' library support /handlers/first char syntax/the character "/.initial=\tikzcd@forward@quotes,% /tikz/edge quotes mean={% edge node={node [execute at begin node=\iftikzcd@mathmode$\fi,%$ execute at end node=\iftikzcd@mathmode$\fi,%$ /tikz/commutative diagrams/.cd,every label,##2]{##1}}}}% \let\tikzcd@errmessage\errmessage% improve error messages \def\errmessage##1{\tikzcd@errmessage{##1^^J...^^Jl.\tikzcd@lineno\space% I think the culprit is a tikzcd arrow in cell \tikzcd@currentrow-\tikzcd@currentcolumn}}% \tikzcd@before@paths@hook% \tikzcd@savedpaths% \endgroup% \end{tikzpicture}% \ifnum0={}\fi}
\NewEnviron{mytikzcd}[1][]{%
\def@temp{\tikzcd@[#1]\BODY}%
\expandafter@temp\endtikzcd
}
\makeatother
\def\temp{&} \catcode`&=\active \let&=\temp
\begin{document}
\begin{mytikzcd}
A \arrow{rd} \arrow{r}{\phi} & B \ & C
\end{mytikzcd}
\begin{mytikzcd}
A \arrow{rd} \arrow{r}{\phi} & B \ & C
\end{mytikzcd}
%re-add the material below to see the problem that results from global redefinition of the &
% \begin{center}
% \begin{tblr}{
% rows = {35pt}, columns = {35pt},
% rowsep = 1pt, colsep = 1pt, stretch = 0,
% cells = {c,font=\Huge},
% cell{odd}{even} = {azure7},
% cell{even}{odd} = {azure7},
% hlines, vlines,
% }
% a & b & c & d & e & f & g & h \
% a & b & c & d & e & f & g & h \
% & & & & & & & \
% & & & & & & & \
% & & & & & & & \
% & & & & & & & \
% a & b & c & d & e & f & g & h \
% \end{tblr}
% \end{center}
\end{document}
Context: this needs to operate within a package; I don't want to tell the users that they have to write extra material before and after each usage of the mytikzcd environment. It needs to be invisible to them.
