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

numbers illegal in a "newcommand"?

149 views
Skip to first unread message

Eric Austin

unread,
Oct 27, 2005, 9:58:26 PM10/27/05
to
I'd like to include a number in a little shortcut, but

\providecommand{\s2}{\ensuremath{\sigma_{2}}\xspace}

triggers

! LaTeX Error: Missing \begin{document}.

The problem goes away if I change my command from \s2 to \stwo, so it's
clear that LaTeX has a probelm wiht a numeral used like this. It looks
like numeral is not recognized as part of the command, and everthing
goes to pieces after that. Is there a good workaround for this?


Thanks, Eric

Morten Høgholm

unread,
Oct 27, 2005, 10:16:30 PM10/27/05
to

No.
--
Morten

Jean-Côme Charpentier

unread,
Oct 27, 2005, 10:33:20 PM10/27/05
to
Eric Austin wrote:
> I'd like to include a number in a little shortcut, but
>
> \providecommand{\s2}{\ensuremath{\sigma_{2}}\xspace}
>
> triggers
>
> ! LaTeX Error: Missing \begin{document}.
>
> The problem goes away if I change my command from \s2 to \stwo, so it's
> clear that LaTeX has a probelm wiht a numeral used like this.

It has no problem: it doesn't accept s2 as a macro name, that's all. You
can use only letter for a multiletter macro name. For LaTeX, a letter is
a..z and A..Z (by default).

> It looks
> like numeral is not recognized as part of the command,

Indeed.

> and everthing
> goes to pieces after that. Is there a good workaround for this?

You can define and use macro which "strange" name. For that,
\@namedef define this kind of macro and \@nameuse... use thess macros.
You have to enclose the code between \makeatletter and \makeatother (to
"make @ a letter" and "make @ a other = noletter"). Example:

\makeatletter
\@namedef{s2}{my definition}
...
\@nameuse{s2}
...
\makeatletter

Nevertheless, it's a bad idea to use this syntax inside your document.
It's only for internal use (preambles, packages, or classes). Inside the
document, a better idea is to use parameters. Example:

\newcommand*\s[1]{\ensuremath{\sigma_{#1}}\xspace}

Jean-Côme Charpentier

Eric Austin

unread,
Oct 28, 2005, 12:12:07 AM10/28/05
to

Jean-Côme Charpentier wrote:
> Eric Austin wrote:
> > I'd like to include a number in a little shortcut, but
> >
> > \providecommand{\s2}{\ensuremath{\sigma_{2}}\xspace}
> >
> > triggers
> >
> > ! LaTeX Error: Missing \begin{document}.
> >
> > The problem goes away if I change my command from \s2 to \stwo, so it's
> > clear that LaTeX has a probelm wiht a numeral used like this.
>
> It has no problem: it doesn't accept s2 as a macro name, that's all. You
> can use only letter for a multiletter macro name. For LaTeX, a letter is
> a..z and A..Z (by default).

snip...

> You can define and use macro which "strange" name. For that,
> \@namedef define this kind of macro and \@nameuse... use thess macros.
> You have to enclose the code between \makeatletter and \makeatother (to
> "make @ a letter" and "make @ a other = noletter"). Example:
>
> \makeatletter
> \@namedef{s2}{my definition}
> ...
> \@nameuse{s2}
> ...
> \makeatletter
>
> Nevertheless, it's a bad idea to use this syntax inside your document.
> It's only for internal use (preambles, packages, or classes). Inside the
> document, a better idea is to use parameters. Example:
>
> \newcommand*\s[1]{\ensuremath{\sigma_{#1}}\xspace}

Ouch. I use paramemters like the above all the time, but for some
reason I was looking right past that simple solution. Seeing the
alternatives, I'll take your suggestion. Thanks. EMA

Michael

unread,
Oct 28, 2005, 4:10:32 AM10/28/05
to
Note that you can still get the syntax you want by using:

\documentclass{article}
\newcommand\s[1]{\ensuremath{\sigma_{#1}}\xspace}
\begin{document}
The first expression is $\s a$, the second sigma is \s2 and the last one is
\s{n^2}.
\end{document}

The only 'problem' would be when the argument to the sigma is more than one
character or a non-number, you will need a space or {brackets} respectively.

Regards

Michael.


co...@zedat.fu-berlin.de

unread,
Oct 30, 2005, 1:25:39 AM10/30/05
to
Eric Austin <e...@clemson.edu> wrote:
: \providecommand{\s2}{\ensuremath{\sigma_{2}}\xspace}

: The problem goes away if I change my command from \s2 to \stwo, so it's


: clear that LaTeX has a probelm wiht a numeral used like this. It looks
: like numeral is not recognized as part of the command, and everthing
: goes to pieces after that. Is there a good workaround for this?

You have a catcode problem; in normal text, numbers are not allowed as part
of a command name. You can build a control sequence by saying
\csname s2\endcsname
but this will not help because in normal text parsing of the control sequence
name \s2 stops before the digit begins.

You can parametrize your command so that the digit becomes its argument, you
can also resort to roman numerals, if that is of any help to you and you
really absolutely need numbered control sequence names.

Oliver.

--
Dr. Oliver Corff e-mail: co...@zedat.fu-berlin.de

Paul Thompson

unread,
Nov 1, 2005, 1:04:07 PM11/1/05
to

Numbers have never been allowed in macro names. So, use letters.

This is not a bug, it is a feature.

0 new messages