Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by Symbol 1 for pgfplots on tikzmath function with conditionals returns an error

It is a bug. When tikzmath parses the if condition, it uses the following code

\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%}

If you are familiar with FPU, you will find the code not working because \pgfmathresult is 1Y1.0e0] or something similar.

The solution is to force a conversion when FPU is on. Like the following

\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%}

Also one needs to change this as well

\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%}

Related

Using ifthenelse in pgfmath might be helpful.

That is no longer a problem because ifthenelse is now officially supported. Nonetheless the idea is the same.

An MWE

\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}

Viewing latest article 1
Browse Latest Browse All 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>