Beamer 让 \pgfplotsinvokeforeach 变得疯狂

Beamer 让 \pgfplotsinvokeforeach 变得疯狂

我正在尝试使用 \pgfplotsinvokeforeach 解析散点图的表格,但在 beamer 中失败,并提示“\iterate 定义中的参数编号非法”。我看到有人建议使用[fragile] 这里,但这对我的情况没有任何改变。我也尝试过使用 ##1 代替 #1,但这也无济于事。您能给我指出一个解决方案吗?

\documentclass[intlimits,mathserif,t,xcolor={dvipsnames,table}]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T3,T1]{fontenc}
\usepackage[british]{babel}

\usepackage{tikz,pgf,pgfplots}

%---TikZ and PGF Libraries---
%\usepgfplotslibrary{fillbetween}
\usetikzlibrary{external}
\usetikzlibrary{intersections}
%\usetikzlibrary{patterns}
%\usetikzlibrary{calc}
%\usetikzlibrary{arrows}
%\usetikzlibrary{positioning}
\pgfplotsset{compat=newest}
\tikzexternalize

\begin{document}

%----- New Slide-----
\begin{frame}

\pgfplotstableread[format=inline,header=false,col sep=colon,row sep=\\,trim cells]
{
    ARG:3.5122347292362:4.3452661462558\\
    AUS:4.18858932664046:4.70604033257871\\
    AUT:4.00473446089377:4.74517820915089\\
}\picTable

\begin{figure}[!t]\centering
    \begin{tikzpicture}
        \begin{axis}[xlabel={\footnotesize\em Variable~1},
                xlabel style={at=(current axis.right of origin),anchor=south east},
                xticklabel style={font=\footnotesize},
                ylabel={\footnotesize\em Variable~2},
                yticklabel style={font=\footnotesize},
                xmin=2.5,xmax=4.6,ymin=2.5,ymax=5.9,
                /pgf/number format/.cd,1000 sep={},
                axis y line=middle,axis x line=bottom,width=\textwidth,height=7cm,enlarge y limits=false]
            
            \pgfplotstablegetrowsof\picTable
            \pgfmathsetmacro\rowcount{\pgfplotsretval}
            
            \pgfplotsextra
            {
                \pgfplotsinvokeforeach{0,...,\rowcount}
                {
                    \relax\pgfplotstablegetelem{#1}{[index]1}\of\picTable
                    \let\x=\pgfplotsretval
                    \pgfplotstablegetelem{#1}{[index]2}\of\picTable
                    \let\y=\pgfplotsretval
                    
                    \node[circle,draw,inner sep=1.4pt,color=orange,fill=orange!50] at 
                        (axis cs:\x,\y) {};
                }
            }
            
            \pgfplotsextra
            {
                \pgfplotsinvokeforeach{0,...,\rowcount}
                {
                    \pgfplotstablegetelem{#1}{[index]0}\of\picTable
                    \let\lbl=\pgfplotsretval
                    \pgfplotstablegetelem{#1}{[index]1}\of\picTable
                    \let\x=\pgfplotsretval
                    \pgfplotstablegetelem{#1}{[index]2}\of\picTable
                    \let\y=\pgfplotsretval
                    
                    \node[inner sep=3pt,anchor=south] at (axis cs:\x,\y) 
                        {\scalebox{0.7}[1]{\color{black!80}\tiny\lbl}};
                }
            }
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{frame}

\end{document}

答案1

\pgfplotsinvokeforeach{0,...,\rowcount}{...}将进行 4 次迭代,分别为 0、1、2 和 3。这将导致错误,因为您的数据只有 3 行。

\documentclass[intlimits,t,xcolor={dvipsnames,table}]{beamer}

\usefonttheme[onlymath]{serif}

\usepackage[utf8]{inputenc}
\usepackage[T2A,T3,T1]{fontenc}
\usepackage[british]{babel}

\usepackage{tikz,pgf,pgfplots}

%---TikZ and PGF Libraries---
%\usepgfplotslibrary{fillbetween}
\usetikzlibrary{external}
\usetikzlibrary{intersections}
%\usetikzlibrary{patterns}
%\usetikzlibrary{calc}
%\usetikzlibrary{arrows}
%\usetikzlibrary{positioning}
\pgfplotsset{compat=newest}
\tikzexternalize

\begin{document}

%----- New Slide-----
\begin{frame}[fragile]

\pgfplotstableread[format=inline,header=false,col sep=colon,row sep=\\,trim cells]
{
    ARG:3.5122347292362:4.3452661462558\\
    AUS:4.18858932664046:4.70604033257871\\
    AUT:4.00473446089377:4.74517820915089\\
}\picTable

\begin{figure}%[!t]\centering
    \begin{tikzpicture}
        \begin{axis}[xlabel={\footnotesize\em Variable~1},
                xlabel style={at=(current axis.right of origin),anchor=south east},
                xticklabel style={font=\footnotesize},
                ylabel={\footnotesize\em Variable~2},
                yticklabel style={font=\footnotesize},
                xmin=2.5,xmax=4.6,ymin=2.5,ymax=5.9,
                /pgf/number format/.cd,1000 sep={},
                axis y line=middle,axis x line=bottom,width=\textwidth,height=7cm,enlarge y limits=false]
            
            \pgfplotstablegetrowsof\picTable
            \pgfmathsetmacro\rowcount{\pgfplotsretval}
            
            \pgfplotsextra
            {
                \pgfplotsinvokeforeach{0,...,\rowcount-1}
                {
                    \relax\pgfplotstablegetelem{##1}{[index]1}\of\picTable
                    \let\x=\pgfplotsretval
                    \pgfplotstablegetelem{##1}{[index]2}\of\picTable
                    \let\y=\pgfplotsretval
                    
                    \node[circle,draw,inner sep=1.4pt,color=orange,fill=orange!50] at 
                        (axis cs:\x,\y) {};
                }
            }
            
            \pgfplotsextra
            {
                \pgfplotsinvokeforeach{0,...,\rowcount-1}
                {
                    \pgfplotstablegetelem{##1}{[index]0}\of\picTable
                    \let\lbl=\pgfplotsretval
                    \pgfplotstablegetelem{##1}{[index]1}\of\picTable
                    \let\x=\pgfplotsretval
                    \pgfplotstablegetelem{##1}{[index]2}\of\picTable
                    \let\y=\pgfplotsretval
                    
                    \node[inner sep=3pt,anchor=south] at (axis cs:\x,\y) 
                        {\scalebox{0.7}[1]{\color{black!80}\tiny\lbl}};
                }
            }
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{frame}

\end{document}

在此处输入图片描述

相关内容