Tikzcd inside parbox

parboxtikz-cd

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

\parbox{\textwidth}{\[
    \begin{tikzcd}
        a \arrow{r}{} & b
    \end{tikzcd}
\]}

\end{document}

The MWE above throws a bunch of errors (Undefined control sequence and Package pgf: Single ampersand used with wrong catcode), and I don't understand why. All kinds of math environments work fine inside \parbox, and \tikz seems to work too. Is this "expected behavior", or a bug?

Best Answer

Due to nested tikz-cd inside \parbox you need replace ampersands with something else. For example as is done in MWE below:

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

\parbox{\textwidth}%
{
\[
    \begin{tikzcd}[ampersand replacement=\&]
a \rar  \& b
    \end{tikzcd}
 \]
}

\end{document}

enter image description here

For details see 20.5 Considerations Concerning Active Characters, page 327 in TikZ & PGF manual, version 3.1.9a.