![“声明函数”在组图中不起作用吗?](https://linux22.com/image/367276/%E2%80%9C%E5%A3%B0%E6%98%8E%E5%87%BD%E6%95%B0%E2%80%9D%E5%9C%A8%E7%BB%84%E5%9B%BE%E4%B8%AD%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%E5%90%97%EF%BC%9F.png)
考虑以下 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}