1

I'm trying to use the functional package for doing calculations (\fpAdd) and then use the siunitx package to format the results (\num).

Here is a very small example.

\documentclass{article}
\usepackage{siunitx}
\usepackage{functional}

\IgnoreSpacesOn
\fpNew \Total
\fpAdd \Total {5.5}
\IgnoreSpacesOff

\begin{document}
My number: \fpUse \Total. % This works.
%My number: \num{\fpUse \Total}. % This does not work.
\end{document}

Uncommenting the last line gives the following error.

! Package siunitx Error: Invalid number '\fpUse \s__fp \__fp_chk:w
(siunitx) 10{1}{5500}{0000}{0000}{0000};'.

Does anyone know how to do that properly?

Thanks for your help.

1
  • 3
    The commands from the functional package are not expandable. Why don't you use the inbuilt latex command \fpeval? Commented Jan 8, 2023 at 9:06

1 Answer 1

0

The commands in functional aren't expandable since they go against the TeX language, trying to make it into something different.

Macro expansion goes “from outside to inside”, whereas functional wants to achieve the reverse strategy, so it has to avoid macro expansion. Not a judgment, just a description.

However, \num wants to fully expand its main argument in order to have an explicit number to manipulate and functional commands can't work.

If you just need to manipulate floating point variables, define your own equivalents:

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand{\newfp}{mo}
 {
  \fp_new:c { l_francesco_var_#1_fp }
  \IfValueT { #2 }
   {
    \fp_set:cn { l_francesco_var_#1_fp } { #2 }
   }
 }
\NewDocumentCommand{\setfp}{mm}
 {
  \fp_set:cn { l_francesco_var_#1_fp } { #2 }
 }
\NewDocumentCommand{\addfp}{mm}
 {
  \fp_add:cn { l_francesco_var_#1_fp } { #2 }
 }
\NewExpandableDocumentCommand{\usefp}{m}
 {
  \fp_use:c { l_francesco_var_#1_fp }
 }
\ExplSyntaxOff

\newfp{Total}[5.5]
\newfp{Weird}
\setfp{Weird}{pi^2-exp(3)}

\begin{document}

My number: \usefp{Total}.

My number: \num[output-decimal-marker={,}]{\usefp{Total}}.

My number: \num{\usefp{Weird}}.

\end{document}

enter image description here

Note the advantage over \fpNew\Total: you don't need to care whether \Total is defined, because \newfp{Total} defines a control sequence that's unlikely to be already used for a different purpose.

You also don't have to worry about spaces: compare

\fpUse\Total\ is a nice number

\usefp{Total} is a nice number

You must log in to answer this question.

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