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.

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.

share|improve this question
    
    
@AlanMunn: Not so fast. It is not a duplicate I think at all. I am not asking for the difference between them in what their functionality provides. I am more specifically asking why would one use one over the other. Hence, using \def in plain TeX and using \newcommand in 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 3:39
1  
Well the first answer linked mentions advantages and disadvantages of both (e.g. checking if defined (\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 3:43
1  
Given your comment below, the answer is simple: \newcommand and \setlength are not defined in plain TeX, so plain TeX users must use \def etc. LaTeX users can use both, since \def etc. 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 4:00
    
Alan: Thanks, that was even a more simple response than I expected. –  night owl Nov 21 '11 at 4:02
show 1 more comment

3 Answers

up vote 13 down vote accepted

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:

share|improve this answer
    
+1...Perfect Summary. –  night owl Nov 21 '11 at 5:07
add comment
  • \newcommand prevents us from accidently overriding the existing command. \def does not provide this compile-checking.
  • \def can accommodate more general delimiters for arguments such as \def\point(#1,#2){\ensuremath{(#1,#2)}}.
share|improve this answer
    
Thanks. But I am looking for an answer as to why a plain-TeX user is suggested to use \def over using \newcommand and also dimen over \setlength. Not necessarily what they do when compiled. –  night owl Nov 21 '11 at 3:45
add comment

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 does something similar to \def in which it redefines (via \renewcommand) or defines (via \newcommand) depending on whether the macro already exists or not.

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.