8

Which commands are there that one can use to define new commands in (La)TeX?

There are: \let, \def, \[re]newcommand[*], \DeclareRobustCommand[*], and ...?

(And I've seen things like \DeclareMathOperator.)

  • 1
    I think there is only \def which defines a new command. \let just assigns a new name. All the others are “only” wrappers around \def, of which there can be arbitrarily many. – bodo Feb 11 '13 at 07:36
  • @canaaerus I suspected something like that. But which well-known (or not so well-known) wrappers are there? – Lover of Structure Feb 11 '13 at 07:45
  • 1
    @LoverofStructure i think your question is rather vague. Just consider LaTeX2e's \newcounter. It sets up a new counter but also defines a command called \thecounter which prints the counter. So for a complete answer one would have to list a lot of macros. – bloodworks Feb 11 '13 at 07:49
  • @bloodworks Thanks for your contribution; \thecounter without any space ... that's weird. If a comprehensive list is long, so be it :-) – Lover of Structure Feb 11 '13 at 07:57
  • @LoverofStructure It would be useful to know if you are talking about primitives or higher-level wrappers, and also if you mean just macros or other cases (e.g. do \chardef or \toksdef count?). – Joseph Wright Feb 11 '13 at 08:14
  • @JosephWright I see - it looks like there are more details to be spceified than I thought. I suppose I could try and delimit my question, but I'd probably be using the notions of tokens, catcodes, and the hashtable of definitions incorrectly. What about me letting others answer in either the most general way or edit down my question to delimit its scope? The former might be better from an informational perspective. – Lover of Structure Feb 11 '13 at 08:18

1 Answers1

15

The LaTeX "official" interface provides

\newcommand
\renewcommand
\providecommand

\newlength
\newsavebox

\newenvironment
\renewenvironment
\newtheorem

\newcounter

All these commands define one or more commands (some only for internal usage) and provide tests for avoiding clobbering of existing commands. There's also \newfont, but its usage is deprecated.

The kernel has many other command-defining macros:

\DeclareRobustCommand

\DeclareTextCommand
\DeclareTextCommandDefault
\DeclareTextSymbol
\DeclareTextSymbolDefault
\DeclareTextAccent
\DeclareTextComposite
\DeclareTextCompositeCommand

\DeclareMathSymbol
\DeclareMathAccent
\DeclareMathDelimiter

\protected@edef

Of these, only the first one is commonly used in LaTeX programming (it shouldn't be used indiscriminately as a replacement of \newcommand, though). The last is very useful in core LaTeX programming.

Some additional packages provide new functions:

\DeclareMathOperator      (amsmath)
\DeclarePairedDelimiter   (mathtools)

are commonly found, but there are scores of other command-defining functions in the hundreds of packages around.


Another commonly used utility for defining commands is the primitive \let, that "copies" the current meaning of a token into a control sequence, allowing for "redefining a macro in terms of itself". However, for the nonexpert (and also the expert) programmer, the

\LetLtxMacro

provided by the letltxmacro package by H. Oberdiek is a recommended replacement.


The etoolbox package provides another set

\newrobustcmd
\renewrobustcmd
\providerobustcmd

(look at the documentation for a description).


Eventually, all of these command-defining functions boil down to the primitive commands

\def
\gdef
\edef
\xdef

\let
\futurelet

\chardef
\mathchardef    
\countdef
\dimendef
\skipdef
\muskipdef
\toksdef

\font
\read

but a description of these would require writing a chapter of a book; refer to the TeXbook or to TeX by Topic

egreg
  • 1,121,712