[Tex/LaTex] “Package pgfplots Error: Could not read table file” in Beamer

beamererrorspgfplots

I am trying to plot a Tikz figure on a Beamer slide but receive the following error:

! Package pgfplots Error: Could not read table file '" x y 0.8 0.8 1. 0.8 1. 1. 0.8 1. 0.9 0.8 1. 0.9 0.9 1. 0.8 0.9 0.9 0.9 "'. In case you intended to provide inline data: maybe TeX screwed up your end-of-lines? Try 'row sep=crcr' and terminate your lines with '\\' (refer to the pgfplotstable manual for details).

Here is the Beamer presentation that generated this error:

\documentclass[]{beamer}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}
\begin{frame}
\begin{tikzpicture} \begin{axis}[view={40}{40},xmin=0,xmax=1,ymin=0,max=1,zmin=0,zmax=1]
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x   y
0.8 0.8
1.  0.8
1.  1.
0.8 1.
0.9 0.8
1.  0.9
0.9 1.
0.8 0.9
0.9 0.9
};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

I tried the suggested solution to no avail:

\documentclass[]{beamer}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}
\begin{frame}
\begin{tikzpicture} \begin{axis}[view={40}{40},xmin=0,xmax=1,ymin=0,max=1,zmin=0,zmax=1]
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic,row sep=crcr] table[z expr=x^2*y^2] {
x   y\\
0.8 0.8\\
1.  0.8\\
1.  1.\\
0.8 1.\\
0.9 0.8\\
1.  0.9\\
0.9 1.\\
0.8 0.9\\
0.9 0.9\\
};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

When I put the same tikzpicture into an article class document, the code compiles as expected. Does anybody know what is the cause of this error in Beamer?

Best Answer

Add the [fragile] option to the frame:

\begin{frame}[fragile]