관련된

관련된

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

버그입니다. if 조건을 구문 분석할 때 tikzmath다음 코드를 사용합니다.

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

에 익숙하다면 is 또는 이와 유사한 FPU코드로 인해 작동하지 않는 코드를 발견하게 될 것입니다 .\pgfmathresult1Y1.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. 그럼에도 불구하고 아이디어는 동일합니다.

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}

관련 정보