Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

\global and \newcommand

427 views
Skip to first unread message

Volker Kuhlmann

unread,
May 28, 1994, 10:05:56 PM5/28/94
to

The subject says it - is there a possibility to use the \global
qualifier together with \newcommand? \global\newcommand, not
surprisingly, does not work.

Thanks, Volker

Robin Fairbairns

unread,
May 31, 1994, 6:24:51 AM5/31/94
to
In article <2s8ta4$k...@cantua.canterbury.ac.nz>,

Have you tried

{\let\def\gdef
\let\edef\xdef
\newcommand{\blah}....
}

I've vaguely peered at the code of \newcommand, and it seems that this
will _probably_ work, but (a) don't take my word as gospel, and (b)
make sure you don't forget those braces, as otherwise the \let will
propagate, and the \global distinction will have been lost.

--
Robin (Campaign for Real Radio 3) Fairbairns r...@cl.cam.ac.uk
U of Cambridge Computer Lab, Pembroke St, Cambridge CB2 3QG, UK
Private page: http://pelican.cl.cam.ac.uk/people/rf/rf.html

Volker Kuhlmann

unread,
Jun 5, 1994, 6:02:18 AM6/5/94
to
In article <2sf39j$k...@lyra.csx.cam.ac.uk>, r...@cl.cam.ac.uk (Robin Fairbairns) writes:
> In article <2s8ta4$k...@cantua.canterbury.ac.nz>,
> Volker Kuhlmann <kuh...@elec.canterbury.ac.nz> wrote:

[deleted]

> Have you tried
>
> {\let\def\gdef
> \let\edef\xdef
> \newcommand{\blah}....
> }

[deleted]

No, I didn't think of that. Considering the effort though I am tempted
to use \gdef, \xdef in the first place, and be free to use \long\outer
at my pleasure as well. The question originated from the recommendation
to use \newcommand instead of \def, but for some cases in style-file
(sorry, package) design I don't think it's feasable. The above also
assumes that \newcommand is always using \long, which is true for
LaTeX2e[1993/12/24]. As another comment, \newcommand always using \long
defeats the purpose of having \long.

Volker

GERNOT_KATZER

unread,
Jun 5, 1994, 11:07:26 AM6/5/94
to
Robin Fairbairns (r...@cl.cam.ac.uk) wrote:
(Suggesting a brilliant solution to use \newcommand for
global macros):

> Have you tried

> {\let\def\gdef
> \let\edef\xdef
> \newcommand{\blah}....
> }

Why do most LaTeX books not encourage the use of \def for
LaTeX users? In LaTeX 2.09, \newcommand seemed to have no real
advantage compared with \def, but lacks all the possibilities
arising from delimited parameters.

Although the situation has changed a little bit in LeTeX2e,
the question remains: Is \def evil for LaTeX applications?

--
| Gernot Katzer
How does a system manager change a light bulb? | kat...@bkfug.kfunigraz.ac.at
| kat...@balu.kfunigraz.ac.at
He doesn't. He just denies access to everyone to |
the area served by the light bulb in question. | NEVER make me sysmgr!
|

David Wald

unread,
Jun 5, 1994, 8:17:42 PM6/5/94
to
In article <2sspne$h...@balu.kfunigraz.ac.at>

kat...@edvz.kfunigraz.ac.at (GERNOT_KATZER) writes:
>
>Why do most LaTeX books not encourage the use of \def for
>LaTeX users?

Because \def doesn't check to see if you're redefining something. Do
you know how many times I've seen novice LaTeX users try to define
\wlog as an abbreviation for "w.l.o.g." or "without loss of
generality"?

>Although the situation has changed a little bit in LeTeX2e,
>the question remains: Is \def evil for LaTeX applications?

Depends on what you mean by application. Sometimes it's necessary to
use a \def variant, but even in style files it's safer to let
\newcommand check for conflicts when feasible. An "already defined"
error is a lot easier to track than a mysterious internal clash.

-David
--
============================================================================
David Wald http://theory.lcs.mit.edu/~wald wa...@theory.lcs.mit.edu
"Blessed are the peacocks, for they shall be called sonship of God"
-- Matt 5:9, from a faulty QuickVerse 2.0
============================================================================

Gernot Salzer

unread,
Jun 6, 1994, 5:41:57 AM6/6/94
to
In article <WALD.94J...@woodpecker.lcs.mit.edu>, wa...@theory.lcs.mit.edu (David Wald) writes:
|> In article <2sspne$h...@balu.kfunigraz.ac.at>
|> kat...@edvz.kfunigraz.ac.at (GERNOT_KATZER) writes:
|> >
|> >Why do most LaTeX books not encourage the use of \def for
|> >LaTeX users?
|>
|> Because \def doesn't check to see if you're redefining something. Do
|> you know how many times I've seen novice LaTeX users try to define
|> \wlog as an abbreviation for "w.l.o.g." or "without loss of
|> generality"?
|>
|> >Although the situation has changed a little bit in LeTeX2e,
|> >the question remains: Is \def evil for LaTeX applications?
|>
|> Depends on what you mean by application. Sometimes it's necessary to
|> use a \def variant, but even in style files it's safer to let
|> \newcommand check for conflicts when feasible. An "already defined"
|> error is a lot easier to track than a mysterious internal clash.

Whenever I want to combine the checking capabilities of \newcommand with the better
parameter interface of \def I do the following:

\newcommand{\mynewcmd}{}
\def\mynewcmd ......

This can, of course, be packed into a macro of its own:

\newcommand{\cdef}[1]{\newcommand{#1}{}\def#1}

(\cdef stands for "checked def"). Then you can write things like

\cdef\add#1+#2=#3{#1+#2=#3}
\cdef\wlog{w.l.o.g.}

where the latter gives you an error.

Note, however, that \cdef can't be used with \long, \outer, \global:
\long\cdef
gives an error, since \long applies to the first command in the expanded
definition of \cdef (which is a \let) and not to the \def at the end of
the definition. In this case you either have to use \newcommand+\long\def,
or define a new command \longcdef.

By the way: I have written a little packages that allows to use optional parameters
at arbitrary argument positions of commands/environments defined by
\(re)newcommand /\(re)newenvironment. In LaTeX2e you just can make the first
argument an optional one. If anyone is interested, send me an email.

Regards,
Gernot Salzer

Daniel Luecking

unread,
Jun 6, 1994, 2:15:46 PM6/6/94
to
wa...@theory.lcs.mit.edu (David Wald) writes:

>In article <2sspne$h...@balu.kfunigraz.ac.at>
>kat...@edvz.kfunigraz.ac.at (GERNOT_KATZER) writes:
>>
>>Why do most LaTeX books not encourage the use of \def for
>>LaTeX users?

>Because \def doesn't check to see if you're redefining something. Do
>you know how many times I've seen novice LaTeX users try to define
>\wlog as an abbreviation for "w.l.o.g." or "without loss of
>generality"?

Reminds me of my first \newcommand attempt:

\newcommand{\fi}{\varphi}

When that failed (with a helpful message) I tried

\renewcommand(\fi}{\varphi}

and TeX went all to pieces, spewing gibberish left and right... :-).

Dan Luecking

--
luec...@comp.uark.edu | Anti-disclaimer:
Department of Mathematical Sciences | You are free to ascribe any
University of Arkansas | opinions herein contained
Fayetteville, AR 72701 USA | to anyone or anything.

Olaf Deppe

unread,
Jun 6, 1994, 2:30:08 PM6/6/94
to
:::From: wa...@theory.lcs.mit.edu (David Wald)
:::Subject: Re: \global and \newcommand
:::Date: Mon, 6 Jun 94 02:17:42 GMT-0:05
:::Organization: Theory of Computation, LCS, MIT
:::
:::In article <2sspne$h...@balu.kfunigraz.ac.at>

:::kat...@edvz.kfunigraz.ac.at (GERNOT_KATZER) writes:
:::>
:::>Why do most LaTeX books not encourage the use of \def for
:::>LaTeX users?
:::
:::Because \def doesn't check to see if you're redefining something. Do
:::you know how many times I've seen novice LaTeX users try to define
:::\wlog as an abbreviation for "w.l.o.g." or "without loss of
:::generality"?
:::
:::>Although the situation has changed a little bit in LeTeX2e,
:::>the question remains: Is \def evil for LaTeX applications?
:::
:::Depends on what you mean by application. Sometimes it's necessary to
:::use a \def variant, but even in style files it's safer to let
:::\newcommand check for conflicts when feasible. An "already defined"
:::error is a lot easier to track than a mysterious internal clash.
:::
:::-David

There is an easy way to combine the conflict test of \newcommand
with all the advantages of \def:

% \makeatletter -- if not within a style file
\newcommand{\new}[2]{\@ifdefinable #2\relax #1#2}
\new\def\ndef{\new\def}

\ndef can be used the same way like \def, but with the safety of \newcommand.
Only if you also need the `prefixes' \outer, \global, etc you need to apply
another syntax:

\new{\long\def}\morethanonepar>>#1<<{``#1'' is more than one paragraph.}
\new{\edef}\blabla{\morethanonepar>>bla\par bla<<}


(Now \typeout\blabla yields
``bla\par bla'' is more than one paragraph.
)

--
Olaf Deppe, Hamburg
de...@desy.de

Éamonn McManus

unread,
Jun 8, 1994, 6:53:37 AM6/8/94
to
sal...@loria.fr writes:
> Whenever I want to combine the checking capabilities of \newcommand with
> the better parameter interface of \def I do the following:
> \newcommand{\mynewcmd}{}
> \def\mynewcmd ......
> This can, of course, be packed into a macro of its own:
> \newcommand{\cdef}[1]{\newcommand{#1}{}\def#1}
[...]

> Note, however, that \cdef can't be used with \long, \outer, \global:
[...]

Here's a way to define \cdef that allows it to be prefixed by \long
etc. It uses a different test for undefinedness from that used by
LaTeX, so if you say
\let\xrelax\relax
it will not allow you to redefine \xrelax (LaTeX will).

(This is not a good thing in all circumstances. I have never
understood why TeX makes a new \csname c.s. equal \relax instead of
leaving it undefined.)

Some care has been taken so that the macros work even if you
incorrectly attempt to redefine a c.s. that is \outer or that means
the same as \if or \else or \fi.

\def\cdef{% The parameter might be \outer, so use a subterfuge.
\expandafter\@cdef\noexpand
}
\def\@cdef#1{%
\ifx\@undefined#1%
\expandafter\def
\else
\def\@junk{}% There might be \long, \outer, or \global prefixes
\message{\string#1 = \meaning#1}%
\errhelp{I'm throwing away this redefinition.}%
\errmessage{`\string#1' is already defined}%
\expandafter\@throwaway
\fi
#1% Right at the end so we don't skip over it when \ifx is false.
}
\def\@throwaway{%
\expandafter\@throwthrow\noexpand
}
\def\@throwthrow#1{\def\@junk}

,
Eamonn

0 new messages