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?

9
  • 1
    +1 I really like your questions, they move me forward. :-)
    – AndréC
    Commented Aug 16, 2020 at 5:05
  • 1
    @ AndréC: Thanks!
    – Aria
    Commented Aug 16, 2020 at 5:14
  • 2
    You can't use curly braces to group terms. Use parentheses, i.e. \edef\z{(\x > \y) ? (\x) : (\y)} Commented Aug 16, 2020 at 5:58
  • 4
    \fpeval and \inteval in xfp package are just wrappers of latex3 functions \fp_eval:n and \int_eval:n, which (as well as their argument, floating point expression and integer expression) are fully documented in texdoc interface3, sec. XI.1, XXIII.2, and XXIII.9. Commented Aug 16, 2020 at 6:52
  • 1
    Why \edef? That’s the wrong bit to begin with.
    – egreg
    Commented Aug 16, 2020 at 8:01

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .