8

I have several functions defined by similar way, and they will be used later by the name base + argument.

I do not find \cs_use:c, but there is \use:c and \cs_if_exist_use:c. Is this the correct way to do such thing or I should use something else like \str_case:nn?

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new_protected:Nn \my_test_a:n
  {
    *#1*
  }
\cs_new_protected:Nn \my_test_b:n
  {
    +#1+
  }
\NewDocumentCommand { \test } { m m }
  {
    \cs_if_exist_use:c { my_test_#1:n } { #2 }
  }
\ExplSyntaxOff

\begin{document}
\test{a}{abc}
\test{b}{abc}
\end{document}
1
  • 1
    There is also \cs_if_exist:cTF, which might help.
    – Alan Xiang
    Aug 31, 2020 at 3:28

2 Answers 2

10

It depends on what you want to do when the composed function does not exist.

The function \cs_if_exist_use:c is essentially the same as \use:c: they're wrappers around \csname...\endcsname. The former, however does a check that the latter doesn't.

What's the reason for \cs_if_exist_use:c, then? It is part of the series

\cs_if_exist_use:c \cs_if_exist_use:cT \cs_if_exist_use:cF \cs_if_exist_use:cTF

If you do

\test{a}{abc}
\test{b}{abc}
\test{c}{abc}

you get

*abc* +abc+ abc

but probably you want to be warned about the third case or maybe to output nothing. With

\NewDocumentCommand { \test } { m m }
  {
    \cs_if_exist_use:cF { my_test_#1:n } { \use_none:n } { #2 }
  }

the output would be

*abc* +abc+

because \my_test_c:n doesn't exist, so TeX is instructed to put \use_none:n in the input stream, which gobbles the following n argument. You may add a suitable warning or error message in front of \use_none:n.

1
  • That's a good advice!
    – ZhiyuanLck
    Aug 31, 2020 at 8:47
5

The \use:c is from the very early days of expl3 and it is precisely for this task (without any check in situations where you know your name is valid). Arguably it could have been given the name \cs_use:c as an alias but it can be equally well useable for token list variables and so it was put into the \use_...family of names.

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.