Several babel modules (e.g. the ngerman one) make active the " character, and this is the source of troubles with the tikz-cd package and the tikz' babel library may be not enough (see e.g. this question).
As a workaround, I can insert \shorthandoff{"} and \shorthandon{"} respectively before and after the tikzcd environments. But, because this becomes tedious, I would want to automate this with \AtBeginEnvironment{tikzcd} and \AfterEndEnvironment{tikzcd}. But this hasn't any effect: in the following MCE, the compilation fails at the second tikzcd environment.
\documentclass[
, ngerman
, english
]{article}
\usepackage{tikz-cd}
\usepackage{babel}
\usetikzlibrary{babel}
\begin{document}
\shorthandoff{"}%
\fbox{%
\begin{tikzcd}[ampersand replacement=&]
X\ar[-stealth,r,swap,"p" ] &
Y \ar[-stealth,l,swap,bend right=30,"s"]
\end{tikzcd}
}
\shorthandon{"}%
\AtBeginEnvironment{tikzcd}{%
\shorthandoff{"}%
}
\AfterEndEnvironment{tikzcd}{%
\shorthandon{"}%
}
\fbox{%
\begin{tikzcd}[ampersand replacement=&]
X\ar[-stealth,r,swap,"p" ] &
Y \ar[-stealth,l,swap,bend right=30,"s"]
\end{tikzcd}
}
\end{document}

\shorthandoff{"}should be not before\begin{tikzcd}, but before (in the example)\fbox:$ – Denis Bitouzé Jun 16 '21 at 16:50BeforeBeginEnvironment, it woulld work? – Bernard Jun 16 '21 at 18:49BeforeBeginEnvironmentthat I had in mind (in symmetry with\AfterEndEnvironment) and wrongly mentioned\AtBeginEnvironment. But neither of them works. – Denis Bitouzé Jun 16 '21 at 19:29tikzcdrelated because simple\shorthandoff{"}does not work inside\fbox. You can check this by comparing\fbox{\shorthandoff{"}-""-}with\shorthandoff{"}\fbox{-""-}. – Kpym Jun 19 '21 at 16:56tikz-cdis partially the culprit as it heavily relies on", which is a pain since this character is active with manybabel's languages. And\documentclass{article}\usepackage[ngerman,english]{babel}\begin{document}\fbox{"Foo"}\end{document}compiles like a charm. – Denis Bitouzé Jun 20 '21 at 13:00\AtBeginEnvironmentbecause this will insert\shorthandoff{"}inside the\fboxand it will not work (like in my previous example). If you wan to usetikzcdinside\fboxprobably you should redefine\fboxin a way to add\shorthandoff{"}before it. – Kpym Jun 20 '21 at 15:51