5

I am trying to use logical functions in pgfmath and getting unexpected errors. It seems to be related to the use of \pgfmathsetmacro, which according to my understanding should be equivalent to \pgfmathparse{whatever}\let\macro=\pgfmathresult. However, the following example from the manual works with \pgfmathparse and gives the error

! Missing number, treated as zero.
<to be read again> 
                   n
l.52 ...etmacro\macro{ifthenelse(5==4,"yes","no")}
                                                   \macro

when used with \pgfmathsetmacro:

\pgfmathparse         {ifthenelse(5==4,"yes","no")}% Works
\pgfmathsetmacro\macro{ifthenelse(5==4,"yes","no")}% Fails

Shouldn't I be able to do this?

2
  • 1
    After looking at the \tracingall output for not very long, I think it might be a bug: pgf is trying to set a \dimen register to "no", which is not going to work. Feb 2, 2012 at 19:44
  • 1
    Note that this turns out to not being related to the ifthenelse operator (so the title can be misleading). \pgfmathsetmacro\macro{"no"} won't work either.
    – cjorssen
    Feb 2, 2012 at 21:07

1 Answer 1

7

Here is the definition of \pgfmathsetmacro in file pgfmathcalc.code.tex.

\def\pgfmathsetmacro#1#2{%
  \begingroup%
    \pgfmathsetlength\pgfmath@x{#2}%
    \edef#1{\pgfmath@tonumber{\pgfmath@x}}%
    \pgfmath@smuggleone{#1}%
  \endgroup%
}

The use of \pgfmathsetlength prevents from using something else than numbers as a result of the evaluation of #2 by \pgfmathparse (which is the case in your sample code). As Bruno Le Floch pointed out in comment, this leads to assign nopt to the dimension \pgfmath@x which does not work.

I must admit it's not the behavior I would expect looking at the documentation. So at least, it should be specified in the doc or the macro should be modified to deal with such string cases.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .