Why doesn't xspace always insert a following space when that is expected?

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



enter image description here










share|improve this question



















  • 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










  • @DavidCarlisle Question tItle edited!
    – alephzero
    25 mins ago










  • see also tex.stackexchange.com/questions/180686/…
    – David Carlisle
    24 mins ago














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:



enter image description here










share|improve this question



















  • 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










  • @DavidCarlisle Question tItle edited!
    – alephzero
    25 mins ago










  • see also tex.stackexchange.com/questions/180686/…
    – David Carlisle
    24 mins ago












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:



enter image description here










share|improve this question















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:



enter image description here







xspace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • see also tex.stackexchange.com/questions/180686/…
    – David Carlisle
    24 mins ago












  • 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










  • @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










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



enter image description here





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



enter image description here




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 :)





share|improve this answer






















  • 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










  • @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











  • @DavidCarlisle Done. Slightly wordier version :)
    – Phelype Oleinik
    4 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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



enter image description here





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



enter image description here




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 :)





share|improve this answer






















  • 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










  • @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











  • @DavidCarlisle Done. Slightly wordier version :)
    – Phelype Oleinik
    4 mins ago














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



enter image description here





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



enter image description here




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 :)





share|improve this answer






















  • 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










  • @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











  • @DavidCarlisle Done. Slightly wordier version :)
    – Phelype Oleinik
    4 mins ago












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



enter image description here





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



enter image description here




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 :)





share|improve this answer














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



enter image description here





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



enter image description here




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 :)






share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • @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











  • @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










  • 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 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















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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery