2

Could someone please explain how xparse-expl3 command \str_case:nnF works by an example? Are str and case important here?

Please avoid long (text) answers. I think if you explain the basics, I can use it wherever I want.

2
  • 1
    Hmmm... I do not understand well the question. str_case:nnF is the name of the command. It's described in interface3 manual, page 64. – Rmano Sep 18 '20 at 11:35
  • 2
    tex.stackexchange.com/… – egreg Sep 18 '20 at 13:21
5

As short as I can:

\str_case:nnF { <string> }
  {
    { <case 1> } { <code 1> }
    { <case 2> } { <code 2> }
    ...
    { <case n> } { <code n> }
  }
  { <false code> }

The command will compare <string> with each <case n> string, in order, and if it finds a match, the corresponding <code n> is executed. If no matches are found, <false code> is executed.


Example: suppose you have a macro to which the user passes some options, say, above, center, or below, and it runs some code depending on the option chosen, and raises an error if something nonsense is typed:

\cs_new_protected:Npn \cfg_test:n #1
  {
    \str_case:nnF {#1}
      {
        { above  } { <'above' code> }
        { center } { <'center' code> }
        { below  } { <'below' code> }
      }
      { \msg_error:nn { cfg } { invalid-option } {#1} }
  }

then \cfg_test:n {center} would run <'center' code> and \cfg_test: {wrong} would raise an error (though if this were for an user interface, l3keys could be used instead).


An expl3 command (variables are different) have a name of the form:

\<module>_<name>:<signature>

The <module> says where that command is from: str is expl3's string module, so you know that \str_case:nnF operates on strings.

The <name> says what the command does: case is used in expl3 for functions that behave as the switch case constructs in other programming languages. expl3's \<whatever>_case:nn(TF) functions always have the same general syntax as above: it only changes what type of comparison they perform. \int_case:nnF, for example, does the same but it compares integers.

The <signature> tells you how many, and what type of arguments the command takes. n is a normal {...}-delimited argument, and F is the same as n, but it's executed if the command evaluates to false (now guess what T means). Take a look at expl3.pdf: you'll find a description of all possible signatures and the kernel <modules>. Then take a look at interface3.pdf, where you'll find a description of what every expl3 command does.

8
  • Thanks a lot. Please give a simple example. and I think I can write \str_case:nnnnnnnF ? Yes? – user108724 Sep 18 '20 at 11:38
  • 1
    @C.F.G Example added. No, \<thing>_case:nn is always :nn: the second n takes a list of {<case>}{<code>} pairs. The available variations of this command are \str_case:nn and \str_case:nn(TF). (TF) means it can be only T, only F, or both TF. – Phelype Oleinik Sep 18 '20 at 11:52
  • Aha. Very helpful. – user108724 Sep 18 '20 at 11:56
  • 2
    @C.F.G Yes, that's right ($2$, more precisely :-). If you just want to count how may chars in a string you can do \str_count:n {dd} (will return 2). – Phelype Oleinik Sep 18 '20 at 12:07
  • 2
    @C.F.G Yes, as I said str is the module, and there are several \str_<thing> commands to manipulate strings. Do take a look at interface3.pdf: all expl3 commands are listed there. You'll find the documentation for the string module on page 60. / As for expl3 vs. “usual TeX”: experienced TeX programmers criticise expl3 because it's an all-new syntax to the same old TeX: it is a lot to learn for a thing you already know. But if you're a novice, expl3 is really a lot easier to understand at first contact. – Phelype Oleinik Sep 18 '20 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy