# Using tikzcd in newcommand (for inline arrows)

I’m using tikz-cd for diagrams, and have a medium-sized diagram that I want to re-use several times. However, when I try using a definition like:

\newcommand{\myarrow}{$\begin{tikzcd} A \ar{r} & B \end{tikzcd}$


I get an error:

ERROR: Package pgfbasematrix Error: Single ampersand used with wrong catcode.


Is it possible to put a tikz-cd diagram into a defined command somehow?

-

It's a known problem with TikZ matrices:

\documentclass{article}
\usepackage{tikz-cd}
\newcommand{\myarrow}{%
$\begin{tikzcd}[ampersand replacement=\&] A \ar{r} \& B \end{tikzcd}$}
\begin{document}
\myarrow
\end{document}


The problem is that tikzpicture wants to do some juggling with the meaning of &, but it's impossible if the & has been found as the replacement text of a macro, as in your original definition. Happily, it provides the

ampersand replacement=...


key that allows to define some command as substitute for &, avoiding the need to do the jugglings.

-