“声明函数”在组图中不起作用吗?

“声明函数”在组图中不起作用吗?

考虑以下 MWE:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=1.13}
\usetikzlibrary{babel, calc, arrows.meta}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}[
    ]
        \begin{groupplot}[
                group style = {group size = 1 by 2},
                width=15cm, height=4.5cm,
                xmin=0, xmax=100, 
                ymin=0, ymax=12,
                xlabel = {x},
                ylabel = {y},
                grid,
                declare function = {r1(\x)=2*\x/10 - \x/800*\x;},
                declare function = {r2(\x)=2*\x - \x/10*\x;},
            ]
            \nextgroupplot[
                % declare function = {r1(\x)=2*\x/10 - \x/800*\x;},
                % declare function = {r2(\x)=2*\x - \x/10*\x;},
            ]
            \addplot[ultra thick, red,  domain=0:10] {r2(x)};
            \addplot[thick, dotted, red,  domain=10:20, forget plot] {r2(x)};

            \nextgroupplot[
                % declare function = {r1(\x)=2*\x/10 - \x/800*\x;},
                % declare function = {r2(\x)=2*\x - \x/10*\x;},
            ]

            \addplot[ultra thick, cyan,  domain=0:80] {r1(x)};
            \addplot[thick, dotted, cyan,  domain=80:100, forget plot] {r1(x)};
        \end{groupplot}

\end{tikzpicture}
\end{document}

编译会失败

buggp.tex|18 error| Package pgfkeys Error: I do not know the key '/pgfplots/declare function', to which you passed 'r1(\x )=2*\x /10 - \x /800*\x ;', and I am going to ignore it. Perhaps you misspelled it.
[...]

但是,如果我将单独的函数定义移到\nextgroupplot上面的代码中(注释),它将正常工作,并输出正确的结果:

在此处输入图片描述

这是错误/功能groupplots还是我做错了什么?我想分享小组中所有图表的定义。

答案1

declare function/tikz我认为属于,所以/tikz/declare function有效。我还没有尝试弄清楚发生了什么,可能是groupplots环境没有显示/tikz

顺便说一下,您只需要一个,可以在同一个函数中定义多个函数。或者,您可以在序言中使用\begin{tikzpicture}[declare function=..或。\tikzset{declare function=..

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=1.13}
\usetikzlibrary{babel, calc, arrows.meta}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}[
    ]
        \begin{groupplot}[
                group style = {group size = 1 by 2},
                width=15cm, height=4.5cm,
                xmin=0, xmax=100, 
                ymin=0, ymax=12,
                xlabel = {x},
                ylabel = {y},
                grid,
                /tikz/declare function = {
                  r1(\x)=2*\x/10 - \x/800*\x;
                  r2(\x)=2*\x - \x/10*\x;
                },
            ]
            \nextgroupplot
            \addplot[ultra thick, red,  domain=0:10] {r2(x)};
            \addplot[thick, dotted, red,  domain=10:20, forget plot] {r2(x)};

            \nextgroupplot    
            \addplot[ultra thick, cyan,  domain=0:80] {r1(x)};
            \addplot[thick, dotted, cyan,  domain=80:100, forget plot] {r1(x)};
        \end{groupplot}

\end{tikzpicture}
\end{document}

相关内容