How can I use tikz-cd and \mathsurround simultaneously?

The proper formatting I need to use includes the line \mathsurround=2pt, but this ruins the arrow placement in tikzcd environments. Can I temporarily turn \mathsurround off, either by recovering the default behavior or else manually placing arrows in my commutative diagrams?

Here is an example of how bad the arrow placement looks with \mathsurround:

The image above was generated by the following:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-cd}
\mathsurround=2pt

\begin{document}
$\begin{tikzcd} B_{\sigma} (\bar{n}_{\bullet}) \ar[r, "\mathrm{diag}"] \ar[d, "\mathbf{y}_{\mathbf{i}_{\sigma}}" left] & B (n_{\bullet}) \ar[d, "\mathbf{y}_{\mathbf{i}}"] \\ A_0^{\mathbf{i}_{\sigma}} (\bar{n}_{\bullet}) \ar[r, "\iota"] & A_0^{\mathbf{i}} (n_{\bullet}) \end{tikzcd}$
\end{document}


Set \mathsurround to zero at each tikz environment.

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

\setlength{\mathsurround}{30pt}
\AtBeginEnvironment{tikzcd}{\setlength{\mathsurround}{0pt}}

\begin{document}

Text $0=0$ text
$\begin{tikzcd} B_{\sigma} (\bar{n}_{\bullet}) \arrow[r, "\mathrm{diag}"] \arrow[d, "\mathbf{y}_{\mathbf{i}_{\sigma}}" left] & B (n_{\bullet}) \arrow[d, "\mathbf{y}_{\mathbf{i}}"] \\ A_0^{\mathbf{i}_{\sigma}} (\bar{n}_{\bullet}) \arrow[r, "\iota"] & A_0^{\mathbf{i}} (n_{\bullet}) \end{tikzcd}$
Text $0=0$ text

\end{document}


The value of \mathsurround has been set to a humongous value just to make it clear that the setting is resumed after the tikzcd environment.

• And just to be sure, this resets the length to 2pt at the end of the tikzcd environment? May 18 at 15:40
• @jackson Yes, I added an example with a very large value just to clearly see that the value is restored. May 18 at 15:50