Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

We have seen this excellent discussion on the differences between \def and \newcommand. From the answers the relatives merits of using \newcommand over \def are obvious.

Why are some LaTeX macros defined using \def?

Let us consider the cases one by one.


  • The highest level (kernel), in latex.ltx

    \def\newcommand{\@star@or@long\new@command}
    

The above is obvious. LaTeX is based on TeX, and \newcommand is a LaTeX overlay. \newcommand needs to be defined first using a TeX primitive. We can not move further without this bootstrapping.

But after this, there still so many \defs. (Afraid my examples have been picked up at random and hence are not representatives of some category.)

\def\@ifnch
\def\makeatletter
\def\makeatother

  • Now we move to the next level, the document classes.

In article.cls, one example of each kind.

    \newcommand\@ptsize{}

    \def\ps@headings

In book.cls, again one random example of each kind.

    \newcommand\@chapapp

    \def\@maketitle

In memoir.cls, \def inside \newcommand (I don't necessarily claim that such cases are not in the above ones.)

\newcommand{\nametest}[2]{%
  \samenamefalse
  \begingroup
  \def\@memtempa{#1} \def\@memtempb{#2}
  \ifx \@memtempa\@memtempb
    \endgroup
    \samenametrue
  \else
    \endgroup
  \fi}

  • We move one step further, the packages. Picked fancyhdr.sty at random. We find both \def and \newcommand.

    \newcommand{\fancyhead}
    
    \def\@fancyerrmsg
    

My question is, why does LaTeX use both \def and \newcommand?

Is it as simple as whatever the developer felt like using at that moment?

(Being originally a TeX human, I still use \def, may be more than \newcommand. There is no particular reason. I do this simply out of habit.)

Or are there deeper technical reasons?

share|improve this question
2  
I think it's the same for everyone else as for you :-) Note that when a user defines macros with \newcommand, he will get notified of duplications also with macros defined by \def, so for the kernel, it's mostly irrelevant. Also (a) some code was copied from TeX plain or packages and (b) sometimes you need delimited arguments. LaTeX3 will have a cleaner interface. –  Stephan Lehmke Dec 16 '13 at 5:57
    
@StephanLehmke Never thought that it is that simple. Yes, I mentioned, whatever the developer felt like using. But I was afraid I will be rebuked for being so naive. Copying some code from TeX is another perspective which you pointed out. And about your last part about delimited arguments. This powerful feature of \def is something I particularly enjoy. –  Masroor Dec 16 '13 at 6:03
1  
at least some of those uses of \def were written before \newcommand had been written. so from the very start of its life, \newcommand and friends have been competing with \def and friends; add to that the many cases where \def can do things that \newcommand can’t, and you have a serious case. sure, it would be better if latex had been programmed to some standard, but in practice ... coding was done by a small group in their own time... –  wasteofspace Dec 18 '13 at 20:31
add comment

3 Answers 3

up vote 28 down vote accepted

One prevailing reason for using \def is because it inherently defines something regardless of whether it already exists or not. For example, using TeX, you could define

\def\mymacro{<stuff>}

and have no concern that \mymacro will exist after this call. In contrast, using pure LaTeX, one would have to (roughly)

\makeatletter
\@ifundefined{mymacro}
  {\newcommand{\mymacro}{<stuff>}}
  {\renewcommand{\mymacro}{<stuff>}}
\makeatother

While some of the LaTeX constructs (in latex.ltx) use the above check-before-defining, it's often just a convenient choice to \def (\edef, \gdef or \xdef).

Moreover, TeX's \def allows for specifying a parameter text which is very powerful, and cannot be replicated with ease using pure LaTeX (some discussion on this regard in Changing from \def to \newcommand*).

share|improve this answer
8  
\providecommand{\mymacro}{}\renewcommand{\mymacro}{<stuff>} would be a bit easier IMHO :) –  cgnieder Dec 16 '13 at 10:30
    
@cgnieder: Great addition! –  Werner Dec 16 '13 at 18:12
add comment

Much of the style of the core latex format and classes such as article goes back to the initial latex 2.x and earlier. Note that it used to take minutes to load article.sty (as it was then) the whole business of removing comments (formalised these days as the dtx/doc system) was because it would be visibly quicker to load the uncommented file than the commented one. In such a situation, defining a high level command system with error checks for the user to use, but defining almost all the core functionality using lower level macros made absolute sense.

LaTeX2e changed it a bit but not much. One thing we did do at that time is to use the latex \setlength rather than a primitive length setting in all user accessible lengths (eg the width argument of minipage or p columns) so that they would be affected by the redefinition of \setlength if the calc package was loaded. But most defs stayed as def:-)

That said, given the facilities of latex2e, it does still make sense to use both forms in internal code. Taking your last example

\newcommand{\fancyhead}

This is the top level command, if this is already defined you may as well generate an error as something bad has happened, there is some other clashing code around.

\def\@fancyerrmsg

But once you have decided you are going to load the package you may as well just define what needs defining without checking everything it's quicker (less important now, but this package is older than latex2e:-) but also you don't really want to generate an error on every internal definition, Ideally you want one error saying the package is clashing, or no errors if everything is good.

share|improve this answer
2  
Nice account of the actual reasons for it all :-) –  Stephan Lehmke Dec 16 '13 at 9:31
    
Good to know the historical side of the issue. Also, the last part is an eye-opener. –  Masroor Dec 16 '13 at 9:33
add comment

Sadly it really is whatever the developer felt like. Sometimes its because it used to be a TEX, other times its because "That's what the developer was used it". LaTeX has gone through a few revisions and grown exponentially, far more than expected, so its natural for a codebase of that size to have a "few" quirks in it.

LaTeX3 is supposed to have a cleaner interface for defining commands, you can read about it here: LaTeX3

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.