TransWikia.com

Using ifthenelse inside tikzmath

TeX - LaTeX Asked on June 5, 2021

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{compat=newest}

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]
        addplot [red, domain=-dor:dor] ({x*lra},{x*lrb});
    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.

One Answer

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));
}
showdor

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

end{document}
> dor=macro:
->4.0.
l.13 showdor

?
> dor=macro:
->-1.33333.
l.20 showdor

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

Correct answer by egreg on June 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP