6

Regularly, I much appreciate that within latex3/expl3 whitespace is ignored. However, there are situations in which I would prefer to temporarily disable this behavior.

One example of this is the definition of new messages, e.g.

\msg_new:nnn{module}{msg}{My example message}

When displaying this message, it will be shown with all words concatenated as the spaces separating them have been ignored.

MWE:

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn

\msg_new:nnn{module}{msg}{My example message}

\msg_warning:nn{module}{msg}

\ExplSyntaxOff

\begin{document}
    MWE
\end{document}

which will produce a warning "Myexamplemessage".

So the question is: How can I temporarily disable whitespace gobbling, so I don't have to escape all whitespaces individually?

6
  • 1
    \catcode32=10 but you don't have to "escape" space you can use ~ so is only one character to type, so no more than typing a space Oct 19, 2022 at 8:54
  • You will get the answer "use ~". But your question is more interesting if you want to create a macro with the parameter separated by space, something like \def\macro #1 {...} or \def\macro #1 #2 {...} or \def\macro #1; {...} (semicolon space is separator) etc. You cannot use ~ in the expl3 environment in these cases.
    – wipet
    Oct 19, 2022 at 8:56
  • @wipet Yes you can: ~ is a space (catcode-10) inside \ExplSyntaxOn.
    – Joseph Wright
    Oct 19, 2022 at 8:57
  • 2
    the fact that byte 32 looks more like a space than byte 126 is an illusion:-) Oct 19, 2022 at 9:00
  • 1
    Related issue: github.com/latex3/latex3/issues/942. Oct 25, 2022 at 18:15

1 Answer 1

7

You can change the treatment of whitespace with \char_set_catcode_space:n { `\ }. The reason this is not done 'automatically' here is that this relies on having no nesting, etc. (i.e. that the input has not already been tokenized): overall, the team have found that having consistency is preferable despite the slight increase in typing requirements.


To reset, assuming you are not using a group, use \char_set_catcode_ignore:n { `\ }.

5
  • Okay, so this allows me to restore the "keep-spaces" behavior. Is this effect local to the current scope? So if I were to define an environment inside which I'd execute this, would its effect be gone after exiting the environment again? EDIT: Just tested and the effect does indeed seem to be local-only
    – Raven
    Oct 19, 2022 at 8:59
  • And for completeness sake: Say I would want to revert the effect of this within the same scope. What cat-code would I have to set the space back to?
    – Raven
    Oct 19, 2022 at 8:59
  • 1
    @Raven 9 (\char_set_catcode_ignore:n) Oct 19, 2022 at 9:07
  • Does the l3token module also define constants for the different default catcodes? E.g. something like \c_char_catcode_ignore_int?
    – Raven
    Oct 19, 2022 at 9:14
  • 1
    @Raven Not at present: this is something we likely could think about, but in many ways I'd rather move toward catcode-tables and avoid most programmers needing to set individual catcodes
    – Joseph Wright
    Oct 19, 2022 at 9:15

You must log in to answer this question.

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