使用 \pgfplotspointmeta 的 PGFplots 中的问题

使用 \pgfplotspointmeta 的 PGFplots 中的问题

我在文章类中有一个运行良好的代码:

\begin{figure}[!htb]
\centering
\begin{tikzpicture}
    \begin{axis}[
        xlabel=km,
        ylabel=km,
        ylabel near ticks,
        width=0.6\textwidth,
        height=0.6\textwidth,
        only marks,nodes near coords={},
        nodes near coords style={
        coordinate,
        name=\pgfplotspointmeta},
        clip marker paths=true,
        point meta=explicit symbolic
        ]
        \addplot[scatter,mark=*,draw=blue,mark options={scale=1, fill=white}] 
        table [
            x=x,
            y=y,
            meta=label] 
            {
                     x      y    label
            -6960.5568  -1250.1     1
            -7802.8464  -991.1904   2
            -7156.128   -420.0336   3
            -6804.9888  150.012     4
            -5902.6944  -526.7088   5
    };
    \end{axis} 
    %\foreach \i in {1,...,5} {
    %        \node[fill=lime,draw=lime,circle,minimum size=1.5pt,scale=0.2,
    %              label={
    %                    [label distance=-3pt]above:{\scalebox{0.2}{\i}}
    %                     },
    %              ] (T) at (\i) {};
    %}
\end{tikzpicture}
\caption{Fluxo de mínimo custo em $t=1$ (modelo 1)}
\end{figure}
%\end{frame}
\end{document}

但是,当我在 beamer 类中转换相同的代码时,会出现错误:

\documentclass[12pt,fleqn]{beamer}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{pgfplotstable}
\usetikzlibrary{positioning}
\usepackage{float}
\usetikzlibrary{plotmarks}
\usetikzlibrary{patterns}
\usepackage{mathtools}
\usetikzlibrary{arrows}
\usetikzlibrary{quotes,angles}
\usetikzlibrary{shapes,shadings}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{fit,shapes.geometric}
\usepackage{xfp}
\usetikzlibrary{math}


\begin{document}

\begin{frame}{Frame Title}
\begin{figure}[!htb]
\centering
\begin{tikzpicture}
    \begin{axis}[
        xlabel=km,
        ylabel=km,
        ylabel near ticks,
        width=0.6\textwidth,
        height=0.6\textwidth,
        only marks,nodes near coords={},
        nodes near coords style={
        coordinate,
        name=\pgfplotspointmeta},
        clip marker paths=true,
        point meta=explicit symbolic
        ]
        \addplot[scatter,mark=*,draw=blue,mark options={scale=1, fill=white}] 
        table [
            x=x,
            y=y,
            meta=label] 
            {
                     x      y    label
            -6960.5568  -1250.1     1
            -7802.8464  -991.1904   2
            -7156.128   -420.0336   3
            -6804.9888  150.012     4
            -5902.6944  -526.7088   5
    };
    \end{axis} 
    \foreach \i in {1,...,5} {
            \node[fill=lime,draw=lime,circle,minimum size=1.5pt,scale=0.2,
                  label={
                        [label distance=-3pt]above:{\scalebox{0.2}{\i}}
                         },
                  ] (T) at (\i) {};
    }
\end{tikzpicture}
\caption{Fluxo de mínimo custo em $t=1$ (modelo 1)}
\end{figure}
\end{frame}

\end{document}

有人能帮助我吗?

答案1

使用fragile框架来避免错误。

其他一些评论:

  • 你不需要加载所有的amsmath等包,beamer 会自动为你加载它们

  • beamer 没有浮动机制。加载包float或指定浮动说明符(如)是没有意义的[!htb]

  • beamer 的图形环境将自动将内容居中,不需要\centering

  • 您不应该将其\scalebox用于文本。如果您选择合适的小字体大小,那么该大小的字母形状将更容易阅读。


\documentclass[12pt,fleqn]{beamer}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel}
%\usepackage{amsmath,amssymb,amsthm}
\usepackage{pgfplotstable}
\usetikzlibrary{positioning}
%\usepackage{float}
\usetikzlibrary{plotmarks}
\usetikzlibrary{patterns}
\usepackage{mathtools}
\usetikzlibrary{arrows}
\usetikzlibrary{quotes,angles}
\usetikzlibrary{shapes,shadings}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{fit,shapes.geometric}
\usepackage{xfp}
\usetikzlibrary{math}


\begin{document}

\begin{frame}[fragile]{Frame Title}
\begin{figure}%[!htb]
%\centering
\begin{tikzpicture}
    \begin{axis}[
        xlabel=km,
        ylabel=km,
        ylabel near ticks,
        width=0.6\textwidth,
        height=0.6\textwidth,
        only marks,nodes near coords={},
        nodes near coords style={
        coordinate,
        name=\pgfplotspointmeta},
        clip marker paths=true,
        point meta=explicit symbolic
        ]
        \addplot[scatter,mark=*,draw=blue,mark options={scale=1, fill=white}] 
        table [
            x=x,
            y=y,
            meta=label] 
            {
                     x      y    label
            -6960.5568  -1250.1     1
            -7802.8464  -991.1904   2
            -7156.128   -420.0336   3
            -6804.9888  150.012     4
            -5902.6944  -526.7088   5
    };
    \end{axis} 
    \foreach \i in {1,...,5} {
            \node[fill=lime,draw=lime,circle,minimum size=1.5pt,scale=0.2,
                  label={
                        [label distance=-3pt]above:{\Tiny \i}
                         },
                  ] (T) at (\i) {};
    }
\end{tikzpicture}
\caption{Fluxo de mínimo custo em $t=1$ (modelo 1)}
\end{figure}
\end{frame}

\end{document}

相关内容