pgfplots
使用语法定义的函数进行绘图tikzmath
function
似乎不起作用。
错误信息:
! Illegal unit of measure (pt inserted).
<to be read again>
Y
l.30 \addplot {myAbsConditional(x)} ;
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)
! Missing = inserted for \ifdim.
<to be read again>
Y
l.30 \addplot {myAbsConditional(x)} ;
I was expecting to see `<', `=', or `>'. Didn't.
代码 :
\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\usepackage{pgfplots}
\tikzmath
{
function myAbs (\x) % this function can be plotted...
{
return (2 * (\x > 0) - 1 ) * \x;
};
function myAbsConditional(\x) % ... but not this one !
{
if (\x > 0) then
{
return \x;
}
else
{
return -\x;
};
};
% let's check that the function works :
\a = myAbsConditional(-3);
\b = myAbsConditional(2);
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
%\addplot {myAbs(x)} ; % this is OK
% the myAbsConditional function works all right ...
\node {\a\b} ;
\addplot {myAbsConditional(x)} ; % ... but cannot be plotted
\end{axis}
\end{tikzpicture}
\end{document}
这是一个错误吗?是否应该修改带有条件的函数?
答案1
这是一个错误。tikzmath
解析 if 条件时,它使用以下代码
\def\tikz@math@if@@doif{% \pgfmathparse{\tikz@math@if@condition}% \ifdim\pgfmathresult pt=0pt\relax% \else% \expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}% \fi% \tikz@math@parse% }
如果您熟悉FPU
,您会发现代码不起作用,因为\pgfmathresult
是1Y1.0e0]
或类似的东西。
解决方案是在打开时强制转换FPU
。如下所示
\def\tikz@math@if@@doif{%
\pgfmathparse{\tikz@math@if@condition}%
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
也需要改变这一点
\def\tikz@math@if@@doifelse{%
\pgfmathparse{\tikz@math@if@condition}%
\let\tikz@math@if@falseaction=\tikz@math@collected%
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\expandafter\tikz@math\expandafter{\tikz@math@if@falseaction}%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
有关的
在 pgfmath 中使用 ifthenelse可能会有帮助。
这不再是问题,因为ifthenelse
现在已得到官方支持。尽管如此,想法是一样的。
数学家
\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\usepackage{pgfplots}
\makeatletter
\def\tikz@math@if@@doif{%
\pgfmathparse{\tikz@math@if@condition}%
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\def\tikz@math@if@@doifelse{%
\pgfmathparse{\tikz@math@if@condition}%
\let\tikz@math@if@falseaction=\tikz@math@collected%
\message{^^JCheck this: \pgfmathresult^^J}% <--- Notice this
\ifpgfmathfloatparseactive% <--- Notice this
\pgfmathfloattofixed{\pgfmathresult}% <--- Notice this
\fi% <--- Notice this
\message{^^JCheck again: \pgfmathresult^^J}%<--- Notice this
\ifdim\pgfmathresult pt=0pt\relax%
\expandafter\tikz@math\expandafter{\tikz@math@if@falseaction}%
\else%
\expandafter\tikz@math\expandafter{\tikz@math@if@trueaction}%
\fi%
\tikz@math@parse%
}
\tikzmath
{
function myAbs (\x) % this function can be plotted...
{
return (2 * (\x > 0) - 1 ) * \x;
};
function myAbsConditional(\x) % ... but not this one !
{
if (\x > 0) then
{
return \x;
}
else
{
return -\x;
};
};
% let's check that the function works :
\a = myAbsConditional(-3);
\b = myAbsConditional(2);
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot {myAbs(x)} ; % this is OK
% the myAbsConditional function works all right ...
\node {\a\b} ;
\addplot {myAbsConditional(x)} ; % ... but cannot be plotted
\end{axis}
\end{tikzpicture}
\end{document}