7

Currently I am trying to create a presentation with tikzcd using the TeXWorks Editor. Apparently, my computer does not recognize the command \arrow in the way I would need it to work with tikzcd (only a theory). Frankly speaking, it looks like my computer has problems with the tikzcd package at all.

This is working (well, if you want to call this working...)

\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}
\end{tikzcd}$

\end{frame}

\end{document} 

But as soon as I insert something marginal useful (e.g. A&B, while a single A is working...), I get an undefined control sequence referring to \end{frame}. I want to include something like this

enter image description here

I feel like I missing an important point, even though I copied the code given above straight from here. What could be wrong and more importantly: how do I fix this?

Thanks in advance!

0

2 Answers 2

6

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} 

enter image description here

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 intikz.code.tex. It is true, though, that ampersand replacement is another way to solve the problem. I also want to add that I was usingexecute 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 becausetikz-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.

3
  • Again, thank you! I'll wait before accepting your answer to see if someone else has some improvements to offer or even an alternative :)
    – mrtaurho
    Commented Sep 8, 2019 at 17:44
  • @mrtaurho What kind of improvement are you after? To me the diagram looks fine (also because you do not overload it), what I would probably do is to make it larger, e.g. \[\begin{tikzcd}[column sep=6em,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}\], which also uses an equation environment. (I use fragile all the time and there is IMHO no real downside to it.)
    – user194703
    Commented Sep 8, 2019 at 17:48
  • 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`\&=13 that can be found in tikz.code.tex. It is true, though, that ampersand replacement is another way to solve the problem.
    – user194703
    Commented Sep 8, 2019 at 21:09
5

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} 

enter image description here

2
  • Hi, but using xy package (xymatrix), is there the same problem?
    – Sebastiano
    Commented Sep 8, 2019 at 20:49
  • 1
    @Sebastiano No, as far as I know.
    – egreg
    Commented Sep 8, 2019 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .