# Tikz: error when using beamer-class but not when using article-class

I'm using Overleaf to build a beamer presentation and wanted to add rectangles around cells in table (as described in this other question/answer by barbara beeton). Unfortunately, I get error messages and disaligned rectangles when I try to implement it in beamer, inside the frame environment.

Here it is a minimal working example using the article class:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\begin{document}
\begin{tikzpicture}
\matrix (M) [%
matrix of nodes, column sep=1cm, row sep=1cm
]
{%
A1& B1 \\
A2& B2 \\
};
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\end{tikzpicture}
\end{document}


Here it is a minimal working example using the beamer class:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\begin{document}
% \begin{frame}{Frame Title}
\begin{tikzpicture}
\matrix (M) [%
matrix of nodes, column sep=1cm, row sep=1cm
]
{%
A1& B1 \\
A2& B2 \\
};
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\end{tikzpicture}
% \end{frame}
\end{document}


However, if I uncomment the frame environment the code does not compile properly anymore. The following is the problematic code:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\begin{document}
\begin{frame}{Frame Title}
\begin{tikzpicture}
\matrix (M) [%
matrix of nodes, column sep=1cm, row sep=1cm
]
{%
A1& B1 \\
A2& B2 \\
};
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\end{tikzpicture}
\end{frame}
\end{document}


Thanks a lot for helping!

You need to add option fragile to frame:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,fit, matrix, shapes,}

\begin{document}
\begin{frame}[fragile]% <---
\frametitle{Frame Title}
\begin{tikzpicture}
\matrix (M) [
matrix of nodes, column sep=1cm, row sep=1cm
]
{
A1& B1 \\
A2& B2 \\
};
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\end{tikzpicture}
\end{frame}
\end{document}


• Why do you think those options have anything to with it? With or without them, the error is the same. See e.g. the question I linked to above or tex.stackexchange.com/questions/208408/… – Torbjørn T. Oct 19 '19 at 18:21
• @TorbjørnT., because with fragile it works fine (no errors). Frankly said, it might be, that I confused something, i.e. that actually problem is with ampersands. – Zarko Oct 19 '19 at 18:40
• You didn't understand what I said. In your answer you say that fragile is required because of matrix of nodes, column sep=1cm, row sep=1cm. That is wrong. – Torbjørn T. Oct 19 '19 at 18:42
• @TorbjørnT., i delete this part of explanation, I need to check again some my older solution of similar problems. – Zarko Oct 19 '19 at 18:44