Plotting with pgfplots
a function defined using the tikzmath
function
syntax does not seem to work.
The error message :
! Illegal unit of measure (pt inserted).<to be read again> Yl.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 todelete the erroneous units; e.g., type `2' to deletetwo letters. (See Chapter 27 of The TeXbook.)! Missing = inserted for \ifdim.<to be read again> Yl.30 \addplot {myAbsConditional(x)} ;I was expecting to see `<', `=', or `>'. Didn't.
The code :
\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}
Is this a bug ? Should the function with a conditional be modified ?