0

I am trying to create a nested sequence where each element is a sequence but for some reason it does not compile and exists with an emergency stop.

MWE:

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn
\cs_generate_variant:Nn \seq_put_right:Nn {Ne}
\cs_generate_variant:Nn \seq_put_right:Nn {NV}

\seq_new:N \l__outer_seq
\seq_new:N \l__inner_seq
\NewDocumentCommand\add{m m m}
{
    \seq_clear:N \l__inner_seq
    \seq_put_right:Ne \l__inner_seq {##1}
    \seq_put_right:Ne \l__inner_seq {##2}
    \seq_put_right:Ne \l__inner_seq {##3}
    \seq_put_right:NV \l__outer_seq \l__inner_seq
}
\ExplSyntaxOff

\begin{document}
\section{MWE}
\add{1}{2}{3}
\end{document}

After the call of add{1}{2}{3} the outer sequence contains one element which is a (inner) sequence containing the element 1,2 and 3

Visual: [[1, 2, 3]] - the brackets represent a sequence

9
  • 1
    So you want to append to \l__inner_seq the items in \l__outer_seq? Please try and explain what \l__inner_seq is expected to contain at the end of the process.
    – egreg
    Oct 12, 2021 at 14:38
  • @egreg I specified the MWE with more information. I hope this is helpfull,.
    – Nyanyan
    Oct 12, 2021 at 15:05
  • 1
    :NV means it takes two arguments so \seq_put_right:NV \l__outer_seq is missing something. Oct 12, 2021 at 15:06
  • Sorry, but sequences cannot contain other sequences. They're not something like Perl or Python arrays. Maybe you could explain what's the problem you want to solve.
    – egreg
    Oct 12, 2021 at 15:06
  • @egreg but they could contain a variable that holds a sequence which is perhaps all the OP needs Oct 12, 2021 at 15:08

1 Answer 1

1

Your outer sequence can hold a variable holding an inner sequence, so:

The sequence \l__outer_seq contains the items (without outer braces):
>  {\l__inner_seq }.
<recently read> }
                 
l.26 \seq_show:N\l__outer_seq
                             
? 
The sequence \l__inner_seq contains the items (without outer braces):
>  {1}
>  {2}
>  {3}.
<recently read> }
                 
l.28 \seq_map_function:NN \l__outer_seq\seq_show:N
                                                  
? 

This shows the outer sequence then maps over that showing the inner sequence is 1,2,3

\documentclass{article}

\usepackage{expl3}

\ExplSyntaxOn
\cs_generate_variant:Nn \seq_put_right:Nn {Ne}
\cs_generate_variant:Nn \seq_put_right:Nn {NV}

\seq_new:N \l__outer_seq
\seq_new:N \l__inner_seq
\NewDocumentCommand\add{m m m}
{
    \seq_clear:N \l__inner_seq
    \seq_put_right:Ne \l__inner_seq {#1}
    \seq_put_right:Ne \l__inner_seq {#2}
    \seq_put_right:Ne \l__inner_seq {#3}
    \seq_put_right:Nn \l__outer_seq \l__inner_seq
}
\ExplSyntaxOff

\begin{document}
\section{MWE}
\add{1}{2}{3}

\ExplSyntaxOn
\seq_show:N\l__outer_seq

\seq_map_function:NN \l__outer_seq\seq_show:N
\ExplSyntaxOff
\end{document}
6
  • Thanks a lot! Your post almost solved all my questions. The command call e.g. \add{7}{8}{\textbf{9}} seems not to work if an argument contains a macro. Do you perhaps know a solution to that?
    – Nyanyan
    Oct 12, 2021 at 16:33
  • @Nyanyan I copied you and used :Ne so they are expanded perhaps you want :Nn so you just add the tokens as supplied Oct 12, 2021 at 16:35
  • :Nn does not work. Tried to test it all day but no success :(
    – Nyanyan
    Oct 12, 2021 at 16:52
  • :Nn will add the argument not its expansion, If something doesnt work make an example and ask a new question Oct 12, 2021 at 17:07
  • You're storing a pointer to the current value of \l__inner_seq, not the value… See tex.stackexchange.com/q/618725/4427
    – egreg
    Oct 12, 2021 at 17:17

You must log in to answer this question.

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