55

I would like to use tkiz matrix but any compilation within a tikz picture ends with error:

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

I wonder if there is no bug, since even this test file with code taken from pgf doc is not compiled, with both lualatex and xelatex engines:

\documentclass[11pt]{beamer}           
\usepackage{polyglossia}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setdefaultlanguage{english}


\title{}
\author{}
\date{}

\begin{document}
\maketitle


\begin{frame}
   \frametitle{test}

\begin{tikzpicture}
\draw[help lines] (0,0) grid (4,2);
\node [matrix,fill=red!20,draw=blue,very thick] (my matrix) at (2,1)
{
\draw (0,0) circle (4mm); & \node[rotate=10] {Hello};\\
\draw (0.2,0) circle (2mm); & \fill[red](0,0) circle (3mm); \\
};
\draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{frame}

\end{document}

I am using texlive 2014, updated it today (but compilation error was there before I updated) under Arch Linux. Could someone check whether this problem is reproductible on texlive 2014 or tell me what's wrong if it is not a bug?

Escaping the ampersands does not really help, every nodes are written at the same place

Many thanks

EDIT: the trouble is connected with beamer. This document compiles without problem if I change documentclass to article (switching of course frame and frametitle). Where can this trouble come from?

sztruks
  • 3,074
  • 1
    when using beamer i need to include ampersand replacement=\& in \matrix options and \& instead of & inside the matrix. – Ignasi Oct 22 '14 at 11:27

2 Answers2

62

When matrix is used in a frame environment, you need ampersand replacement just as you would when the matrix is in the argument to a command:

\documentclass[11pt]{beamer}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}
\frametitle{test}

\begin{tikzpicture}[ampersand replacement=\&]
  \draw[help lines] (0,0) grid (4,2);
  \node [matrix,fill=red!20,draw=blue,very thick] (my matrix) at (2,1)
    {
     \draw (0,0) circle (4mm); \& \node[rotate=10] {Hello};\\
     \draw (0.2,0) circle (2mm); \& \fill[red](0,0) circle (3mm); \\
    };
  \draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{frame}

\end{document}

Alternatively, use the fragile option (slower, as it writes the frame code in a file and inputs it):

\documentclass[11pt]{beamer}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}[fragile]
\frametitle{test}

\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,2);
  \node [matrix,fill=red!20,draw=blue,very thick] (my matrix) at (2,1)
    {
     \draw (0,0) circle (4mm); & \node[rotate=10] {Hello};\\
     \draw (0.2,0) circle (2mm); & \fill[red](0,0) circle (3mm); \\
    };
  \draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 1
    @Paul Gaborit: Thank you very much for both clear answers. I had already tried to comment the ampersand, but since I wasn’t aware of this option, I had all my nodes stacked at the same place. Now it works fine. – sztruks Oct 24 '14 at 15:17
8

Here are two methods to avoid problems with active characters:

  1. add the [fragile] option to your frame environment (cf. section "12.9 Verbatim and Fragile Text", p.126, userguide of beamer),

  2. add the ampersand replacement=\& option to your matrix node and use \& as separator (cf. section "20.5 Considerations Concerning Active Characters", p.313, pgfmanual).

Code:

\documentclass[11pt]{beamer}           
\usepackage{polyglossia}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setdefaultlanguage{english}


\title{}
\author{}
\date{}

\begin{document}

  \maketitle

\begin{frame}[fragile]
   \frametitle{test}

\begin{tikzpicture}
\draw[help lines] (0,0) grid (4,2);
\node [matrix,fill=red!20,draw=blue,very thick] (my matrix) at (2,1)
{
\draw (0,0) circle (4mm); & \node[rotate=10] {Hello};\\
\draw (0.2,0) circle (2mm); & \fill[red](0,0) circle (3mm); \\
};
\draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{frame}

\begin{frame}
   \frametitle{test}

\begin{tikzpicture}
\draw[help lines] (0,0) grid (4,2);
\node [matrix,fill=red!20,draw=blue,very thick,ampersand replacement=\&] (my matrix) at (2,1)
{
\draw (0,0) circle (4mm); \& \node[rotate=10] {Hello};\\
\draw (0.2,0) circle (2mm); \& \fill[red](0,0) circle (3mm); \\
};
\draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{frame}

\end{document}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283