Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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. –  Bruno Le Floch Feb 2 '12 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 '12 at 21:07
add comment

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.