这个问题导致了一个新的方案的出现:
pgfmath-xfp
如果浮点计算超出 TeX 范围,我会使用tikzpicture
with 。问题是,看不到声明的函数,如下例所示:expl3
expl3
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
[
declare function={
functionA(\x) = \fpeval{\x};
functionB(\x) = \fpeval{ln(2)*functionA(\x)};
},
]
\draw [domain=0:1, variable=\x]
plot ({\x}, {functionB(\x)});
\end{tikzpicture}
\end{document}
这会产生这个错误:
! Undefined control sequence.
<argument> \LaTeX3 error:
Unknown fp word functionA.
l.20 plot ({\x}, {functionB(\x)})
是否可以expl3
看到声明的函数?
答案1
编辑:
下面首先描述的包装纸加拿大运输安全局,因此使用最新 LaTeX 发行版的代码是
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfmath-xfp}
\usepackage{siunitx}
\usepackage{expl3}
\pgfmxfpdeclarefunction{functionA}{1}{#1}
\pgfmxfpdeclarefunction{functionB}{1}[functionA(#1)]{ln(2) * #1}
\pgfmxfpdeclarefunction{fplog}{1}{ln(#1)}
% slow (but for demonstration)
\pgfmxfpdeclarefunction{nlogn}{1}[#1,fplog(#1)]{#1 * #2}
% faster variant of the above
\pgfmxfpdeclarefunction{NlogN}{1}{#1 * ln(#1)}
\pgfmxfpdeclarefunction{lognormal}{3}
{exp(-((ln(#1) - #2)^2) / (2 * (#3)^2)) / (#1 * #3 * sqrt(2 * pi))}
\begin{document}
\begin{tikzpicture}
\draw [domain=0:1, variable=\x]
plot ({\x}, {functionB(\x)});
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {lognormal(x,ln(5),0.2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {nlogn(x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {NlogN(x)};
\end{axis}
\end{tikzpicture}
\end{document}
输出如下。
我创建了一个包装器来定义内部pgfmath
使用的函数\fpeval
。语法如下:
\pgfmathdeclarefpevalfunction{<name>}{<arg-count>}[<arg-processors>]{<function>}
在此
<name>
是函数的名称<arg-count>
pgfmath
是函数需要的参数数量<arg-processors>
是可选的以逗号分隔的已处理参数列表。这些已处理参数将由处理pgfmath
(因此在这里您可以使用嵌套函数)。<function>
是函数应该如何解析\fpeval
。如果使用,则函数内部的参数数量<function>
是列表中声明的参数数量<arg-processors>
,否则等于提供的数量<arg-count>
请注意,以这种方式定义的函数并不是非常快,但应该在任何pgfmath
情况下都能工作。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{expl3}
\ExplSyntaxOn
\tl_new:N \l_pgffpeval_function_body_tl
\tl_new:N \l_pgffpeval_function_definition_tl
\int_new:N \l_pgffpeval_tmp_int
\cs_new_protected:Npn \pgffpeval_declare_function:nnn #1#2#3
{
\__pgffpeval_initialize_body:
\int_step_inline:nn {#2}
{
\tl_put_right:Nx \l_pgffpeval_function_body_tl
{
\exp_not:n { \pgfmathsetmacro } \exp_not:c { __pgffpeval_arg##1 }
{ \exp_not:n {####} ##1 }
}
}
\__pgffpeval_define_function:nnnn {#2} {#1} {#2} {#3}
}
\cs_new_protected:Npn \pgffpeval_declare_function_processed_args:nnnn #1#2#3#4
{
\__pgffpeval_initialize_body:
\int_zero:N \l_pgffpeval_tmp_int
\clist_map_inline:nn {#3}
{
\int_incr:N \l_pgffpeval_tmp_int
\tl_put_right:Nx \l_pgffpeval_function_body_tl
{
\exp_not:n { \pgfmathsetmacro }
\exp_not:c { __pgffpeval_arg \int_use:N \l_pgffpeval_tmp_int }
{ \exp_not:n {##1} }
}
}
\exp_args:NV
\__pgffpeval_define_function:nnnn \l_pgffpeval_tmp_int {#1} {#2} {#4}
}
\cs_new_protected:Npn \__pgffpeval_initialize_body:
{
\tl_set:Nn \l_pgffpeval_function_body_tl
{
\group_begin:
\pgfkeys{/pgf/fpu=true, /pgf/fpu/output~format=sci}%
}
}
\cs_new:Npn \__pgffpeval_process_function_aux:n #1 { \exp_not:n {## #1} }
\cs_new_protected:Npn \__pgffpeval_process_function:nnn #1#2#3
{
\exp_last_unbraced:Nx
\cs_set_protected:cpn
{
{ __pgffpeval_function_ #2 _cmd }
\int_step_function:nN {#1} \__pgffpeval_process_function_aux:n
}
{ \group_end: \exp_args:Nf \pgfmathparse { \fp_eval:n {#3} } }
}
\cs_new_protected:Npn \__pgffpeval_define_function:nnnn #1#2#3#4
{
\__pgffpeval_process_function:nnn {#1} {#2} {#4}
\tl_put_right:Nx \l_pgffpeval_function_body_tl
{
\use:x
{
\exp_not:c { __pgffpeval_function_ #2 _cmd }
\int_step_function:nN {#1} \__pgffpeval_define_function_aux:n
}
}
\exp_args:Nnno
\pgfmathdeclarefunction {#2} {#3} \l_pgffpeval_function_body_tl
}
\cs_new:Npn \__pgffpeval_define_function_aux:n #1
{ { \exp_not:c { __pgffpeval_arg#1 } } }
\NewDocumentCommand \pgfmathdeclarefpevalfunction { m m o m }
{
\IfValueTF {#3}
{ \pgffpeval_declare_function_processed_args:nnnn {#1} {#2} {#3} }
{ \pgffpeval_declare_function:nnn {#1} {#2} }
{#4}
}
\ExplSyntaxOff
\pgfmathdeclarefpevalfunction{functionA}{1}{#1}
\pgfmathdeclarefpevalfunction{functionB}{1}[functionA(#1)]{ln(2) * #1}
\pgfmathdeclarefpevalfunction{fplog}{1}{ln(#1)}
% slow (but for demonstration)
\pgfmathdeclarefpevalfunction{nlogn}{1}[#1,fplog(#1)]{#1 * #2}
% faster variant of the above
\pgfmathdeclarefpevalfunction{NlogN}{1}{#1 * ln(#1)}
\pgfmathdeclarefpevalfunction{lognormal}{3}
{exp(-((ln(#1) - #2)^2) / (2 * (#3)^2)) / (#1 * #3 * sqrt(2 * pi))}
\begin{document}
\begin{tikzpicture}
\draw [domain=0:1, variable=\x]
plot ({\x}, {functionB(\x)});
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {lognormal(x,ln(5),0.2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {nlogn(x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ domain=0.01:10, samples=100 ]
\addplot {NlogN(x)};
\end{axis}
\end{tikzpicture}
\end{document}