# Using \ifthenelse inside tikzmath

I'm trying to draw a simple plot of certain line I need, and I've used a conditional to generalize every case.

My MWE is:

\documentclass{article}

\usepackage{ifthen}

\usepackage{pgfplots}

\pgfplotsset{my style1/.append style={clip = false, axis lines* = middle, axis equal, xtick = \empty, ytick = \empty}}

\usepackage{tikz}
\usetikzlibrary{calc, math, fpu}

\begin{document}

\ifthenelse{4=3}{hi}{bye}

\begin{tikzpicture}
\tikzmath{
\lra = 1;
\lrb = -3;
\dor = 0;
\ifthenelse{\lra = 0}{\dor = 4/\lrb}{\dor = 4/\lra};
}
\begin{axis}[my style1, samples=200]
\end{axis}
\end{tikzpicture}

\end{document}


But I'm getting the error "Argument of \ifthenelse has an extra }". I can't understand why. Using \ifthenelse in normal text, it worked.

Any help would be appreciated.

• \ifthenelse is not expandable. You can't use it in such calculations. Apr 14 at 11:05
• What are you trying to do? \tikzmath has its own if ... then ... else construct. Why are you trying to use \ifthenlese here? Apr 14 at 12:35

You should use the ifthenelse function. With a slight complication, because both expressions in the true and false branches are evaluated and you can't divide by zero.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc, math, fpu}

\begin{document}

\tikzmath{
\lra = 1;
\lrb = -3;
\dor = ifthenelse(\lra == 0, 4/\lrb, 4/(or(\lra,1)*\lra));
}
\show\dor

\tikzmath{
\lra = 0;
\lrb = -3;
\dor = ifthenelse(\lra == 0, 4/\lrb, 4/(or(\lra,1)*\lra));
}
\show\dor

\end{document}

> \dor=macro:
->4.0.
l.13 \show\dor

?
> \dor=macro:
->-1.33333.
l.20 \show\dor


The or function keeps PGF happy in the second case when it would otherwise try division by zero.

• github.com/pgf-tikz/pgf/issues/1006 Probaly hard to fix, because it looks like it's the parser that is evaluating eagerly. Apr 14 at 12:41
• @HenriMenke Also \fp_eval:n has the conditional, and for technical reasons it evaluates all the involved expressions. It’s just important to warn about it. Apr 14 at 15:21