# Problem with beamer and tikz

For some reason this code doesn't work and I don't know why:

\documentclass[pdf]{beamer}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{upgreek}
\usepackage{textcomp}
\usepackage{extarrow}
\usepackage{fontenc,mathtext}
\usepackage{todonotes}
\usepackage{mathrsfs}
\usetikzlibrary{matrix,arrows}
\usetikzlibrary{chains}
\tikzset{join/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}
\tikzset{>=stealth',every on chain/.append style={join},every join/.style={->}}


[...]

\hypersetup{pdfpagemode=FullScreen}
\usecolortheme{progressbar}
\usefonttheme{progressbar}
\useinnertheme{progressbar}
\useoutertheme{progressbar}
\usepackage{pgf}
\usepackage{fancybox}
\usetikzlibrary{mindmap,trees,arrows}


[...]

\begin{document}
\frame{
$\begin{tikzpicture}[>=angle 90] \matrix(a)[matrix of math nodes, row sep=2.5em, column sep=2.5em, text height=1.5ex, text depth=0.20ex] {\bigwedge^2 S^m&S^m&S&0\\ F_2&F_1&S&0\\}; \path[->](a-1-1) edge (a-1-2); \path[->](a-1-2) edge (a-1-3); \path[->](a-1-3) edge (a-1-4); \path[->](a-2-1) edge (a-2-2); \path[->](a-2-2) edge (a-2-3); \path[->](a-2-3) edge (a-2-4); \path[->](a-1-1) edge node[auto] {\gamma}(a-2-1); \path[->](a-1-2) edge node[auto] {\cong}(a-2-2); \path[->](a-1-3) edge node[auto] {id}(a-2-3); \path[->](a-2-3) edge (a-1-3); \end{tikzpicture}$
}


Any idea of why doesn't it work?

-
What error do you get or what is not how it should be? –  Martin Scharrer Jul 8 '11 at 17:20
Please always give a minimal working example (MWE) that shows the error, i.e. remove any unrelated material also from the preamble (see the link for more info). In this case you only need \documentclass{beamer} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} ...  to recreate the error. Half the time you find the issue yourself during this process. –  Martin Scharrer Jul 8 '11 at 17:36

I have not tried specifically on your example, so I cannot be sure if it works or not. However I remember having a similar problem when using tikz and matrix. What I did was adding an option to the matrix :

ampersand replacement=\&


and then replace every piece of my code as such:

  \matrix[row sep=0.01cm,column sep=0.5cm,ampersand replacement=\&] {
\&
\node (A) [tree] {$\mathbf{A}$}; \&
\&
\\
\node (v0) [state] {$v_{src};v'_{src})$}; \&
\&
\node (vn)   [state] {$(v_{snk};v'_{snk})$};     \&
\\
\&
\node (B)   [tree] {$\mathbf{B}$};       \&
\&
\\
}


I hope this helped. Sorry I did not have much time to really check your issue specifically.

-
+1 This should help because it avoids the issue with the catcode change of & (by avoiding the use of &). –  Martin Scharrer Jul 8 '11 at 17:33

The \matrix command of TikZ changes the catcode of & and is therefore not allowed in an argument of another macro like \frame or an pseudo-environment like {frame}. This can be fixed by using \begin{frame}[fragile] .. \end{frame} instead. It makes sure that all catcode changes (for verbatim mode etc.) work correctly.

Works:

\begin{frame}[fragile]
$\begin{tikzpicture}[>=angle 90] \matrix(a)[matrix of math nodes, row sep=2.5em, column sep=2.5em, text height=1.5ex, text depth=0.20ex] {\bigwedge^2 S^m&S^m&S&0\\ F_2&F_1&S&0\\}; \path[->](a-1-1) edge (a-1-2); \path[->](a-1-2) edge (a-1-3); \path[->](a-1-3) edge (a-1-4); \path[->](a-2-1) edge (a-2-2); \path[->](a-2-2) edge (a-2-3); \path[->](a-2-3) edge (a-2-4); \path[->](a-1-1) edge node[auto] {\gamma}(a-2-1); \path[->](a-1-2) edge node[auto] {\cong}(a-2-2); \path[->](a-1-3) edge node[auto] {id}(a-2-3); \path[->](a-2-3) edge (a-1-3); \end{tikzpicture}$
\end{frame}

-