5

Let's take a trivial case: getting the head and tail of a token list and reversing the order:

\documentclass{article}

\usepackage{xparse}
\usepackage{expl3}

\ExplSyntaxOn

\cs_set:Npn \cs_split:n #1 {
  \tl_set:Nn \l_tmpa_tl {\tl_head:n {#1}}
  \tl_set:Nn \l_tmpb_tl {\tl_tail:n {#1}}
  \tl_use:N \l_tmpb_tl
  \tl_use:N \l_tmpa_tl
}


\NewDocumentCommand\Split { m } { \cs_split:n #1 }

\ExplSyntaxOff

\begin{document}
\Split{wXYZ}
\end{document}

I would expect the output to be "XYZw" but in fact, it's the same as input, "wXYZ".

1
  • 2
    Could you make the question title a little less "boy cries wolf" ? It's not clear here why this example means TL commands don't work in general. Commented Jan 29, 2018 at 23:50

2 Answers 2

5

You forgot a pair of braces around the argument of \cs_split:n in the definition of \Split:

\documentclass{article}

\usepackage{xparse}
\usepackage{expl3}

\ExplSyntaxOn

\cs_set:Npn \cs_split:n #1 {
  \tl_set:Nn \l_tmpa_tl {\tl_head:n {#1}}
  \tl_set:Nn \l_tmpb_tl {\tl_tail:n {#1}}
  \tl_use:N \l_tmpb_tl
  \tl_use:N \l_tmpa_tl
}


\NewDocumentCommand\Split { m } { \cs_split:n { #1 } }

\ExplSyntaxOff

\begin{document}
\Split{wXYZ}
\end{document}
1
  • 3
    Plus he might want to actually use \tl_set:Nx to save the head and tail respectively into the token lists. With the current :Nn versions he is leaving the evaluation of head and tail commands to the point of use where the token lists are used.
    – Manuel
    Commented Jan 29, 2018 at 22:31
7

You're calling \cs_split:n wXYZ and the argument to the function is just w. Remember to always brace n-type arguments.

On the other hand, you're using unnecessary tools:

\documentclass{article}

\usepackage{expl3} % actually not needed, it's loaded by xparse
\usepackage{xparse}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\Split}{m}
 {
  \kevin_split:n { #1 }
 }

\cs_new:Npn \kevin_split:n #1
 {
  \tl_tail:n { #1 }
  \tl_head:n { #1 }
 }

\ExplSyntaxOff

\begin{document}

\Split{wXYZ}

\end{document}

There's no need to use token list variables. Besides, you should use \cs_new:Npn rather than \cs_set:Npn (that doesn't check for definedness) and not the cs prefix, which is reserved by the kernel.

1
  • I should have made the syntax corrections in my answer. Thank you for adding those!
    – Skillmon
    Commented Jan 29, 2018 at 23:47

You must log in to answer this question.

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