1

I want to show a plot in a beamer frame. I tried to do it like this

\documentclass[]{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{frame}{Comparison to template fitting}
    \begin{tikzpicture}
        \begin{axis}[]
            \addplot table [] {
            0.22 0.56
            0.43 0.54
            0.65 0.53
            0.86 0.55
            1.08 0.48
            1.3  0.34
            1.52 0.17
            1.73 0.23
            1.95 0.16
            2.17 0.17
            2.38 0.23
            2.6  0.26
            };
        \end{axis}
    \end{tikzpicture}
\end{frame}
\end{document}

But this raises the error

! Package pgfplots Error: Could not read table file '" 0.22 0.56 0.43 0.54 0.65

If I change the document class to article and remove the frame, I don't have any problem

\documentclass[]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

%\begin{frame}{Comparison to template fitting}
    \begin{tikzpicture}
        \begin{axis}[]
            \addplot table [] {
            0.22 0.56
            0.43 0.54
            0.65 0.53
            0.86 0.55
            1.08 0.48
            1.3  0.34
            1.52 0.17
            1.73 0.23
            1.95 0.16
            2.17 0.17
            2.38 0.23
            2.6  0.26
            };
        \end{axis}
    \end{tikzpicture}
%\end{frame}
\end{document}

So the plot itself is fine.

How can I use my pgfplot in a beamer frame?

1 Answers1

1

The problem is that the axis is part of a macro when wrapped in a frame. One way of making this work is to make the frame fragile. See e.g. here for a discussion.

\documentclass[]{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{frame}[fragile]
\frametitle{Comparison to template fitting}
    \begin{tikzpicture}
        \begin{axis}[]
            \addplot table [] {
            0.22 0.56
            0.43 0.54
            0.65 0.53
            0.86 0.55
            1.08 0.48
            1.3  0.34
            1.52 0.17
            1.73 0.23
            1.95 0.16
            2.17 0.17
            2.38 0.23
            2.6  0.26
            };
        \end{axis}
    \end{tikzpicture}
\end{frame}
\end{document}

enter image description here

Alternatively you can add explicit row separators. The pgfplots manual v1.17 writes on p. 49 that

enter image description here

Putting the plot in the beamer frame is precisely one of those situations, so we can use \\ here.

\documentclass[]{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{frame}
\frametitle{Comparison to template fitting}
    \begin{tikzpicture}
        \begin{axis}[]
            \addplot table [row sep=\\] {
            0.22 0.56\\
            0.43 0.54\\
            0.65 0.53\\
            0.86 0.55\\
            1.08 0.48\\
            1.3  0.34\\
            1.52 0.17\\
            1.73 0.23\\
            1.95 0.16\\
            2.17 0.17\\
            2.38 0.23\\
            2.6  0.26\\
            };
        \end{axis}
    \end{tikzpicture}
\end{frame}
\end{document}

Off-topic: I recommend using the \frametitle command for the titles of the frames.

  • What is the benefit of \frametitle versus \begin{frame}{<title>} ? – BambOo May 06 '20 at 09:00
  • @BambOo Sometimes things get inadvertently interpreted as title, and there were even some questions here on unexplainable errors. Unfortunately, samcarter is not around, who could give a better explanation. –  May 06 '20 at 16:00
  • Ok, good to know. I never had such issues, but thanks for the warning. It is a shame indeed that samcarter has been suspended (still did not understand why by the way). I just checked and the suspension comes to an end in 2 days... let's hope it will come back to normal (unlikely but who knows) – BambOo May 06 '20 at 16:10
  • Not sure it is relevant to stir things up now. I believe there were some discussions in meta or in the chat, but I missed most of it (I was writing my phd thesis). In the same fashion marmot disappeared from here (winter has come probably). It is interesting to see so much people active on multiple sites. – BambOo May 06 '20 at 16:22
  • Do you mean the sun will shine on the marmots again ? – BambOo May 06 '20 at 16:42
  • @BambOo That for sure. –  May 06 '20 at 16:47