Tikzcd in beamer not working
Questions of this type have been asked here before, but I could not find any answer with an explanation (which is most likely my fault). Anyway, here we go. In section 8 Creating Frames of the beamer
manual version 3.56 it is stated that (I highlighted the important part)
The environment contents can be normal LATEX text, but may not contain \verb commands or verbatim environments or any environment that changes the character codes, unless the fragile option is given.
It also says on p. 61
If a frame contains fragile text, different internal mechanisms are used to typeset the frame to ensure that inside the frame the character codes can be reset. The price of switching to another internal mechanism is that either you cannot use overlays or an external file needs to be written and read back (which is not always desirable).
In any case, since tikz-cd
does change some character codes, we need to add fragile
. (As a rule of thumb, whenever something does not work in beamer
which works fine elsewhere, it is worthwhile adding the fragile
key and check whether or not this solves the problem. Some of the drawbacks of using fragile
are discussed in this thread.)
I also used the opportunity to typeset the diagram, in which the execute at end picture
key is used to connect the two edge labels, which attained names with the alias
key.
\documentclass{beamer}
\usetheme[width=2.18cm]{PaloAlto}
\usefonttheme{serif}
\usepackage{tikz-cd}
\begin{document}
\begin{frame}[fragile]
\frametitle{Not important}
\framesubtitle{Neither important}
$\begin{tikzcd}[execute at end picture={
\path (F) edge[double equal sign distance, -Implies,"$\omega$",
shorten >=2pt,shorten <=2pt] (G);}]
\mathcal{C} \arrow[r,bend left,"F" {alias=F}]
\arrow[r,bend right,"G" {swap,alias=G}]& \mathcal{D}
\\
\end{tikzcd}$
\end{frame}
\end{document}
ADDENDUM: It has been suggested that the ampersand is the problem. I disagree with that statement. E.g.
\begin{frame}
\frametitle{The ampersand per se is not the problem}
\begin{tabular}{ccc}
a & b & c\\
\end{tabular}
\end{frame}
does work. It is the line \catcode\
\&=13that can be found in
tikz.code.tex. It is true, though, that ampersand replacement is another way to solve the problem. I also want to add that I was using
execute at end picturefor a reason. Even though there are seemingly simpler ways, they do not allow you to control the order in which the arrows are placed because
tikz-cd` collects them internally and draws them in an order that we cannot control. Here it does not matter, but you can find here an example where it does.
This is a known problem: when a tikzcd
is inside the argument to another command, &
cannot be used; there's a handy workaround, though.
\documentclass{beamer}
\usetheme[width=2.18cm]{PaloAlto}
\usefonttheme{serif}
\usepackage{tikz, tikz-cd}
\usetikzlibrary{arrows,shapes,cd}
\begin{document}
\begin{frame}{Not important}
\framesubtitle{Neither important}
\begin{tikzcd}[ampersand replacement=\&,column sep=4em]
\mathcal{C} \arrow[r, bend left=30, "F"{name=U}]
\arrow[r, bend right=30, "G"'{name=D}]
\& \mathcal{D}
\arrow[Rightarrow, from=U, to=D,shorten=4pt,"\omega"]
\end{tikzcd}
\end{frame}
\end{document}