2

On page 1/2 xfp package we see ternary operator x ? y : z as a valid boolean expression. it deserves the author puts at least the definition of this. In C I was told we have:

variable = Expression1 ? Expression2 : Expression3 is equivalent to if(Expression1) { variable = Expression2;} else {variable = Expression3;}.

To check the validity of the definition above I wrote a code below:

\documentclass{article}
\usepackage{xfp}

\begin{document} \edef\x{6.25} \edef\y{-1} \edef\z {{\x > \y} ? {\x} :{ \y}} $(x>y)?x:y=\fpeval{\z}$ \end{document}

But got an error:

! File ended while scanning use of \__fp_parse_continue:NwN.

Do you know how to fix it?

Aria
  • 1,523

1 Answers1

4

You shouldn't be using \edef to begin with, unless you keep changing the meaning of \x and \y.

But this is not the cause of the error; braces should never be used in fp-expressions. You can freely use (<fp-expression>) if you're worried about the interpretation of some part in the big expression.

\documentclass{article}
\usepackage{xfp}

\begin{document}

\def\x{6.25} \def\y{-1} \def\z {\x > \y ? \x : \y}

$(x>y)?x:y=\fpeval{\z}$

\def\y{6.25} \def\z {\x >= \y ? \x+1 : \y-1}

$(x>=y)?x:y=\fpeval{\z}$

\end{document}

What's the difference between the following pieces of code?

% with \edef
\edef\x{6.25}
\edef\y{-1}
\edef\z {\x > \y ? \x : \y}

% with \def \def\x{6.25} \def\y{-1} \def\z {\x > \y ? \x : \y}

The definition of \x and \y are unaffected. In the first case, the definition of \z would be

6.25 > -1 ? 6.25 : -1

in the second case it would be

\x > \y ? \x : \y

and the values of \x and \y will be replaced with the ones which are current at run time. Which is quite likely the purpose of using \x and \y.

On the other hand, I already suggested you a way to use “named variables”, which avoids quirks with short command names. The first time you use \c for a variable and want to cite some Turkish author in your paper, you'll understand what I mean.

egreg
  • 1,121,712