Is there a version of tl_map that does not ignore spaces that are part of a token list?

\tl_new:N \l_token_tl
\tl_set:Nn \l_token_tl { a~b~c }
%\tl_show:N \l_token_tl
\tl_map_inline:Nn \l_token_tl { (#1) }

This example does only three iterations, ignoring all the spaces.

  • 2
    There is a currently-internal approach for mapping allowing for spaces, but it's very rarely been required (hence being internal). Perhaps you can explain what you actually need to achieve here?
    – Joseph Wright
    Sep 13, 2014 at 19:26
  • 1
    I would split it up into a sequence delimited by ~; that's usually what I mean when I need this sort of thing. Sep 13, 2014 at 19:30
  • @Joseph I have a syntax parser (mhchem package) that I would like to convert to LaTeX3. The syntax contains spaces as important markup element. I am looking for a way to get this done, but always hit a wall with LaTeX3's space handling. I.e. I do not necessarily need this tl_map version if, for instance, the peek approach would work.
    – mh543
    Sep 13, 2014 at 20:01
  • @mh543 Wouldn't \tl_replace_all:Nnn be enough? (I don't know anything about mhchem so probably I'm wrong.)
    – Manuel
    Sep 13, 2014 at 20:19
  • @mh543 When I had similar problems, I used either the method suggested by Manuel, or split the input in a sequence at spaces, so you still know where the spaces originally were.
    – egreg
    Sep 13, 2014 at 21:12

2 Answers 2


You have two strategies available.

Strategy 1: replace spaces with some function that you can redefine depending on the current state

\tl_new:N \l_mhchem_input_tl
\tl_set:Nn \l_mhchem_input_tl { a ~ b ~ c } % this would come from a macro argument
\tl_replace_all:Nnn \l_mhchem_input_tl { ~ } { \__mhchem_space_do: } 
\tl_map_inline:Nn \l_mhchem_input_tl { whatever with #1 }

Strategy 2: split the token list into a sequence that you can subsequently use.

\seq_new:N \l_mhchem_split_input_seq
\seq_set_split:Nnn \l_mhchem_split_input_seq { ~ } { a ~ b ~ c } % this would come from a macro argument
\seq_use:Nn \l_mhchem_split_input_seq { do something in place of spaces }

With \seq_set_map:NNn you can set another sequence adding something around the values stored in \l_mhchem_split_input_seq and use this new sequence.


I prepared the mentioned \seq approach for you. I append a space to every item of the sequence and remove the resulting trailing space using \tex_unksip:D.

\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }

\tl_new:N \l_token_tl
\tl_set:Nn \l_token_tl { a~b~c }

\seq_new:N \l_token_seq
\seq_set_split:NnV \l_token_seq { ~ } \l_token_tl
\seq_map_inline:Nn \l_token_seq { (#1)~ }
\tex_unskip:D % remove trailing space


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.