0

I am still trying to get my head around expandability and its effects on commands / functions.

I used \newcommand instead of \NewDocumentCommand since the latter is not expandable and I am not using protected functions but still the result of that function can not be evaluated by \num. What am I doing wrong?

\documentclass{article}

\usepackage{siunitx}

\ExplSyntaxOn

\fp_gzero_new:N \g__invoice_sum_fp

\NewDocumentCommand{\resetinvoicesum}{} { \fp_gzero_new:N \g__invoice_sum_fp } \resetinvoicesum

\NewDocumentCommand{\addtoinvoicesum}{m} { \fp_gadd:Nn \g__invoice_sum_fp { #1 } }

\newcommand{\showinvoicesum}[1][default] { \fp_eval:n { \g__invoice_sum_fp } }

\ExplSyntaxOff

\begin{document}

\addtoinvoicesum{12}

\showinvoicesum{}

\num{\showinvoicesum}

\end{document}

mrCarnivore
  • 1,505
  • 3
    The optional argument mechanism used by \newcommand is not expandable. In general it is not possible to robustly parse for an optional argument if it's not followed by at least one mandatory argument in an expandable way, so \showinvoicesum is not expandable. – Skillmon May 12 '23 at 16:37
  • Thanks! I had missed to remove that for my MWE and it turned out to be the reason... Removing it works. But adding [2][default] (one optional followed by a mandatory argument) does not work either. Neither does [1] (only one mandatory argument) so no arguments at all otherwise it is not expandable? – mrCarnivore May 12 '23 at 16:41
  • Correction: [1] does work. So no optional arguments allowed to make it expandable? There is not way around it? – mrCarnivore May 12 '23 at 17:06
  • 1
    you can use optional arguments with commands defined by \NewExpandableDocumentCommand – David Carlisle May 12 '23 at 17:47
  • 1
    what is the point of the argument, as it is never used?? \newcommand{\showinvoicesum}[1][default] { \fp_eval:n { \g__invoice_sum_fp } } – David Carlisle May 12 '23 at 17:51
  • Also, instead of \fp_eval:n I'd suggest using \fp_to_decimal:N \g__invoice_sum_fp (should be faster, but I didn't test). – Skillmon May 12 '23 at 18:52
  • @DavidCarlisle: The optional argument is necessary in my real example. But trying to provide a short MWE I removed the code where it was used and missed that part and used that MWE since it did show the same problem. Of course, removing the optional part that is unnecessary in this example would solve that MWE but does not solve my real example... It is hard to create good MWEs that behave exactly the same without fully understanding the mechanisms.... Thank you for the hint with \NewExpandableDocumentCommand! – mrCarnivore May 12 '23 at 19:00
  • @DavidCarlisle: What is so crazy in optional commands (in the ones that provide a default value at least) that they prevent expanding? Even \NewExpandableDocumentCommand does not allow only optional parameters but must have a mandatory parameter at the end... – mrCarnivore May 12 '23 at 19:09
  • you have to look ahead and grab the following character and test if it is [ you can not do that in the general case without doing a \let assignment, \futurelet\tmp... \ifx\tmp[ ... assignments are by definition not expandable – David Carlisle May 12 '23 at 19:17
  • @DavidCarlisle: Thanks for that explanation and the help! – mrCarnivore May 12 '23 at 19:27
  • 1
    this is why pgf for example never directly returns calculations but saves them in a \result macro, if you did \showinvoicesum \num{\result} your command can do non-expandable stuff and finally leave an answer in \result which then expands for \num – David Carlisle May 12 '23 at 19:40
  • 1
    To extend the explanation by @DavidCarlisle, \NewExpandableDocumentCommand essentially just grabs an argument and checks whether that is just [, and if so assumes an optional argument it then reads in. In your usage example this wouldn't be possible since the next token would be } and this would lead to a low-level TeX error as that can't be the next token when you grab a parameter, hence \NewExpandableDocumentCommand doesn't allow optional arguments at the end of the argument signature to avoid such issues. – Skillmon May 12 '23 at 20:54

0 Answers0