3

I have a question about the many ways to define a new command. I looked through the web and TeX.SE but couldn't find a complete answer (only some partly answers like, here, here, here, or here).

I know of several ways:

  • \newcommand,
  • \DeclareRobustCommand,
  • \newrobustcmd,
  • \NewDocumentCommand,
  • \NewExpandableDocumentCommand

to name just a few, most of them also with starred versions, and of course the primitives like

  • \def,
  • \gdef,
  • \edef and so on.

I have come to the conclusion that it is usually better to use LaTeX macros to define new commands, rather than using primitives. Some arguments pro macros are better syntax (allowing you to understand the execution), security (since most macros check for existing command-names), or the possibility to patch or redefine the commands.

But when I skimmed through several other cls files by third parties, I found that they very often use the \def primitives over the elaborated macros. The latter are more or less only used in special situations.

Are there valid reasons to use primitives over LaTeX macros to define new commands; especially when writing a cls file? Maybe reduce package size, or something similar? Or just old habits?

I know that it will depend on the situation, if I simply use \newcommand or \newrobustcmd. But maybe there is a strong tendency, when to use primitives in class files rather than macros?

Edit

As suggested in my question and confirmed in the comments there is no general answer. Therefore, it would be great if some of the more experienced LaTeX authors could share their personal preferences when writing/editing a class file, and why they do it this way.

Edit 2

What I forgot so far are the expl3 commands like \cs_new.... If I understood it correctly, LaTeX3 was introduced as elaborated programming layer especially for usage in sty, cls and other backend files. So, would/should a modern approach in writing such files relay more on these expl3 commands than on primitives or more or less simple LaTeX macros?

9
  • 2
    For frontend macros: There is no good reason if you only target LaTeX to use primitives for the definitions. But for internal parsing stuff, there are things that are only really possible with primitives, for instance right delimited arguments. But if you want to play safe, you can wrap your primitve definitions in \@ifdefinable.
    – Skillmon
    Jul 14, 2023 at 15:41
  • But that internal parsing stuff seems not to be used all the time. Nevertheless, \def is used very often, also for very simple definitions like \def\command{short definition}. That's one of the points why I ask if there is a reason for that frequent use of it. But maybe its all due to my little experience working in the Latex backend...
    – lukeflo
    Jul 14, 2023 at 15:51
  • 3
    I suspect the answer to your questions is "all of the above". The range of technical skill of package/class writers varies substantially, and code gets copied over the years. So there's probably a lot of inconsistency overall. But for internal commands there's no real need for the overhead of the LaTeX versions which is probably why they get used a lot.
    – Alan Munn
    Jul 14, 2023 at 16:14
  • 1
    Class files were introduced with LaTeX2e, which was first released in 1994. At this time most of the mentioned commands weren't available. And for former LaTeX2.09 style file authors it was completely normal to use TeX primitives or a mixture of higher level LaTeX commands and TeX primitives. Nowadays LaTeX has more capabilities, official commands, and hooks to adapt behaviour and layout. Therefore it is better to use newer class files as example. Jul 14, 2023 at 20:08
  • @BerndRaichle, maybe I didn't make it clear enough. Of course I mean relatively new class files which nevertheless contain many primitives. I understand That standard class files, like article etc., still have many primitives due to historical reasons .
    – lukeflo
    Jul 14, 2023 at 20:56

0

You must log in to answer this question.