Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a version of \DeclareDocumentCommand (in LaTeX 3) which performs like \edef? I.e. where the argument is evaluated at the time of definition as opposed to the time the macro is called?

For example

\ExplSyntaxOn
\fp_zero_new:N {\fpX}   
\fp_set:Nn {\fpX} {6}   
\DeclareDocumentCommand { \dX } {} {\fp_to_decimal:N{\fpX}}

\fp_set:Nn{\fpX} {12}

\dX
\ExplSyntaxOff

prints 12

\ExplSyntaxOn
\fp_zero_new:N {\fpX}   
\fp_set:Nn {\fpX} {6}   
\edef\dX{\fp_to_decimal:N{\fpX}}

\fp_set:Nn{\fpX} {12}

\dX
\ExplSyntaxOff

prints 6

and I would like \dX to expand to 6

share|improve this question
1  
Can you show a use case? –  egreg Jul 6 at 23:42
add comment

1 Answer 1

up vote 8 down vote accepted

The family of commands in xparse for creating new document commands are not really intended for this sort of thing. This can be seen in your example where \DeclareDocumentCommand is intermixed with expl3 code.

The short answer is that the expl3 version of \edef is either \cs_set:Npx or \tl_set:Nx depending if you're creating a macro that takes arguments or simply a token list.

In your example, instead of writing

\edef\dX{\fp_to_decimal:N{\fpX}}

you should just use

\tl_set:Nx \dX { \fp_to_decimal:N { \fpX } }
share|improve this answer
    
Thanks, this is exactly what I wanted. Am I correct that the x in :Nx is what tells it to expand the argument? (as when I replaced it with :Nn it didn't work) –  Nate Ackerman Jul 7 at 3:07
    
Exactly: x means ‘expand the argument fully (i.e., with \edef) before passing it to \tl_set:Nn’. See expl3.pdf or source3.pdf for a full explanation of the various argument types. –  Will Robertson Jul 7 at 4:22
    
@WillRobertson, where do you get source3.pdf? –  daleif Jul 7 at 8:29
    
@daleif — It should be available with texdoc source3 if you have a complete TeX Live or MiKTeX installation. Otherwise <mirror.ctan.org/tex-archive/macros/latex/contrib/l3kernel>; –  Will Robertson Jul 7 at 8:34
    
@WillRobertson I just tried and it doesn't work for me (may be because of that “freeze” they apply to TeX Live at some point). –  Manuel Jul 7 at 8:35
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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