1

I have heard more than once that \xspace should not be used and does not work reliable.

To find a better solution I have found xpeek and tried to use the example give in the documentation.

\documentclass{scrlttr2}

\usepackage{fmtcount}

\usepackage{xpeek}

\ExplSyntaxOn

\tl_const:Nn \c_xsp_exceptions_tl { ,;:.!? } \NewDocumentCommand \xsp {} { \xpeek_collect_do:nn \c_empty_tl { \xpeek_if_in:NNTF \c_xsp_exceptions_tl \l_peek_token { } { ~ } } }

\NewDocumentCommand{\numbertext}{m} { \int_compare:nNnTF {#1} > {12} { % true #1\xsp } { % false \numberstringnum{#1}\xsp } }

\NewDocumentCommand{\numbertextnoxsp}{m} { \int_compare:nNnTF {#1} > {12} { % true #1 } { % false \numberstringnum{#1} } }

\NewDocumentCommand{\numbertextnoxspunits}{m} { \numbertextnoxsp{#1} units }

\ExplSyntaxOff

\begin{document}

numberstring:

\numberstringnum{13} units. \numberstringnum{13}.

\bigskip
numbertext:

\numbertext{12}units. \numbertext{12}.

\numbertext{12} units. \numbertext{12}.

\numbertext{13}units. \numbertext{13}.

\numbertext{13} units. \numbertext{13}.

\bigskip
numbertextnoxsp:

\numbertextnoxsp{12}units. \numbertextnoxsp{12}.

\numbertextnoxsp{12} units. \numbertextnoxsp{12}.

\numbertextnoxsp{13}units. \numbertextnoxsp{13}.

\numbertextnoxsp{13} units. \numbertextnoxsp{13}.

\bigskip
numbertextnoxspunits:

\numbertextnoxspunits{12}. \numbertextnoxspunits{12} it is.

\numbertextnoxspunits{12}. \numbertextnoxspunits{12} it is.

\numbertextnoxspunits{13}. \numbertextnoxspunits{13} it is.

\numbertextnoxspunits{13}. \numbertextnoxspunits{13} it is.

\end{document}

However, this does also not seem to work since the last two lines show different results and I would not expect them to...

Output is:

example3

mrCarnivore
  • 1,505
  • 1
    What would be the reason to use \xspace for a command with argument? – egreg May 08 '23 at 16:00
  • 1
    I would want to have a space after the text if there is normal text following and no space otherwise. Like there should of course be no space after my command if the next symbol is . or ,... Like with the result of \numberstringnum directly it should work the same way with #1... – mrCarnivore May 08 '23 at 16:05
  • 2
    It's not that xspace works unreliably, it's the concept of using heuristics for adding a space after a macro that is not recommended, regardless of implementation – Phelype Oleinik May 08 '23 at 16:05
  • @mrCarnivore You do get a space or not without resorting to \xspace. Did you bother to try? – egreg May 08 '23 at 16:36
  • Maybe my MWE was too short. I have extended to show the problems: if you use the function in expl3 part there definitely is no space without \xspace or similar... – mrCarnivore May 08 '23 at 16:43
  • @DavidCarlisle: How would I solve that problem otherwise? What are the problems with \xspace in expl3? Will it just get lost? – mrCarnivore May 08 '23 at 16:48
  • I explain the issues here https://tex.stackexchange.com/questions/86565/drawbacks-of-xspace/86620#86620 but the issue is not with the implementation it is that it is better to use correct markup than try to fix bad markup. But your command has arguments so there is no problem to solve and nothing to do – David Carlisle May 08 '23 at 16:52
  • But it my command \numbertextnoxspunits does not work because there are no space added. That is the whole point of the question... – mrCarnivore May 08 '23 at 16:55
  • So your suggestion is: just use \xspace in my expl3 code (in my case in \\numbertextnoxsp to make \numbertextnoxspunits work correctly? I mainly do not understand why everybody is going crazy for me using \xspace all the time and not see the problem... – mrCarnivore May 08 '23 at 16:59
  • Apparently it is since the code in \numbertextnoxspunits does not result in any spaces before the word units... – mrCarnivore May 08 '23 at 17:02
  • but that is like saying it is a problem that there is no space in helloworld, if you want a space after \numbertextnoxspunits{13} use a space. you can not simply use a space after a command with no arguments as spaces are dropped, that is what xspace tries to solve – David Carlisle May 08 '23 at 17:05
  • But when would there be a need / user for \xspace following that logic? – mrCarnivore May 08 '23 at 17:09
  • xspace saves you using \ after commands like \LaTeX that have no arguments. Commands with arguments have no problem to solve – David Carlisle May 08 '23 at 17:12
  • I will extensively test and revisit my current understanding to better grasp if and when \xspace might be necessary. Thank you for your patience and input! – mrCarnivore May 08 '23 at 17:41

1 Answers1

5

There is no reason whatsoever for using \xspace or variant thereof at the end of a command with arguments.

There is little reason for \xspace after commands without argument either, but that's a different game.

\documentclass{article}

\usepackage{fmtcount}

\ExplSyntaxOn \NewDocumentCommand{\numbertext}{m} { \int_compare:nNnTF {#1} > {12} { % true #1 } { % false \numberstringnum{#1} } } \ExplSyntaxOff

\begin{document}

numberstring:

\numberstringnum{13} units. \numberstringnum{13}.

\bigskip numbertext:

\numbertext{12}units. \numbertext{12}.

\numbertext{12} units. \numbertext{12}.

\numbertext{13}units. \numbertext{13}.

\numbertext{13} units. \numbertext{13}.

\end{document}

enter image description here

Is there something unexpected?

Well you added

\NewDocumentCommand{\numbertextnoxspunits}{m}
{
    \numbertextnoxsp{#1} units
}

and complain that there's no space in output after the number. Well there is NO space in the definition, because inside \ExplSyntaxOn spaces are ignored. You want

\NewDocumentCommand{\numbertextnoxspunits}{m}
{
    \numbertextnoxsp{#1}~units
}

because in that context ~ stands for a space token.

egreg
  • 1,121,712
  • Thank you for the answer. I will extensively test and revisit my current understanding to better grasp if and when \xspace might be necessary. – mrCarnivore May 08 '23 at 17:42