3

I would like to define a command that has a starred version and pass it to \fp_eval:n for some calculations. However, regardless of whether I use the starred or the regular version, I get the error when using the command inside \fp_eval:n:

! You can't use `\edef' after \the.
<argument> \cs_set_nopar:Npx 

Full example:

\documentclass{standalone}
\usepackage{expl3,xparse}
\ExplSyntaxOn
  \cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}

\NewDocumentCommand\nostar{}{1}
\fpeval{\nostar} % works fine

\NewDocumentCommand\withstar{s}{\IfBooleanTF{#1}{2}{3}}
\fpeval{\withstar} % does not work
\fpeval{\withstar*} % does not work either

\end{document}

Because \fp_eval:n is from LaTeX3, I assumed that the best way to define the command is with \NewDocumentCommand from xparse. It also attempted to use \WithSuffix or \@ifstar, but no success.

It seems the same problem occurs if I let the command have an optional argument. Mandatory arguments work fine.

  • The s argument type prevents expansion as would [] do as well. Both are optional argument types. Neither \NewExpandableDocumentCommand nor \DeclareExpandableDocumentCommand won't work here as well, since it ends with an optional argument, which is not allowed for that type of macros – user31729 May 15 '17 at 8:43
  • By there way, there is xfp package, which does \fpeval for you ;-). I think, the only way to make this working, is to shift the \fpeval into \withstar, after the \IfBoolean test – user31729 May 15 '17 at 8:47
  • @ChristianHupfer Thanks for your reply. I declare commands for constants and want to use them in different calculations, so putting \fpeval into \withstar is not possible for me. I am surprised that this does not work, because so far I never experienced restrictions on where commands with optional arguments or stars can be used. Does this mean there is no chance to do what I would like, even when using a different way than \NewDocumentCommand to declare my command? Or somehow forcing expansion of the arguments? If there is no way, maybe this should be an answer? – Philipp Wendler May 15 '17 at 8:55
  • I know of now way, but this does not mean that there's none! Perhaps you have to use another approach instead of using * - commands – user31729 May 15 '17 at 9:01
  • You can make a command with a *-variant expandable only if it also has a trailing mandatory argument: \NewExpandableDocumentCommand{\withstar}{sm}{\IfBooleanTF{#1}{2}{3}} and call it as \fpeval{\withstar{}} or \fpeval{\withstar*{}}. Just the * cannot work, because in this case LaTeX has to perform assignments in order to peek at the next token. – egreg May 15 '17 at 9:38
2

Commands with a *-variant cannot, in general, work in an expansion context, which is required by \fp_eval:n.

The problem is that the macro has to look forward in order to see if a * follows and the only safe way is to do a peek with \futurelet which then makes the construction not fully expandable. This affects \@ifstar, \WithSuffix and \NewDocumentCommand.

There is a workaround with xparse, but it requires the macro have a trailing mandatory argument. After

\NewExpandableDocumentCommand{\myvar}{sm}{%
  \IfBooleanTF{#1}{2}{3}%
}

you can call

\fpeval{\myvar{}}
\fpeval{\myvar*{}}

Complete example; note that xfp takes care of defining \fpeval.

\documentclass[border=4]{standalone}
\usepackage{expl3,xparse,xfp}

\NewExpandableDocumentCommand\nostar{m}{1}

\NewExpandableDocumentCommand\withstar{sm}{\IfBooleanTF{#1}{2}{3}}

\begin{document}

\fpeval{\nostar{}} % works fine

\fpeval{\withstar{}} % works fine

\fpeval{\withstar*{}} % works fine

\end{document}

enter image description here

I also defined \star with a mandatory argument for uniformity. Think to it as C.

  • It seems etextools manages to do it, somehow (with \FE@ifstar)? (Sadly, the package seems to be incompatible with (e.g.) BibLaTeX…) – Magnus Lie Hetland Apr 17 at 19:07
  • @MagnusLieHetland etextools is not only incompatible with biblatex. – egreg Apr 17 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.