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

14
  • 1
    What would be the reason to use \xspace for a command with argument?
    – egreg
    May 8, 2023 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... May 8, 2023 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 May 8, 2023 at 16:05
  • @mrCarnivore You do get a space or not without resorting to \xspace. Did you bother to try?
    – egreg
    May 8, 2023 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... May 8, 2023 at 16:43

1 Answer 1

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.

1
  • Thank you for the answer. I will extensively test and revisit my current understanding to better grasp if and when \xspace might be necessary. May 8, 2023 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .