33

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?

migrated from stackoverflow.com Oct 22 '14 at 11:20

This question came from our site for professional and enthusiast programmers.

  • 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
40

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

  • 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
6

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}
  • I don't know how to thank you all for pointing out the problem of ampersand placement in pgf... Thanks a lot... – Sandipan Karmakar Aug 31 '18 at 12:10

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.