# Keep getting missing $error in beamer using code that worked fine elsewhere I am getting errors like, missing$ inserted or extra or forgotten } while trying to compile the following code using beamer class.

\documentclass{beamer}
\newcommand*{\theorembreak}{\usebeamertemplate{theorem end}\framebreak\usebeamertemplate{theorem begin}}
\usepackage{mathtools}
\allowdisplaybreaks
\usepackage{tikz-cd}

\usetheme{Warsaw}
\begin{frame}
$$\begin{tikzcd}[font=\small] 0 \ar{r} & \frac{E'(K)}{\phi(E(K))} \ar{r}{\delta} \ar{d} & H^{1}(G_K, E[\phi]) \ar{r} \ar{d}{Res_v} & WC(E/K)[\phi] \ar{r} \ar{d} & 0 \\ 0 \ar{r} & \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\delta_{v}} \ar{d} & H^{1}(G_{v}, E[\phi]) \ar{r} \ar{d} & WC(E/K_{v})[\phi] \ar{r} \ar{d} & 0 \\ 0 \ar{r} & \prod\limits_{\mathclap{v \in M_K}} \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\prod\limits_{\mathclap{v \in M_K}}\delta_{v}} & \prod\limits_{\mathclap{v \in M_K}} H^{1}(G_{v}, E[\phi]) \ar{r} & \prod\limits_{\mathclap{v \in M_K}} WC(E/K_{v})[\phi] \ar{r} & 0 \end{tikzcd}$$
\end{frame}
\end{document}


Specifically, I get the following message:

! Undefined control sequence.
<argument> \pgf@matrix@last@nextcell@options

l.23 \end{frame}


If I type r and hit return, LaTeX reports lots of errors, spits out "That makes 100 errors; please try again." and crashes.

I have used the same code in another LaTeX document where it worked fine. But so I think the error could be some missing package. I don't know..

Can someone help me figure this out?

Thank you.

– Mico
Apr 26 '20 at 3:05

The error goes away if one adds fragile to the frame options. But then one still has to adjust some widths to make it fit on a frame. One possible way is to set the column sep to 1em but increase the distance between the second and third column to 2em by adding [2em] in the third column. (I also added the t option because I like this better and dropped the equation number to save some space and since in presentations it is in general not too useful to have them, but like everything here these are just suggestions.)

\documentclass{beamer}
\newcommand*{\theorembreak}{\usebeamertemplate{theorem end}\framebreak\usebeamertemplate{theorem begin}}
\usepackage{mathtools}
\allowdisplaybreaks
\usepackage{tikz-cd}

\usetheme{Warsaw}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{A commutative diagram}
$\begin{tikzcd}[font=\small,column sep=1em] 0 \ar{r} &\frac{E'(K)}{\phi(E(K))} \ar{r}{\delta} \ar{d} &[2em] H^{1}(G_K, E[\phi]) \ar{r} \ar{d}{Res_v} & WC(E/K)[\phi] \ar{r} \ar{d} & 0 \\ 0 \ar{r} & \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\delta_{v}} \ar{d} & H^{1}(G_{v}, E[\phi]) \ar{r} \ar{d} & WC(E/K_{v})[\phi] \ar{r} \ar{d} & 0 \\ 0 \ar{r} & \prod\limits_{\mathclap{v \in M_K}} \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\prod\limits_{\mathclap{v \in M_K}}\delta_{v}} & \prod\limits_{\mathclap{v \in M_K}} H^{1}(G_{v}, E[\phi]) \ar{r} & \prod\limits_{\mathclap{v \in M_K}} WC(E/K_{v})[\phi] \ar{r} & 0 \end{tikzcd}$
\end{frame}
\end{document}


Instead of adding fragile you can add an ampersand replacement.

\documentclass{beamer}
\newcommand*{\theorembreak}{\usebeamertemplate{theorem end}\framebreak\usebeamertemplate{theorem begin}}
\usepackage{mathtools}
\allowdisplaybreaks
\usepackage{tikz-cd}

\usetheme{Warsaw}
\begin{document}
\begin{frame}[t]
\frametitle{A commutative diagram}
$\begin{tikzcd}[font=\small,column sep=1em,ampersand replacement=\&] 0 \ar{r} \&\frac{E'(K)}{\phi(E(K))} \ar{r}{\delta} \ar{d} \&[2em] H^{1}(G_K, E[\phi]) \ar{r} \ar{d}{Res_v} \& WC(E/K)[\phi] \ar{r} \ar{d} \& 0 \\ 0 \ar{r} \& \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\delta_{v}} \ar{d} \& H^{1}(G_{v}, E[\phi]) \ar{r} \ar{d} \& WC(E/K_{v})[\phi] \ar{r} \ar{d} \& 0 \\ 0 \ar{r} \& \prod\limits_{\mathclap{v \in M_K}} \frac{E'(K_v)}{\phi(E(K_v))} \ar{r}{\prod\limits_{\mathclap{v \in M_K}}\delta_{v}} \& \prod\limits_{\mathclap{v \in M_K}} H^{1}(G_{v}, E[\phi]) \ar{r} \& \prod\limits_{\mathclap{v \in M_K}} WC(E/K_{v})[\phi] \ar{r} \& 0 \end{tikzcd}$
\end{frame}
\end{document}


Both the fragile and ampersand replacement options have been proposed in many posts, such as the answers to this question.