# Latex3 Token-List commands do not work as expected

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

• 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

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_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}

• 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

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 }
}

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

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