I was wondering what's the (advantage) of doing something one way over the other in different systems. For instance, I read that users of plain TEX should use \def whenever they want to create a new command rather than using \(re)newcommand in LaTeX, and dimen assignments whenever they want to set lengths to something rather than using \setlength. I was just wanting to know the reasons for this, because I use LaTeX2e, but I tend to choose \def over using \(re)newcommand or \newcommand. To be more clear, I am looking for an explanation similar to the case of using \(...\) and \[...\] over $...$ and $$...$$ respectively.
- 7,593
3 Answers
First some terminology: Plain TeX and LaTeX are formats (sets of macros written in the TeX language.) The LaTeX format provides many new commands which are only available to LaTeX users and are not available to plain TeX users. Among these are the commands \newcommand and \setlength. For a list of some of the common correspondences see:
Any command which is defined as part of the TeX language itself can be used in either the plain TeX format or LaTeX. However, the converse is not so: commands that are defined in LaTeX cannot be used in plain TeX. So plain TeX users must use \def, while LaTeX users can use either \def or \newcommand. However, there are some differences between the two:
On the differences between the length commands see:
\newcommandprevents us from accidently overriding the existing command.\defdoes not provide this compile-checking.\defcan accommodate more general delimiters for arguments such as\def\point(#1,#2){\ensuremath{(#1,#2)}}.
- 36,086
-
Thanks. But I am looking for an answer as to why a
plain-TeXuser is suggested to use\defover using\newcommandand alsodimenover\setlength. Not necessarily what they do when compiled. – night owl Nov 21 '11 at 03:45
For someone that knows LaTeX and having the choice between \def or \newcommand, there should be zero difference. That is, the difference is probably subjective/left up to personal preference. LaTeX defines \newcommand in terms of \def. As such, \newcommand merely provides a user-friendly and therefore slightly restricted interface to defining a macro.
Using \newcommand has the built-in safety of not being able to redefine a macro. For this, one should use \renewcommand. Alternatively, \providecommand can be used to define a new command; it defaults to \newcommand if the command does not exist, or discards its arguments if the command already exists (no redefinition).
- 603,163
\defpart: macros - What is the difference between \def and \newcommand. Duplicate for the\setlengthpart: What is the difference between \fboxsep=1cm and \setlength{\fboxsep}{1cm} – Alan Munn Nov 21 '11 at 03:36\defin plain TeX and using\newcommandin LaTeX. A question that could arise maybe, why can't one use either or for what platform they are working with? And sure, one probably can use either or (I never used plain-TeX), but there must be an advantage using one over the other depending on which platform you use. – night owl Nov 21 '11 at 03:39\newcommand); delimited arguments (\def)). Maybe less so for the lengths commands. Maybe you could clarify your question based on the answers given there, then? – Alan Munn Nov 21 '11 at 03:43\newcommandand\setlengthare not defined in plain TeX, so plain TeX users must use\defetc. LaTeX users can use both, since\defetc. are defined in the TeX language, and not specifically part of the plain format. So the choice is either/or for LaTeX users, but not for plain TeX users. – Alan Munn Nov 21 '11 at 04:00