I noticed that there are two different ways to define a new function in LaTeX3: using parameter text or siganature.

For example, I can use both \cs_new:Npn and \cs_new:Nn to create a function \mytest_foo:n:

\cs_new:Npn \mytest_foo:n #1
  { some~ code~ containing~ #1 }


\cs_new:Nn \mytest_foo:n
  { some~ code~ containing~ #1 }

I only know that the second method will "detect the number of parameters automatically from the function signature". But what's the real difference between them? And how can I choose from these two \cs_new:...?

PS: Is it a bad convention to use both of them in one file?

  • \cs_set:Npn is essentiall nothing but \long\def, whereas \cs_set:Nn makes the argument from the signature. It's a little like \newcommand where you only give a number. – Henri Menke Aug 10 '17 at 10:10

First, I'll note that the :Nn version is only useful for defining 'well-behaved' expl3 functions: if we want w-type arguments or to define document commands (inside xparse), we have to use the :Npn version. Thus it is quite possible to favour :Nn but still use :Npn in some places.

With that restriction in place, the two are functionally equivalent: both versions in the question give an identical outcome. There are though two differences in use. The first is that the :Nn version has to work much harder than the :Npn one to extract the signature. If you do a trace, there is a lot of code to do it, so the :Nn version is slower. For most uses that isn't a big problem as they are being used a point-of-loading, and the actual time cost is small. However, if you are doing a definition at point of use (particularly in a 'tight' loop) then you'll want the:Npn.

The main difference is thus one of appearance/style, and the team have provided both as code authors will have different views on this. For some people, the fact that:Npn requires an explicit #1, etc. will be seen a good thing, whereas for others this will be seen as making the code less clear compared to the auto-detection of the :Nn version. There are arguments both ways here, and for this reason both versions are provided.

  • 1
    You'll see from my own code that I favour :Npn but that's just an opinion ... – Joseph Wright Aug 10 '17 at 8:30
  • ... whilst in contrast Will (for example) favours :Nn (see fontspec). – Joseph Wright Aug 10 '17 at 8:37
  • One thing is that one cannot only use :Nn (if you have some strange things) but can only use :Npn. It reminds me of the "Holy War" between spaces and tabs... – stone-zeng Aug 10 '17 at 9:33

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.