7

\fpeval doesn't like decimal numbers with commas as separator:

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{siunitx,xfp}
\sisetup{locale=DE}

\begin{document}
\num{1.5}
\num{1,5}
\num{\fpeval{1.5+1.4 + max(1.5 , 2 )}} 
\num{\fpeval{1,5+1,4 + max(1,5 , 2 )}} %error
\num{\fpeval{1,5+1.4}} %error

\end{document}

This gives

! Undefined control sequence.
<argument> \LaTeX3 error: 
                           Unexpected comma: extra arguments ignored.

As a german I'm rather used to commas (and other germans too) and so I constantly have to correct my input. Is there an expandable way to get this working?

The difficulty is naturally that the comma is used in the \fpeval syntax, e.g. for max. I would accept the requirement to use spaces here, or to use another symbol e.g. ;, or to use some protection command like \dontsanitize{max(1.5,2)}, or max(1,5 \realcomma 2).

Ulrike Fischer
  • 327,261
  • 2
    The simpler approach would be to use \sanitize{1,5}. In any case I really don't understand the use of commas, I understand that one wants the output to be a comma (that's okey) but in the input, I think it's absolutely better to use a dot. Other programming languages use that, plus there's no doubt of what you wrote in max(1,5,2) (which is what is read inside \ExplSyntax(On|Off)). In any case, may be it could be changed the syntax to max(1,2;2) but I don't know the internals. – Manuel May 30 '17 at 10:11
  • 1
    @Manuel I guess you aren't german ;-). When you are used to write decimals with commas since you were a child, it is not easy to always avoid that you do it also when you type. Beside this: due to my locale most applications (e.g. the windows calculator or my spreadsheet) copy & paste numbers with commas. – Ulrike Fischer May 30 '17 at 10:43
  • 1
    Using spaces around nondecimal commas is out of the question, I'm afraid. It may be possible to make the fp module use either commas or periods for the decimal separator and a semicolon for separating arguments, but I wouldn't dare asking Bruno to implement it. Having a standard syntax for numeric fp expressions is much better. – egreg May 30 '17 at 11:59
  • @egreg: Even if it were possible I don't think that it would be a good idea to add it to fp directly. I would prefer an (expandable) command that I can inject if needed (and it would be okay if replace all commas by dots, the max-case seldom occur in the wild ...) – Ulrike Fischer May 30 '17 at 12:43

1 Answers1

4

You could replace the ,digit by .digit using l3regex

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{siunitx,xfp}
\usepackage{l3regex}%unless you have very new expl3
\sisetup{locale=DE}
\let\oldnum\num
\ExplSyntaxOn
\def\num#1{\def\tmp{#1}\regex_replace_all:nnN{,([0-9])}{.\1}{\tmp}\oldnum{\tmp}}
\ExplSyntaxOff

\begin{document}
\num{1.5}
\num{1,5}
\num{\fpeval{1.5+1.4 + max(1.5 , 2 )}} 
\num{\fpeval{1,5+1,4 + max(1,5 , 2 )}} %error
\num{\fpeval{1,5+1.4}} %error

\end{document}
David Carlisle
  • 757,742