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".

  • 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. – Will Robertson Jan 29 '18 at 23:50
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}
| improve this answer | |
  • 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 Jan 29 '18 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.

| improve this answer | |

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.