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

Problem with LaTeX and \xspace

91 views
Skip to first unread message

Luc St-Louis

unread,
Nov 2, 1997, 3:00:00 AM11/2/97
to

Please look at the following example:

\documentclass{report}
\RequirePackage{xspace}
\newenvironment{ENV}{}{\xspace}
\newcommand{\CMD}{\begin{ENV}hello\end{ENV}}
\begin{document}
1 \CMD 2 \CMD! 3
\end{document}

I expected it to print:
1 hello 2 hello! 3
but instead, there is a space before the '!':
1 hello 2 hello ! 3

Can someone explain this to me?

Robin Fairbairns

unread,
Nov 3, 1997, 3:00:00 AM11/3/97
to

In article <345CFD9D...@cam.org>, Luc St-Louis <lu...@cam.org> wrote:
>\documentclass{report}
>\RequirePackage{xspace}
>\newenvironment{ENV}{}{\xspace}
>\newcommand{\CMD}{\begin{ENV}hello\end{ENV}}
>\begin{document}
>1 \CMD 2 \CMD! 3
>\end{document}
>
>I expected it to print:
> 1 hello 2 hello! 3
>but instead, there is a space before the '!':
> 1 hello 2 hello ! 3

\xspace works by looking ahead to the next token that would be
processed. If it appears in the `ending' argument to an environment
definition, when it gets executed it will see some part of the
environment wrap-up code, decide it's not a punctuation character, and
insert the space.

The document says that \xspace is for use in the argument of a
command, *not* in the arguments of an environment: what you need is

\newcommand{\CMD}{hello\xspace}
--
Robin (Campaign for Real Radio 3) Fairbairns r...@cl.cam.ac.uk
U of Cambridge Computer Lab, Pembroke St, Cambridge CB2 3QG, UK
Home page: http://www.cl.cam.ac.uk/users/rf/robin.html

Luc St-Louis

unread,
Nov 3, 1997, 3:00:00 AM11/3/97
to

Robin Fairbairns wrote:
>
> In article <345CFD9D...@cam.org>, Luc St-Louis <lu...@cam.org> wrote:
> >\documentclass{report}
> >\RequirePackage{xspace}
> >\newenvironment{ENV}{}{\xspace}
> >\newcommand{\CMD}{\begin{ENV}hello\end{ENV}}
> >\begin{document}
> >1 \CMD 2 \CMD! 3
> >\end{document}
> >
> >I expected it to print:
> > 1 hello 2 hello! 3
> >but instead, there is a space before the '!':
> > 1 hello 2 hello ! 3
>
> \xspace works by looking ahead to the next token that would be
> processed. If it appears in the `ending' argument to an environment
> definition, when it gets executed it will see some part of the
> environment wrap-up code, decide it's not a punctuation character, and
> insert the space.
>
> The document says that \xspace is for use in the argument of a
> command, *not* in the arguments of an environment: what you need is
>
> \newcommand{\CMD}{hello\xspace}

Thanks for the explanation. I see how using
'\newcommand{\CMD}{hello\xspace}' would work, but I am really trying to
design an _environment_ that would append a space at the end, the way
\xspace would. I am an experienced programmer, but relatively new to TeX
and LaTex. Can you give me a few pointers on how to go about designing
such an environment?

Donald Arseneau

unread,
Nov 3, 1997, 3:00:00 AM11/3/97
to

In article <345E6D33...@cam.org>, Luc St-Louis <lu...@cam.org> writes...

>Thanks for the explanation. I see how using
>'\newcommand{\CMD}{hello\xspace}' would work, but I am really trying to
>design an _environment_ that would append a space at the end, the way
>\xspace would.

There is no reason to. \xspace might be needed because spaces after
commands are ignored, but spaces after } are preserved just fine.
In fact, \end contains a system of *deleting* following spaces.

>I am an experienced programmer, but relatively new to TeX
>and LaTex. Can you give me a few pointers on how to go about designing
>such an environment?

Look at the definition of \end in LaTeX; it is in ltmiscen.dtx.

Donald Arseneau as...@triumf.ca

Luc St-Louis

unread,
Nov 4, 1997, 3:00:00 AM11/4/97
to

Donald Arseneau wrote:
>
> In article <345E6D33...@cam.org>, Luc St-Louis <lu...@cam.org> writes...
> >Thanks for the explanation. I see how using
> >'\newcommand{\CMD}{hello\xspace}' would work, but I am really trying to
> >design an _environment_ that would append a space at the end, the way
> >\xspace would.
>
> There is no reason to. \xspace might be needed because spaces after
> commands are ignored, but spaces after } are preserved just fine.
> In fact, \end contains a system of *deleting* following spaces.
>

Maybe I'm going about this wrong. Let me explain exactly what I'm trying
to accomplish. I have to typeset a document that will contain a number
of abbreviations, in the form of commands. For instance, suppose the
abbreviations \a and \b should respectively expand to 'A' and 'B'. The
expansion of 'The \a and the \b, etc.' will not, as you know, have a
space after the \a unless we use \a\. If at all possible, I would rather
not have to impose that extra backslash to the author of the document.
'\newcommand{\a}{A\xspace}' would be fine if there was not also the
requirement that the expansion may need to be, for example, in a font
different from the surrounding text. I would like to be able to
experiment different fonts for _all_ these abbreviations at once. So I
thought I might get away with something like

\newcommand{\a}{\begin{TrySomeFont}A\end{TrySomeFont}}
\newcommand{\b}{\begin{TrySomeFont}B\end{TrySomeFont}}
...
\newenvironment{TrySomeFont}{...set the font here...}{...\xspace}

But, alas, that doesn't work.

I will take a look at ltmiscen.dtx (I have to download it first), but if
you have any ideas on how I can solve my problem, short of cutting and
pasting any font changes into each abbreviation definition :-(, I would
appreciate them.

Thanks - Luc

Diego Zamboni

unread,
Nov 4, 1997, 3:00:00 AM11/4/97
to Luc St-Louis

Luc St-Louis <lu...@cam.org> writes:
> Maybe I'm going about this wrong. Let me explain exactly what I'm trying
> to accomplish.
> [...]

> '\newcommand{\a}{A\xspace}' would be fine if there was not also the
> requirement that the expansion may need to be, for example, in a font
> different from the surrounding text. I would like to be able to
> experiment different fonts for _all_ these abbreviations at once. So I
> thought I might get away with something like
>
> \newcommand{\a}{\begin{TrySomeFont}A\end{TrySomeFont}}
> \newcommand{\b}{\begin{TrySomeFont}B\end{TrySomeFont}}
> ...
> \newenvironment{TrySomeFont}{...set the font here...}{...\xspace}
>
> But, alas, that doesn't work.

Maybe I'm misunderstanding your question, but why not do this:

\newcommand{\trymyfonts}[1]{{\bfseries#1}}
\newcommand{\myabbrev}[1]{\trymyfonts{#1}\xspace}

\newcommand{\a}{\myabbrev{A}}
\newcommand{\b}{\myabbrev{B}}

Where you can change \bfseries to any sequence of font-selection
commands that you want. For example, for italics-sans-serif, you could
use "\sffamily\itshape". And you can define any new abbreviations you
want with the \myabbrev command.

Then, when you want to change the font of your abbreviations, you just
change the definition of \trymyfonts, and voila.

Cheers,
--Diego

Greg Salomons

unread,
Nov 4, 1997, 3:00:00 AM11/4/97
to

> Maybe I'm going about this wrong. Let me explain exactly what I'm trying
> to accomplish. I have to typeset a document that will contain a number
> of abbreviations, in the form of commands. For instance, suppose the
> abbreviations \a and \b should respectively expand to 'A' and 'B'. The
> expansion of 'The \a and the \b, etc.' will not, as you know, have a
> space after the \a unless we use \a\. If at all possible, I would rather
> not have to impose that extra backslash to the author of the document.
> '\newcommand{\a}{A\xspace}' would be fine if there was not also the
> requirement that the expansion may need to be, for example, in a font
> different from the surrounding text. I would like to be able to
> experiment different fonts for _all_ these abbreviations at once. So I
> thought I might get away with something like
>
I would strongly recomend that you do leave the added space up to the author.
If the author wishes to place punctuation immediately following \a he would
have a greater difficulty removing the extra space than in adding the
space where required.

\,|,/
~(o o)~
/===================oOO--(_)--OOo===========================================\
Greg Salomons | Email: smo...@eeyore.phy.queensu.ca
Dept. of Physics | Phone: (613) 545-6000 ext. 7375
Queen's University | Fax: (613) 545-6463
Kingston, Ont. Canada, | Disclaimer: I accept no responsibility for the
K7L-3N6 | fact that my employer may not share my opinions.
\===========================================================================/
|/ \|
(_) (_)


0 new messages