Why doesn't xspace always insert a following space when that is expected?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
In the MWE, xspace
correctly inserts a space in the first test of foo
, but not in the second test when it is followed by an en-dash.
Is there a fix for this?
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
enddocument
Output:
xspace
add a comment |Â
up vote
3
down vote
favorite
In the MWE, xspace
correctly inserts a space in the first test of foo
, but not in the second test when it is followed by an en-dash.
Is there a fix for this?
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
enddocument
Output:
xspace
4
Note the question is misworded,xspace
never detects the space, the space is not available to tex macros at all. so it detectsB
in the first case and-
in the second. forB
it guesses a space is needed so adds one, for-
it guesses it is a hyphenated word, so does not. The inputfoo --
is identical to the inputfoo--
after tex parsing, neither generates a space token.
– David Carlisle
35 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In the MWE, xspace
correctly inserts a space in the first test of foo
, but not in the second test when it is followed by an en-dash.
Is there a fix for this?
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
enddocument
Output:
xspace
In the MWE, xspace
correctly inserts a space in the first test of foo
, but not in the second test when it is followed by an en-dash.
Is there a fix for this?
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
enddocument
Output:
xspace
xspace
edited 26 mins ago
asked 45 mins ago
alephzero
1,2621410
1,2621410
4
Note the question is misworded,xspace
never detects the space, the space is not available to tex macros at all. so it detectsB
in the first case and-
in the second. forB
it guesses a space is needed so adds one, for-
it guesses it is a hyphenated word, so does not. The inputfoo --
is identical to the inputfoo--
after tex parsing, neither generates a space token.
– David Carlisle
35 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago
add a comment |Â
4
Note the question is misworded,xspace
never detects the space, the space is not available to tex macros at all. so it detectsB
in the first case and-
in the second. forB
it guesses a space is needed so adds one, for-
it guesses it is a hyphenated word, so does not. The inputfoo --
is identical to the inputfoo--
after tex parsing, neither generates a space token.
– David Carlisle
35 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago
4
4
Note the question is misworded,
xspace
never detects the space, the space is not available to tex macros at all. so it detects B
in the first case and -
in the second. for B
it guesses a space is needed so adds one, for -
it guesses it is a hyphenated word, so does not. The input foo --
is identical to the input foo--
after tex parsing, neither generates a space token.– David Carlisle
35 mins ago
Note the question is misworded,
xspace
never detects the space, the space is not available to tex macros at all. so it detects B
in the first case and -
in the second. for B
it guesses a space is needed so adds one, for -
it guesses it is a hyphenated word, so does not. The input foo --
is identical to the input foo--
after tex parsing, neither generates a space token.– David Carlisle
35 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
The hyphen character -
is in xspace
's exception list1, where the space isn't added. You can remove it with xspaceremoveexception-
:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
enddocument
As David said in the comments, xspace
doesn't detect the space. As far as it knows foo--bar
and foo --bar
are exactly the same, because at the time xspace
gets to do its thing, TeX has already tokenized the macro and the next character, and any space in between was ignored.
xspace
looks ahead to see if the next character is supposed to be preceded by a space and re-inserts that space if it is needed.
With the solution above, we changed xspace
's exception list, so it will always, from now on, add a space before a -
, even when you don't want it to.
Still not satisfied?
If you want to hack into xspace
's rules, you can start by the example given in the manual:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
xspaceremoveexception-% repeated, for the sake of copy-pasting
makeatletter
renewcommand*@xspace@hook%
ifx@let@token-%
expandafter@xspace@dash@i
fi
def@xspace@dash@i-futurelet@let@token@xspace@dash@ii
def@xspace@dash@ii%
ifx@let@token-%
else
unskip
fi
-%
makeatother
foo - why no space here?
foo -- why no space here?
enddocument
There David shows how to use @xspace@hook
to check if the next two characters are --
and, if not, remove the space inserted by xspace
. This can be expanded to check more characters, but is it worth it?
My 2 cents: I used xspace
for a few months, when I was writing my bachelor's thesis. It helps you not forget to type foo
or foo
when you are in the habit of forgetting it. But with time, you need different things (as you did here), and it gets more time consuming than it should. My opinion (and David's, apparently :P).
1 The exception list, by default, contains:
,.'/?;:!~-) /bgroupegroup@sptokenspace@xobeyspfootnotefootnotemark
% ↑ Here it is :)
all true but you could also make clear that xspace never detects the space afterfoo
so if you do thisfoo--BAR
would also makeFOO --BAR
with a space being inserted by xspace.
– David Carlisle
34 mins ago
But removing-
from the exception list messes up using a single-
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)
– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwingnewcommand
away as well, and usingdeffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, butfoo/--
andfoo/ --
both do what you would expect.
– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The hyphen character -
is in xspace
's exception list1, where the space isn't added. You can remove it with xspaceremoveexception-
:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
enddocument
As David said in the comments, xspace
doesn't detect the space. As far as it knows foo--bar
and foo --bar
are exactly the same, because at the time xspace
gets to do its thing, TeX has already tokenized the macro and the next character, and any space in between was ignored.
xspace
looks ahead to see if the next character is supposed to be preceded by a space and re-inserts that space if it is needed.
With the solution above, we changed xspace
's exception list, so it will always, from now on, add a space before a -
, even when you don't want it to.
Still not satisfied?
If you want to hack into xspace
's rules, you can start by the example given in the manual:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
xspaceremoveexception-% repeated, for the sake of copy-pasting
makeatletter
renewcommand*@xspace@hook%
ifx@let@token-%
expandafter@xspace@dash@i
fi
def@xspace@dash@i-futurelet@let@token@xspace@dash@ii
def@xspace@dash@ii%
ifx@let@token-%
else
unskip
fi
-%
makeatother
foo - why no space here?
foo -- why no space here?
enddocument
There David shows how to use @xspace@hook
to check if the next two characters are --
and, if not, remove the space inserted by xspace
. This can be expanded to check more characters, but is it worth it?
My 2 cents: I used xspace
for a few months, when I was writing my bachelor's thesis. It helps you not forget to type foo
or foo
when you are in the habit of forgetting it. But with time, you need different things (as you did here), and it gets more time consuming than it should. My opinion (and David's, apparently :P).
1 The exception list, by default, contains:
,.'/?;:!~-) /bgroupegroup@sptokenspace@xobeyspfootnotefootnotemark
% ↑ Here it is :)
all true but you could also make clear that xspace never detects the space afterfoo
so if you do thisfoo--BAR
would also makeFOO --BAR
with a space being inserted by xspace.
– David Carlisle
34 mins ago
But removing-
from the exception list messes up using a single-
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)
– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwingnewcommand
away as well, and usingdeffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, butfoo/--
andfoo/ --
both do what you would expect.
– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
 |Â
show 1 more comment
up vote
4
down vote
The hyphen character -
is in xspace
's exception list1, where the space isn't added. You can remove it with xspaceremoveexception-
:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
enddocument
As David said in the comments, xspace
doesn't detect the space. As far as it knows foo--bar
and foo --bar
are exactly the same, because at the time xspace
gets to do its thing, TeX has already tokenized the macro and the next character, and any space in between was ignored.
xspace
looks ahead to see if the next character is supposed to be preceded by a space and re-inserts that space if it is needed.
With the solution above, we changed xspace
's exception list, so it will always, from now on, add a space before a -
, even when you don't want it to.
Still not satisfied?
If you want to hack into xspace
's rules, you can start by the example given in the manual:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
xspaceremoveexception-% repeated, for the sake of copy-pasting
makeatletter
renewcommand*@xspace@hook%
ifx@let@token-%
expandafter@xspace@dash@i
fi
def@xspace@dash@i-futurelet@let@token@xspace@dash@ii
def@xspace@dash@ii%
ifx@let@token-%
else
unskip
fi
-%
makeatother
foo - why no space here?
foo -- why no space here?
enddocument
There David shows how to use @xspace@hook
to check if the next two characters are --
and, if not, remove the space inserted by xspace
. This can be expanded to check more characters, but is it worth it?
My 2 cents: I used xspace
for a few months, when I was writing my bachelor's thesis. It helps you not forget to type foo
or foo
when you are in the habit of forgetting it. But with time, you need different things (as you did here), and it gets more time consuming than it should. My opinion (and David's, apparently :P).
1 The exception list, by default, contains:
,.'/?;:!~-) /bgroupegroup@sptokenspace@xobeyspfootnotefootnotemark
% ↑ Here it is :)
all true but you could also make clear that xspace never detects the space afterfoo
so if you do thisfoo--BAR
would also makeFOO --BAR
with a space being inserted by xspace.
– David Carlisle
34 mins ago
But removing-
from the exception list messes up using a single-
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)
– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwingnewcommand
away as well, and usingdeffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, butfoo/--
andfoo/ --
both do what you would expect.
– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
 |Â
show 1 more comment
up vote
4
down vote
up vote
4
down vote
The hyphen character -
is in xspace
's exception list1, where the space isn't added. You can remove it with xspaceremoveexception-
:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
enddocument
As David said in the comments, xspace
doesn't detect the space. As far as it knows foo--bar
and foo --bar
are exactly the same, because at the time xspace
gets to do its thing, TeX has already tokenized the macro and the next character, and any space in between was ignored.
xspace
looks ahead to see if the next character is supposed to be preceded by a space and re-inserts that space if it is needed.
With the solution above, we changed xspace
's exception list, so it will always, from now on, add a space before a -
, even when you don't want it to.
Still not satisfied?
If you want to hack into xspace
's rules, you can start by the example given in the manual:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
xspaceremoveexception-% repeated, for the sake of copy-pasting
makeatletter
renewcommand*@xspace@hook%
ifx@let@token-%
expandafter@xspace@dash@i
fi
def@xspace@dash@i-futurelet@let@token@xspace@dash@ii
def@xspace@dash@ii%
ifx@let@token-%
else
unskip
fi
-%
makeatother
foo - why no space here?
foo -- why no space here?
enddocument
There David shows how to use @xspace@hook
to check if the next two characters are --
and, if not, remove the space inserted by xspace
. This can be expanded to check more characters, but is it worth it?
My 2 cents: I used xspace
for a few months, when I was writing my bachelor's thesis. It helps you not forget to type foo
or foo
when you are in the habit of forgetting it. But with time, you need different things (as you did here), and it gets more time consuming than it should. My opinion (and David's, apparently :P).
1 The exception list, by default, contains:
,.'/?;:!~-) /bgroupegroup@sptokenspace@xobeyspfootnotefootnotemark
% ↑ Here it is :)
The hyphen character -
is in xspace
's exception list1, where the space isn't added. You can remove it with xspaceremoveexception-
:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
enddocument
As David said in the comments, xspace
doesn't detect the space. As far as it knows foo--bar
and foo --bar
are exactly the same, because at the time xspace
gets to do its thing, TeX has already tokenized the macro and the next character, and any space in between was ignored.
xspace
looks ahead to see if the next character is supposed to be preceded by a space and re-inserts that space if it is needed.
With the solution above, we changed xspace
's exception list, so it will always, from now on, add a space before a -
, even when you don't want it to.
Still not satisfied?
If you want to hack into xspace
's rules, you can start by the example given in the manual:
documentclassarticle
usepackagexspace
newcommandfooFOOxspace
begindocument
foo BAR
foo -- why no space here?
xspaceremoveexception-
foo -- why no space here?
xspaceremoveexception-% repeated, for the sake of copy-pasting
makeatletter
renewcommand*@xspace@hook%
ifx@let@token-%
expandafter@xspace@dash@i
fi
def@xspace@dash@i-futurelet@let@token@xspace@dash@ii
def@xspace@dash@ii%
ifx@let@token-%
else
unskip
fi
-%
makeatother
foo - why no space here?
foo -- why no space here?
enddocument
There David shows how to use @xspace@hook
to check if the next two characters are --
and, if not, remove the space inserted by xspace
. This can be expanded to check more characters, but is it worth it?
My 2 cents: I used xspace
for a few months, when I was writing my bachelor's thesis. It helps you not forget to type foo
or foo
when you are in the habit of forgetting it. But with time, you need different things (as you did here), and it gets more time consuming than it should. My opinion (and David's, apparently :P).
1 The exception list, by default, contains:
,.'/?;:!~-) /bgroupegroup@sptokenspace@xobeyspfootnotefootnotemark
% ↑ Here it is :)
edited 5 mins ago
answered 40 mins ago


Phelype Oleinik
17.4k43770
17.4k43770
all true but you could also make clear that xspace never detects the space afterfoo
so if you do thisfoo--BAR
would also makeFOO --BAR
with a space being inserted by xspace.
– David Carlisle
34 mins ago
But removing-
from the exception list messes up using a single-
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)
– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwingnewcommand
away as well, and usingdeffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, butfoo/--
andfoo/ --
both do what you would expect.
– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
 |Â
show 1 more comment
all true but you could also make clear that xspace never detects the space afterfoo
so if you do thisfoo--BAR
would also makeFOO --BAR
with a space being inserted by xspace.
– David Carlisle
34 mins ago
But removing-
from the exception list messes up using a single-
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)
– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwingnewcommand
away as well, and usingdeffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, butfoo/--
andfoo/ --
both do what you would expect.
– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
all true but you could also make clear that xspace never detects the space after
foo
so if you do this foo--BAR
would also make FOO --BAR
with a space being inserted by xspace.– David Carlisle
34 mins ago
all true but you could also make clear that xspace never detects the space after
foo
so if you do this foo--BAR
would also make FOO --BAR
with a space being inserted by xspace.– David Carlisle
34 mins ago
But removing
-
from the exception list messes up using a single -
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)– alephzero
31 mins ago
But removing
-
from the exception list messes up using a single -
as a hyphen character. Oh well, I suppose I can't have everything working the way I want it ;)– alephzero
31 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
@alephzero my recommendation would be to not use xspace at all tex.stackexchange.com/questions/86565/drawbacks-of-xspace/…
– David Carlisle
29 mins ago
OK, I'll consider throwing
newcommand
away as well, and using deffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, but foo/--
and foo/ --
both do what you would expect.– alephzero
19 mins ago
OK, I'll consider throwing
newcommand
away as well, and using deffoo/FOO
with a guard character to prevent accidents. (foo --
is now invalid, but foo/--
and foo/ --
both do what you would expect.– alephzero
19 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
@DavidCarlisle Done. Slightly wordier version :)
– Phelype Oleinik
4 mins ago
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f453608%2fwhy-doesnt-xspace-always-insert-a-following-space-when-that-is-expected%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
Note the question is misworded,
xspace
never detects the space, the space is not available to tex macros at all. so it detectsB
in the first case and-
in the second. forB
it guesses a space is needed so adds one, for-
it guesses it is a hyphenated word, so does not. The inputfoo --
is identical to the inputfoo--
after tex parsing, neither generates a space token.– David Carlisle
35 mins ago
@DavidCarlisle Question tItle edited!
– alephzero
25 mins ago
see also tex.stackexchange.com/questions/180686/…
– David Carlisle
24 mins ago