PGF/Tikz Ifthenelse 抱怨缺少数字

PGF/Tikz Ifthenelse 抱怨缺少数字

此 MWE

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
    overline/.style={black,fill},
    underline/.style={black,fill=white}]
    
    \pgfmathsetmacro{\Px}{2}
    \pgfmathsetmacro{\Py}{3}

    \foreach \Ax/\Ay in {2/1, 0/3, 2/.5, -2/-1, 1.5/-1.5} {    
        \pgfmathsetmacro{\AdotP}{ \Ax * \Px + \Ay * \Py }
        \ifthenelse{\AdotP > 0}
        {%
            \draw[overline] (\Ax, \Ay) circle [radius=2pt];
        }%
        {%
            \draw[underline] (\Ax, \Ay) circle [radius=2pt];
        }
    }
\end{tikzpicture}
\end{document}

失败了

缺少 = 插入 \ifnum。

. l.447 \ifthenelse{\AdotP > 0} 我原本以为会看到<',=' 或 `>'。但并没有。main.tex,第 447 行

缺失数字,视为零。

. l.447 \ifthenelse{\AdotP > 0} 这里应该有一个数字;我0'. (If you can't figure out why I needed to see a number, look up在 The TeXbook 索引中插入了“奇怪的错误”。)

如何修复此问题?

答案1

\AdotP是十进制数,但\ifthenelse只处理整数。您可以使用\lengthtestas test(但最好使用 pgf 提供的函数):

\documentclass[tikz]{standalone}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
    overline/.style={black,fill},
    underline/.style={black,fill=white}]

    \pgfmathsetmacro{\Px}{2}
    \pgfmathsetmacro{\Py}{3}

    \foreach \Ax/\Ay in {2/1, 0/3, 2/.5, -2/-1, 1.5/-1.5} {
        \pgfmathsetmacro{\AdotP}{ \Ax * \Px + \Ay * \Py }
        \ifthenelse{\lengthtest{\AdotP pt > 0pt}}
        {%
            \draw[overline] (\Ax, \Ay) circle [radius=2pt];
        }%
        {%
            \draw[underline] (\Ax, \Ay) circle [radius=2pt];
        }
    }
\end{tikzpicture}
\end{document}

相关内容