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 \def
s. (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?
\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.\def
is something I particularly enjoy.\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...