1

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.

2
  • \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
2

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.

2
  • 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.
    – egreg
    Apr 14 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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