11

I did a commutative diagram with tikz in the article class and everything works. The next code is the MWE in article class.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{center}
\begin{tikzpicture}
  \matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
  { A & B \\
    C & D \\};
  \path[-stealth]
    (m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
    (m-1-2) edge node [right] {g} (m-2-2)
    (m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{document}

But, when I copy the code in a Beamer document, something is wrong, as the following code.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}
  \matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
  { A & B \\
    C & D \\};
  \path[-stealth]
    (m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
    (m-1-2) edge node [right] {g} (m-2-2)
    (m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

Any idea? Am I doing something wrong?

15

With your code you need either to use the fragile option for the frame or to use the ampersand replacement key:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}
\begin{center}
\begin{tikzpicture}[ampersand replacement=\&]
  \matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
  { A \& B \\
    C \& D \\};
  \path[-stealth]
    (m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
    (m-1-2) edge node [right] {g} (m-2-2)
    (m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[fragile]
\begin{center}
\begin{tikzpicture}
  \matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
  { A & B \\
    C & D \\};
  \path[-stealth]
    (m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
    (m-1-2) edge node [right] {g} (m-2-2)
    (m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}

\end{document}

enter image description here

Another option is to use use tikz-cd package to facilitate writing commutative diagrams; in this case, you need to use the fragile option for frame:

\documentclass{beamer}

\usepackage{tikz-cd}
\usetikzlibrary{matrix}

\begin{document}
\begin{frame}[fragile]
\begin{center}
\begin{tikzcd}
A\arrow{r}{e}\arrow{d}[swap]{f} & B\arrow{d}{g} \\
C\arrow{r}[swap]{h} & D \\
\end{tikzcd}
\end{center}
\end{frame}

\end{document}

enter image description here

| improve this answer | |
  • I do that (add tikz-cd) but still does not work. Actually it does not work if I want to make second column using the & symbol. Any ideas? – Marion Apr 14 '17 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.