How to use choice keys in l3?

I want to create some shortcuts for constants and I want to use these shortcuts for

• calculations (without units) for example with \fpeval
• with one or two different units

I tried the following code, but I dont understand really the use of choice-keys and the the use of the value for calculations with \fpeval do not work.

Thanks for help.

\documentclass{article}
\usepackage{xfp}
\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand { \numSc } { O{3} m } {
\num[scientific-notation=true,round-mode=figures,round-precision=#1] { \fp_eval:n { #2 } }
}

\keys_define:nn { thomas/elC }
{
dez .int_set:N = \l__thomas_test_dez_int,
dez .initial:n = 4,
unit .choice:,
unit / choice-As .code:n = { \ampere\second },
unit / choice-C .code:n = { \coulomb },
%  unit .initial:n = As,
}

\NewDocumentCommand { \elC } { s O{} } {
\group_begin:
\keys_set:nn { thomas/elC } { #2 }
\IfBooleanTF {#1}
{
\SI [scientific-notation=true,round-mode=figures,round-precision=\l__thomas_test_dez_tl]
{ 1.6021766209e-19 } { \tl_use:N \l_keys_choice_int }
}
{ \num [scientific-notation=true,round-mode=figures,round-precision=\l__thomas_test_dez_int] { 1.6021766209e-19 } }
\group_end:
}
\ExplSyntaxOff

\def\elCh{1.6021766209e-19}

\begin{document}

\numSc[6]{ 3*\elCh }

\elC*[dez=5]

\end{document}

• What exactly does not work? For me it does compile. What is your real question? – TeXnician Dec 9 '17 at 9:28
• @TeXnician yes this code compiles, but I want to have the units. Sometimes As or C. And this does not work – user139826 Dec 9 '17 at 9:31
• Btw: You do not need to load xfp, siunitx suffices. Do also note that you do not want to do an int_set on a variable you name like a token list, make it _int instead. – TeXnician Dec 9 '17 at 9:32
• And use the command for calculations does not work too. – user139826 Dec 9 '17 at 9:33
• Which command does not work? \fp_eval does. – TeXnician Dec 9 '17 at 9:39

The following does work by introducing a variable that holds the unit. If you set a key you cannot use a direct value, but have to use some intermediate form.

Some further improvements:

• You do not need xfp, siunitx suffices.
• You should name an integer like an integer, not like a token list (_int instead of _tl).
• If you set an initial value, it must have been defined as a choice.

Code:

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand { \numSc } { O{3} m } {
\num[scientific-notation=true,round-mode=figures,round-precision=#1] { \fp_eval:n { #2 } }
}

\tl_new:N \l__thomas_unit_tl
\keys_define:nn { thomas/elC }
{
dez .int_set:N = \l__thomas_dez_int,
dez .initial:n = 4,
unit .choice:,
unit / choice-As .code:n = { \tl_set:Nn \l__thomas_unit_tl { \ampere\second } },
unit / choice-C .code:n = { \tl_set:Nn \l__thomas_unit_tl { \coulomb } },
unit .initial:n = choice-As,
}

\NewDocumentCommand { \elC } { s O{} } {
\group_begin:
\keys_set:nn { thomas/elC } { #2 }
\IfBooleanTF {#1}
{
\SI [scientific-notation=true,round-mode=figures,round-precision=\l__thomas_dez_int]
{ 1.6021766209e-19 } { \tl_use:N \l__thomas_unit_tl }
}
{ \num [scientific-notation=true,round-mode=figures,round-precision=\l__thomas_dez_int] { 1.6021766209e-19 } }
\group_end:
}
\ExplSyntaxOff

\def\elCh{1.6021766209e-19}

\begin{document}

\numSc[6]{ 3*\elCh }

\elC*[dez=5]

\end{document}

• -@TeXnician thanks this works fine. And thanks for your improvements. – user139826 Dec 9 '17 at 9:41