# Tikzcd with Beamer: extremely simple code results in & ampersand catcode error

\documentclass{beamer}
\usepackage{amsmath}
\usepackage{tikz-cd}

\begin{document}
\begin{frame}[fragile, t]{My slides}
\only<1>{
\begin{equation*}
\begin{tikzcd}
a
\end{tikzcd}
\end{equation*}}
\only<2>{
\begin{equation*}
\begin{tikzcd}
a  \arrow{r} &  b
\end{tikzcd}
\end{equation*}}
\end{frame}
\end{document}


All I want is to show 'a' first, then a point to b, like this 'a->b' (and it has to use Tikz-cd!). But Tikzcd is making it impossible because I get the following error:

Package pgf Error: Single ampersand used with wrong catcode.

See the pgf package documentation for explanation. Type H for immediate help. ...
l.13 \end{equation*}} This error message was generated by an \errmessage command, so I can't give any explicit help. Pretend that you're Hercule Poirot: Examine all clues, and deduce the truth by order and method.

Is there any way to solve this issue? The & works fine when it is on its own frame. But when I put it into a \only block things go bonkers.

I thought [fragile] was supposed to prevent these things!

Normal ampersand (&) does not work well with beamer, so you have to change it. I change to \&.

\documentclass{beamer}
\usepackage{amsmath}
\usepackage{tikz-cd}

\begin{document}
\begin{frame}[fragile, t]{My slides}
\only<1>{
\begin{equation*}
\begin{tikzcd}
a
\end{tikzcd}
\end{equation*}}
\only<2>{
\begin{equation*}
\begin{tikzcd}[ampersand replacement=\&]
a  \arrow{r} \&  b
\end{tikzcd}
\end{equation*}}
\end{frame}
\end{document}

• Great. Thanks. I didn't try that solution because I thought this was for the case when you actually want to write an ampersand. I thought Tikzcd is using that ampersand as an anchor to the next node – The man of your dream May 20 '19 at 8:03
• @Themanofyourdream TikZ-cd is, literally, a TikZ matrix, nothing more. – user156344 May 20 '19 at 8:03
• I'm guessing that the & issue here comes from the fact that the tikzcd is being used inside a macro and that closses the catcodes prematurely. – daleif May 20 '19 at 10:01
• @daleif I have very little knowledge with beamer, so I can't explain it fully. But after some issues I had to deal with, and looking at some questions here, I found that & and beamer are not friends. – user156344 May 20 '19 at 10:43
• @JouleV it has nothing to do with beamer, try wrapping an tikzcd env in the following command: \newcommand\test[1]{#1} you get the same issue, and the same solution. – daleif May 20 '19 at 11:37