The TikZ user guide (version 3.18b, page 1042) says this:
ifthenelse(x,y,z)This returns y if x evaluates to some non-zero value, otherwise z is returned.
It would appear that both y and z are first evaluated and then returned, which is (to me) a surprise; a TeX \if does not evaluate the unused option -- so why does TikZ bother to evaluate an unused option? I discovered this in a vain attempt to avoid division by zero:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\foo}{0}
\pgfmathsetmacro{\foobar}{ifthenelse(equal(\foo,0),0,1)}\foobar
%% Both return the error: "You've asked me to divide 1' by0'..."
% Uncomment the following to see the problem:
%\pgfmathsetmacro{\foobar}{ifthenelse(equal(\foo,0),0,1/\foo)}\foobar
%\pgfmathsetmacro{\foobar}{ifthenelse(greater(\foo,0),1/\foo,0)}\foobar
\end{document}
There are a number of non-TikZ approaches (for the moment I have settled on \ifdim\foo pt=0...\else...\fi). Just wondering what a more TikZ-idiomatic approach might be.
\pgfmathsetmacro{\foo}...? They do not work. – sgmoye Feb 06 '21 at 15:181.0in both – Feb 06 '21 at 15:20\pgfmathsetmacro{\foo}{0}-- I posted, in error, an interim version of the file. Changed in code. Apologies. And, yes, using MacTeX2020. – sgmoye Feb 06 '21 at 15:24pgfworks.ifthenelseis a function with three arguments, and pgf evaluates the arguments first before applying the function to the values. – gernot Feb 06 '21 at 15:28\ifnumgives an error:Missing number, treated as zero.– sgmoye Feb 06 '21 at 15:37\ifdim\foo pt=0...\else...\fitrick – sgmoye Feb 06 '21 at 15:49