what is the difference among \let, \def and \newcommand?

4,223 views
Skip to first unread message

Vahid Damanafshan

unread,
Dec 19, 2010, 3:13:45 AM12/19/10
to latexus...@googlegroups.com
Hi there,
My question is just the title of my post.

--
--------------------------------------
وحید دامن افشان
Vahid Damanafshan
http://www.damanafshan.tk
vdamana...@ms.tabrizu.ac.ir
vdaman...@gmail.com

Peter Flynn

unread,
Dec 19, 2010, 9:34:38 AM12/19/10
to latexus...@googlegroups.com
On Sun, Dec 19, 2010 at 8:13 AM, Vahid Damanafshan <math...@gmail.com> wrote:
Hi there,
My question is just the title of my post.

\let is a TeX primitive, explained on pp.206-207 of the TeXbook.
\let\foo=\bar will assign the meaning of \bar to \foo, so they become exactly equivalent.
You should not use \let unless you have read the TeXbook and know what you are doing.

\def is also a TeX primitive, explained in detail in Chapter 20 (Macros) of the TeXbook.
\def\foo#1{\textit{#1}} defines a macro \foo with one argument, whose expansion is contained in the curly braces following.
You should not use \def in LaTeX unless you have read and understood Chapter 9 :-)

\newcommand is a LaTeX command to define a new command.
\newcommand{\foo}[1]{\textit{#1}} defines a command \foo with one argument, which puts the argument text in italic type.
You normally use \newcommand in the preamble of a LaTeX document, or in a package (style) file or class file.
The command \renewcommand has to be used (the same way) if you need to overwrite an existing command (defined with \newcommand) with a new meaning.

///Peter


Vahid Damanafshan

unread,
Dec 19, 2010, 12:18:34 PM12/19/10
to latexus...@googlegroups.com
Thanks Peter.

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

Aleks Kleyn

unread,
Dec 19, 2010, 8:10:54 PM12/19/10
to latexus...@googlegroups.com

I have few more questions. AMS bulletin requires for some reason that all \def should be substitute by \newcommand. They write that using of \def code reduce the quality of text. This is not clear but this is their requirement.

 

In some cases this is possible. But difference here is that \def I can repeat as much as I need. But if I use \newcommand second time I should write \renewcommand.

 

Also for me is not clear how I can use \newcommand instead of \def in code like

\def\LanguagePrefix{R}

\def\StartCite{[}%

\def\citeBib#1{\protect\showCiteBib#1,endCite,}%

\def\endCite{endCite}%

\def\showCiteBib#1,{\def\temp{#1}%

\ifx\temp\endCite

]%

\def\StartCite{[}%

\else

     \StartCite\LanguagePrefix \ref{\LanguagePrefix bibitem: #1}%

     \@ifundefined{\LanguagePrefix ViewCite#1}{%

           \NameDef{\LanguagePrefix ViewCite#1}{}%

     }{%

     }%

     \def\StartCite{, }%

\expandafter\showCiteBib%

\fi}%

 

 

 

 

Aleks Kleyn

http://sites.google.com/site/AleksKleyn/

http://arxiv.org/a/kleyn_a_1 

http://AleksKleyn.blogspot.com/

--

Peter Flynn

unread,
Dec 20, 2010, 4:30:26 AM12/20/10
to latexus...@googlegroups.com
On Mon, Dec 20, 2010 at 1:10 AM, Aleks Kleyn <Aleks...@mailaps.org> wrote:

I have few more questions. AMS bulletin requires for some reason that all \def should be substitute by \newcommand. They write that using of \def code reduce the quality of text. This is not clear but this is their requirement.


Using \def makes the code less maintainable because there are things you can do with \def that you cannot do with \newcommand, and they are things a publisher probably only want experts to do, not users.
 

 In some cases this is possible. But difference here is that \def I can repeat as much as I need. But if I use \newcommand second time I should write \renewcommand.


Correct. That's part of what makes the code more maintainable: it's more explicit.
 

 Also for me is not clear how I can use \newcommand instead of \def in code like


As you haven't said what these commands are or where they come from, it's not possible to help.
Which AMS class do they belong to?

///Peter
 

Aleks Kleyn

unread,
Dec 20, 2010, 11:07:33 PM12/20/10
to latexus...@googlegroups.com

I use both amsbook and amsart. This macros I when communicate with this group sometime ago. The main here that I work with array. I removed unimportant macros. The problem as I understand in lines marked by %+ because they create loop which leads to error with \newcommand.

 

\def\StartCite{[}%

\def\citeBib#1{\protect\showCiteBib#1,endCite,}%+

\def\endCite{endCite}%

\def\showCiteBib#1,{\def\temp{#1}%+

\ifx\temp\endCite

]%

\def\StartCite{[}%

\else

     \StartCite \ref{ bibitem: #1}%

     \def\StartCite{, }%

\expandafter\showCiteBib%+

\fi}%

 

Aleks Kleyn

http://sites.google.com/site/AleksKleyn/

http://arxiv.org/a/kleyn_a_1 

http://AleksKleyn.blogspot.com/

 

From: latexus...@googlegroups.com [mailto:latexus...@googlegroups.com] On Behalf Of Peter Flynn
Sent: Monday, December 20, 2010 4:30 AM
To: latexus...@googlegroups.com
Subject: Re: [latexusersgroup] what is the difference among \let, \def and \newcommand?

 

On Mon, Dec 20, 2010 at 1:10 AM, Aleks Kleyn <Aleks...@mailaps.org> wrote:

--

Aleks Kleyn

unread,
Mar 6, 2011, 8:40:35 PM3/6/11
to latexus...@googlegroups.com

Last year I sent fragment bellow to ask how to implement it using renewcommand. Nobody answered, and I tried it myself. Mainly I succeeded but one place still is not clear for me. I have wrote next text

\newcommand\StartCite{}

\newcommand\citeBib[1]{}

\newcommand\showCiteBib[1]{}

\newcommand\endCite{endOfCite}%

\newcommand\Temp{}

\newcommand\Semafor{}

\renewcommand\StartCite{[}%

\renewcommand\citeBib[1]{\protect\showCiteBib#1,endOfCite,}%

\renewcommand\showCiteBib[1],{\renewcommand\Temp{#1}%

\ifx\Temp\endCite

]%

\renewcommand\StartCite{[}%

\else

     \StartCite \ref{bibitem: #1}%

     \renewcommand\StartCite{, }%

\expandafter\showCiteBib%

\fi}%

 

The problem I see in the line

\renewcommand\showCiteBib[1],{\renewcommand\Temp{#1}%

Because I cannot put “,” between command ID and its body. When I use \def this way I can sent list of words separated by “,” as parameter of def. how I can do this for the command.

Peter Flynn

unread,
Mar 8, 2011, 4:08:39 AM3/8/11
to latexus...@googlegroups.com
On Mon, Mar 7, 2011 at 1:40 AM, Aleks Kleyn <Aleks...@mailaps.org> wrote:

Last year I sent fragment bellow to ask how to implement it using renewcommand. Nobody answered, and I tried it myself. Mainly I succeeded but one place still is not clear for me. I have wrote next text

\newcommand\StartCite{}

\newcommand\citeBib[1]{}

\newcommand\showCiteBib[1]{}

\newcommand\endCite{endOfCite}%

\newcommand\Temp{}

\newcommand\Semafor{}

\renewcommand\StartCite{[}%

\renewcommand\citeBib[1]{\protect\showCiteBib#1,endOfCite,}%

\renewcommand\showCiteBib[1],{\renewcommand\Temp{#1}%

\ifx\Temp\endCite

]%

\renewcommand\StartCite{[}%

\else

     \StartCite \ref{bibitem: #1}%

     \renewcommand\StartCite{, }%

\expandafter\showCiteBib%

\fi}%

 

The problem I see in the line

\renewcommand\showCiteBib[1],{\renewcommand\Temp{#1}%

Because I cannot put “,” between command ID and its body. When I use \def this way I can sent list of words separated by “,” as parameter of def. how I can do this for the command.


I still don't know what you are trying to do here.

///Peter

Reply all
Reply to author
Forward
0 new messages