1

OK, here is the thing. I want to store property-value pairs and then later use this as a lookup table to feed the value into another function. This is what I came up with and I don't know how to fix the problem:

\documentclass{article}

\usepackage{expl3,xparse}

\ExplSyntaxOn

% Create a new sequence.
\prop_clear_new:N {\MyLookupTable}

\prop_put:Nnn {\MyLookupTable} {key1} {intermediate1}
\prop_put:Nnn {\MyLookupTable} {key2} {intermediate2}


\cs_generate_variant:Nn \str_case:nnF {xn}
\NewDocumentCommand{\DoSomething}{m}{%
    \str_case:xnF {\str_fold_case:n{#1}}
    {
        {intermediate1} {Result~1}
        {intermediate2} {Result~2}
    }
{Unknown~intermediate}
}

\ExplSyntaxOff


\begin{document}
Test1: \ExplSyntaxOn\prop_if_in:NnTF {\MyLookupTable} {key1} {\DoSomething{\prop_item:Nn {\IconNameToUnicodeList} {key1}}} {key1~not~in~list}\ExplSyntaxOff ~However: \DoSomething{intermediate1}

Test2: \ExplSyntaxOn\prop_if_in:NnTF {\MyLookupTable} {key1} {key1~found} {key1~not~in~list}\ExplSyntaxOff ~And: \DoSomething{intermediate1}

Final: \ExplSyntaxOn

\prop_get:NnNTF {\MyLookupTable} {key1} {\answer} {TRUE: \DoSomething{\answer}} {FALSE}
\ExplSyntaxOff
\end{document}

I tried a \cs_generate_variant:Nn \DoSomething:m {V} but LaTeX didn't like it and I am still not very familiar with the LaTeX3 structure.

1
  • N arguments should always be a single token not a brace group as you have here, also note you only need to load expl3 and xparse in old formats expl3 has been loaded by default for some time and xparse since last year. Jan 4, 2022 at 21:38

1 Answer 1

2

Please, use the proper recommended naming scheme. You have a wrong name in he code, by the way.

Note that x-expansion is not expandable, but there is \str_case_e:nnF that does “expandable expansion”.

Also, you need to e-expand the argument passed to \DoSomething.

\documentclass{article}

%\usepackage{expl3,xparse}

\ExplSyntaxOn

% Create a new sequence.
\prop_clear_new:N \l_tobibs_lookup_prop

\prop_put:Nnn \l_tobibs_lookup_prop {key1} {intermediate1}
\prop_put:Nnn \l_tobibs_lookup_prop {key2} {intermediate2}

\NewExpandableDocumentCommand{\DoSomething}{m}
  {
    \tobibs_dosomething:e { #1 }
  }

\cs_new:Nn \tobibs_dosomething:n
  {
    \str_case_e:nnF {\str_foldcase:n{#1}}
      {
        {intermediate1} {Result~1}
        {intermediate2} {Result~2}
      }
      {Unknown~intermediate}
  }
\cs_generate_variant:Nn \tobibs_dosomething:n { e }

\ExplSyntaxOff

\begin{document}

Test1:
 
\ExplSyntaxOn
\prop_if_in:NnTF \l_tobibs_lookup_prop {key1}
  {\DoSomething{\prop_item:Nn \l_tobibs_lookup_prop {key1}}}
  {key1~not~in~list}
\ExplSyntaxOff

However: \DoSomething{intermediate1}

\bigskip

Test2:

\ExplSyntaxOn
\prop_if_in:NnTF \l_tobibs_lookup_prop {key1}
  {key1~found}
  {key1~not~in~list}
\ExplSyntaxOff 

And: \DoSomething{intermediate1}

\bigskip

Final:

\ExplSyntaxOn
\prop_get:NnNTF \l_tobibs_lookup_prop {key1} \answer {TRUE: \DoSomething{\answer}} {FALSE}
\ExplSyntaxOff

\end{document}

enter image description here

Avoid bracing N arguments (although it's not really a problem).

Note: \str_fold_case:n should be \str_foldcase:n (the latter used to be the official name, but it was renamed for consistency with other names).

7
  • Thank you @egreg, my local version of LaTeX seems to be to old to directly make use of your code, as it yields errors, especially without xparse and expl3, but I am on the way to update it and then understand the answer, thanks!
    – TobiBS
    Jan 4, 2022 at 22:56
  • What is the purpose of capsulating \DoSomething and what is the purpose of generating the variant through \cs_generate_variant:Nn \tobibs_dosomething:n { e } instead of directly defining it that way?
    – TobiBS
    Jan 5, 2022 at 17:40
  • @TobiBS That's the recommended way: if the document command must do complex work, its definition should just pass the arguments to an internal function. Anyway, how would you do it?
    – egreg
    Jan 5, 2022 at 17:47
  • Well as I cannot test it on my machine right now, I can just describe what I was thinking should be possible: Directly say \cs_new:Ne \tobibs_dosomething:e and would look if there is a way to directly put the code into \NewExpandableDocumentCommand{\DoSomething}{m}
    – TobiBS
    Jan 5, 2022 at 17:51
  • 1
    @TobiBS No, \cs_new:Nn only manages N and n argument types. To have e you must generate a variant.
    – egreg
    Jan 5, 2022 at 18:01

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.