Expl3 – How to Add Explicit Space After Macro in LaTeX3

expl3

Consider this MWE:

\documentclass{scrartcl}

\usepackage{expl3}

\ExplSyntaxOn

\str_new:N \my_str
\str_set:Nn \my_str {Test}

\cs_new:Nn \my_printer:x { \sys_shell_now:n { echo ~ #1 ~ > ~ output } }

% \my_printer:n {This ~ is ~ a ~ test}

\my_printer:n {\my_str ~ another ~ word}

\begin{document}
    MWE
\end{document}

\ExplSyntaxOff

Even though I always added an explicit (non-ignored) space between the words for the argument of \my_printer:n, the second case writes Got 'Testanother word' rather than Got 'Test another word' to the output file. In the example without the macro in the argument, everything works as expected.

If I encountered macros eating spaces in regular LaTeX, I would simply add {} to the end of the macro, ensuring it does not eat the following space as its argument. However, when attempting that within expl3 the braces are actually printed as plain characters.

I also tried explicitly using \use_str:N to get the string's value, but the result is the same.

What is the necessary trick to prevent \my_str from eating up the following spaces?

EDIT:

It turned out that the regular \my_str{} approach works fine for my MWE, but it doesn't work in non-typesetting contexts as e.g. shell-escape (in which case the behavior is as I described it).

EDIT2:

Adapted MWE to actually reflect my problem

Best Answer

The situation is exactly the same as \foo word in classic TeX.

You can use \foo{} word or \foo\space word (or \foo\c_space_tl word)

Note in the first form the {} are not absorbed, they make two additional tokens. An empty group has no effect if typetting but these tokens would show up in a \typeout or shell escape, or other non-typesetting contexts.

Related Question