1

I am trying to use function from this answer

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
    \NewExpandableDocumentCommand{\switchcondition}{ O{string} m m m }
     {
      \use:c { fraiman_#1_switch:nnn } { #2 } { #3 } { #4 }
     }

    \cs_new:Nn \fraiman_string_switch:nnn
     {
      \str_case:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_token_switch:nnn
     {
      \tl_case:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_integer_switch:nnn
     {
      \int_case:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_dimen_switch:nnn
     {
      \dim_case:nnF { #1 } { #2 } { #3 }
     }
\ExplSyntaxOff

\begin{document}

    \newcommand{\Hello}{A4}

    \switchcondition{\Hello}{
        {A4}{It is A4}
        {A5}{It is A5}
    }{Oops!}

\end{document}

The problem is that I always get "Oops!", no matter how I define \Hello.

However,

    \switchcondition{A5}{
        {A4}{It is A4}
        {A5}{It is A5}
    }{Oops!}

works.

1 Answers1

2

If you want the first argument of \str_case:nnF to be fully expanded, you have to use \str_case_e:nnF instead:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
    \NewExpandableDocumentCommand{\switchcondition}{ O{string} m m m }
     {
      \use:c { fraiman_#1_switch:nnn } { #2 } { #3 } { #4 }
     }

    \cs_new:Nn \fraiman_string_switch:nnn
     {
      \str_case_e:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_token_switch:nnn
     {
      \tl_case:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_integer_switch:nnn
     {
      \int_case:nnF { #1 } { #2 } { #3 }
     }
    \cs_new:Nn \fraiman_dimen_switch:nnn
     {
      \dim_case:nnF { #1 } { #2 } { #3 }
     }
\ExplSyntaxOff

\begin{document}

    \newcommand{\Hello}{A4}

    \switchcondition{\Hello}{
        {A4}{It is A4}
        {A5}{It is A5}
    }{Oops!}

\end{document}

enter image description here

  • 1
    \str_case_e:nnF also fully expands some of the token lists inside the second argument (A4 and A5 in the OP's example: these two are unchanged by this process unless catcodes have been severely tampered with). If expansion of only the first argument is desired, one can use variants such as \str_case:onF (exactly one expansion step) or \str_case:xnF (full expansion). This is a complement, not a criticism of the answer. :-) – frougon Jul 21 '19 at 12:14
  • Well, I must add that \str_case:xnF won't work correctly in this context (called from a macro defined with \NewExpandableDocumentCommand). \str_case:enF should, but requires a relatively recent TeX engine, otherwise it will be very slow. But \str_case:onF is always an option, if one expansion step is enough. – frougon Jul 21 '19 at 12:23