14

I don't understand an example from section 6.1 of expl3-intro.tex from LATEX3 documentation. It is said there:

Here are two equivalent ways of defining the function \foo:nn:

\cs_set:Npn \foo:nn  #1#2 {(#1)/(#2)}
\cs_set:cpn {foo:nn} #1#2 {(#1)/(#2)}

These macros are respectively equivalent to \TeX's

\long\def\foo:nn 

, and

\expandafter\long\expandafter\def\csname foo:nn\endcsname

.

Note the :nn suffix to denote that |\foo| takes two arguments.

Given equivalents seem to be reduced rather than complete. Could somebody provide accurate equivalents of

\cs_set:Npn \foo:nn  #1#2 {(#1)/(#2)}

and

\cs_set:cpn {foo:nn} #1#2 {(#1)/(#2)}

?

In particular, I wonder at \long\def\foo:nn. Is colon allowed in command names in Plain TeX or LaTeX 2e? Is it assumed here that catcode of : has been changed? If so, corresponding commands are suggested to be explicitly added to the above example. And where is the expanded part of definitions (i.e. {(#1)/(#2)})?

1 Answers1

17

In LaTeX3 code blocks, both : and _ are 'letters'. Thus in the demo, the control sequence is \foo:nn, including the : and the nn. As the docs say

\cs_set:Npn \foo:nn #1#2 { (#1) / (#2) }

is exactly equivalent to

\long\def\foo:nn#1#2{(#1)/(#2)}

and

\cs_set:cpn { foo:nn } #1#2 { (#1) / (#2) }

is exactly equivalent to

\expandafter\long\expandafter\def\csname foo:nn\endcsname#1#2{(#1)/(#2)}

Here, I've taken advantage of fact that spaces are ignored in LaTeX3 code blocks. I've also assumed in the first example that : is a 'letter'.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • So, \long\def should be prepended with \catcode`:=11 and \catcode`_=11. – Igor Kotelnikov Nov 24 '11 at 08:49
  • @Igor Conceptually yes, in the same way as saying that with a lot of LaTeX2e snippets "do not forget \makeatletter". – Joseph Wright Nov 24 '11 at 08:56
  • @IgorKotelnikov I just want to note here that \ExplSyntaxOn/Off is the expl3 analogue to \makeatletter/other. Catcode assignments aren't required :) (I say this here because I've seen it and they cited this Q.) – Sean Allred May 18 '15 at 18:43