2

I have some code that works but stops working when I try to refactor it into a function. Here is a MWE:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \l_shepi_sub_seq:nnn
\cs_new_protected:Nn \shepi_seq:nnn {
  \seq_set_split:Nnn \l_shepi_sub_seq {,} {#3}
  \seq_set_map:NNn \l_shepi_sub_seq \l_shepi_sub_seq {\exp_not:n {#2}}
  \seq_use:Nn \l_shepi_sub_seq {#1}
}

\NewDocumentCommand \works { m } {
  \seq_set_split:Nnn \l_shepi_sub_seq {,} {#1}
  \seq_set_map:NNn \l_shepi_sub_seq \l_shepi_sub_seq {\exp_not:n {l}}
  \[\begin{array}{\seq_use:Nn \l_shepi_sub_seq {}}
  \end{array}\]
}

\NewDocumentCommand \fails { m } {
  \[\begin{array}{\shepi_seq:nnn {} {l} {#1}}
  \end{array}\]
}

\ExplSyntaxOff

\begin{document}

\works{A, B, C}

\ExplSyntaxOn
\shepi_seq:nnn{}{l}{A, B, C}
\ExplSyntaxOff

%\fails{A, B, C}

\end{document}

Uncommenting the \fails call interrupts the compilation with the following error:

! Undefined control sequence.
<argument> \LaTeX3 error: 
                           A sequence was misused.
l.55     \fails{A, B, C}

As my \shepi_seq:nnn function is used by other parts of my codebase, I'd prefer to leave it as it is, but if changes are required, so be it.

Can someone explain what I did wrong?

Side question: Is it good practice to call \seq_set_map:NNn like I did? (The two first parameters denote the same sequence).

2

The argument to \begin{array} should be fully expandable to a correct array preamble, but \shepi_seq:nnn isn't expandable.

Also, the argument to \begin{array} will not be expanded anyway if the array package is loaded, so you want a different approach.

You're apparently counting the number of items, so do it. ;-)

\documentclass{article}
\usepackage{xparse}
\usepackage{array} % just in case

\ExplSyntaxOn

\seq_new:N \l_shepi_sub_seq

\NewDocumentCommand{\fabianarray}{m}
 {
  \[
  \seq_set_split:Nnn \l_shepi_sub_seq { , } { #1 }
  \begin{array}{ *{ \clist_count:n { #1 } } { l } }
  \seq_use:Nn \l_shepi_sub_seq { & }
  \end{array}
  \]
 }

\ExplSyntaxOff

\begin{document}

\fabianarray{A, B, C}

\end{document}

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.