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:


\xspacefor a command with argument? – egreg May 08 '23 at 16:00.or,... Like with the result of\numberstringnumdirectly it should work the same way with#1... – mrCarnivore May 08 '23 at 16:05\xspace. Did you bother to try? – egreg May 08 '23 at 16:36expl3part there definitely is no space without \xspace or similar... – mrCarnivore May 08 '23 at 16:43\numbertextnoxspunitsdoes not work because there are no space added. That is the whole point of the question... – mrCarnivore May 08 '23 at 16:55\xspacein my expl3 code (in my case in\\numbertextnoxspto make\numbertextnoxspunitswork correctly? I mainly do not understand why everybody is going crazy for me using\xspaceall the time and not see the problem... – mrCarnivore May 08 '23 at 16:59\numbertextnoxspunitsdoes not result in any spaces before the wordunits... – mrCarnivore May 08 '23 at 17:02helloworld, 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\after commands like\LaTeXthat have no arguments. Commands with arguments have no problem to solve – David Carlisle May 08 '23 at 17:12