Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to plot the following data in one slide

%-----------------------
\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

\begin{frame}[t]
\frametitle{Numerical Results}
\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}

\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{frame}
%---------------------

\end{document}

but the error is

! Package pgfplots Error: Sorry, could not retrieve column 'n' from table 'Sp.d
at'. Please check spelling (or introduce name aliases)..

See the pgfplots package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.1718 \end{frame}
share|improve this question
1  
By creating a real minimal working example with the \documentclass and packages it works by removing the unnecessary \end{figure} since that environment was not opened before. –  Claudio Fiandrino Jul 24 '12 at 15:20
1  
The example works fine on my computer. Can you complete this snippet to a full compilable example? –  percusse Jul 24 '12 at 15:24
    
I use above inside –  Mohammad Jul 24 '12 at 15:38
    
I use above snippet inside \documentclass{beamer} \usepackage{tikz} \usepackage{pgfplots} \usepackage{filecontents} \begin{document} –  Mohammad Jul 24 '12 at 15:38
    
But the same error! for me it also works with for instance {article} instead of {beamer}. –  Mohammad Jul 24 '12 at 15:42
show 3 more comments

1 Answer

Add the fragile option to the frame:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

%-----------------------
\begin{frame}[t,fragile]
\frametitle{Numerical Results}
\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}
\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{frame}
%---------------------

\end{document}

enter image description here

or move the filecontents environment outside the frame environment; you can, for example, place it in the preamble:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{Sp.dat}
n   r     a        k 
1          100          100          100
2       95.721       98.645       98.738
4        82.31       93.497       93.295
8       56.667       63.901       63.838
16       46.536       52.572        53.31
32       45.399       53.421       53.318
\end{filecontents}

\begin{document}

%-----------------------
\begin{frame}[t]
\frametitle{Numerical Results}
\pgfplotstableread{Sp.dat}{\Sp}
\pgfplotsset{tick label style={font=\tiny\bfseries},
label style={font=\small},
legend style={font=\tiny}
}
\begin{tikzpicture}[scale=1.0]
\begin{axis}[ymajorgrids=true,
 xmin=0,xmax=32,ymin=40,ymax=105,
xtick={1,2,4,8,16,32},
ytick={45,50,60,70,80,90,100},
xlabel= Number of processors,
ylabel=Efficiency]
\addplot [black,very thick] table [x={n}, y={r}] {\Sp};
\addlegendentry{$20\rightarrow 15$}
\addplot [dashed,red,very thick] table [x={n}, y={n}] {\Sp};
\addlegendentry{$20\rightarrow 10$}
\addplot [dotted,blue,very thick] table [x={n}, y={k}] {\Sp};
\addlegendentry{$20\rightarrow 5$}
\end{axis}
\end{tikzpicture}
\end{frame}
%---------------------

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.