[Tex/LaTex] Why using a backslash in command definitions

etoolboxmacrosxparse

I want to know the reasoning behind the (La)TeX inferface for defining commands with leading backslashes. I see why a backslash has to be used to call (expand) a command (macro), but I do not understand why it is used in definitions.

There are many ways to define a command in (La)Tex (ignoring the \let commands and the LaTeX 3 core and possible others) giving the command name with a leading backslash:

  • TeX: \def, \gdef, \edef and xdef, all having possibly the prefix \long
  • LaTeX: \newcommand, \renewcommand, providecommand, DeclareRobustCommand, all having possibly the suffix *
  • etoolbox: \newrobustcommand, \renewrobustcommand, providerobustcommand, all having possibly the suffix *
  • xparse: \DeclareDocumentCommand, \NewDocumentCommand, \RenewDocumentCommand, \ProvideDocumentCommand, all having possibly an + in the argument specification

There is also the possibility to define a command without a backlash

  • TeX: \expandafter\def\csname name \endcsname (similar for the commands mentioned for TeX above)
  • etoolbox: \csdef (similar for the commands mentioned for TeX above)

I think the definitions without a backslash have clear advantages:

  • more general: allowing to create dynamically named commands (e.g. \csdef{double#1}{#1,#1})
  • better semantic: The backslash means using a variable, i.e. expanding it
  • shorter: Save one character, do not disturb the eye

The question is: Why do the default command definitions need a backlash when it seems more useful to do without?

Specifiying the question a bit more:
Obviously, the definitions from etoolbox have advantages compared to the TeX ones being much shorter and clearer. The xparse definitions have clear advantages regarding their arguments' flexibility, too. So it seems natural to combine them. If one would define (with \expandnext from etextools) a command like

\NewDocumentCommand{\declare}{mmm}{%
    \expandnext{\DeclareDocumentCommand}{\csname #1\endcsname}{#2}{#3}%
}

what would one loose? Let us assume, that new, renew and provide would be defined similarly and let us keep the local/global discussion and the possible expansion of the third argument aside, leading to \gdeclare, \edeclare and xdeclare in the long run.

Best Answer

There are a few reasons why defining document commands has always been done using the control sequence for the command (\foo) rather than the control sequence name (foo). Of course, some of this comes down to 'you'd have to ask Leslie Lamport or Don Knuth', as it follows from the TeX basics. (For example, \newcommand\foo mirrors \def\foo, and \DeclareDocumentCommand\foo mirrors \cs_new:Npn \foo.)

First, there is an intrinsic consistency for requiring that the 'appearance' at the point of definition is the same as that at point of use.

Secondly, requiring that a token is construction avoids confusion about what can usually appear in a command name. For example, if you did

\declare{foo-bar}...

\foo-bar

the latter would not work (assuming standard LaTeX category codes) as TeX would 'see' the token \foo followed by a hyphen and the letters b, a and r. That looks like a good way to have problems.

Third, the current set up allows 'experts' to skip the { ... } pair

\newcommand\foo{definition}

works in the same way as

\newcommand{\foo}{definition}

something that was important when TeX was 'smaller' (we save two tokens there), but also something that many people find easier to read. (Moreover, as most good editors highlight LaTeX tokens differently from 'running text', using a token-based approach works better with many editors.)

Related to that, you have to remember that the current interface was designed in the 1980s based on a system developed in the late 1970s. The additional work for both \csname expansion and storage of tokens (separate letters rather than a control sequence) was much more important in the past than it is today.

Finally, if you look at the LaTeX3 stuff we want to avoid 'dynamic' definitions as far as possible at the document interface level. One of the lessons of LaTeX2.09/LaTeX2e is that a clearly defined interface is a good thing: encouraging the use of dynamic names does not help there.

Related Question