0

Suppose we have 3 variables \l__ta_seq, \l__tb_seq, and \l__tc_seq in some LaTeX3 recursive process and we want to ship their contents:

        \seq_set_eq:NN \l__ta_seq \l__tb_seq
        \seq_set_eq:NN \l__tb_seq \l__tc_seq

However, if I try to do this inside an align* environment, things get messed up. Here is an example

  \seq_set_from_clist:Nn \l__ta_seq {1,2,3}
  \seq_set_from_clist:Nn \l__tb_seq {a,b,c}
  \seq_set_from_clist:Nn \l__tb_seq {x,y}
  \begin{align*}
    A&=\seq_use:Nn \l__ta_seq {+}\\
    B&=\seq_use:Nn \l__tb_seq {+}\\
    C&=\seq_use:Nn \l__tc_seq {+}\\
    \seq_set_eq:NN \l__ta_seq \l__tb_seq
    \seq_set_eq:NN \l__tb_seq \l__tc_seq
    A&=\seq_use:Nn \l__ta_seq {+}\\
    B&=\seq_use:Nn \l__tb_seq {+}\\
    C&=\seq_use:Nn \l__tc_seq {+}\\
  \end{align*}

You can find that the interchange doesn't happen.

What cause this and how to solve it?

Update: The goal is to put a recursion inside an align* environment. Here is a (not) working example:


\documentclass{article}

  \seq_set_from_clist:Nn \l__ta_seq {1,2,3}
  \seq_set_from_clist:Nn \l__tb_seq {9,8,7}
  \seq_clear_new:N \l__tc_seq 

\usepackage{amsmath}
  \cs_new_nopar:Npn \fun_trim:N #1 {
    \bool_do_while:nn {
      \int_compare_p:n {\seq_count:N #1 > 1}
      &&
      \int_compare_p:n {\seq_item:Nn #1 {1} == 0}
    } {
      \seq_pop_left:NN #1 \l_tmpa_tl
    }
  }
  \cs_new_nopar:Npn \fun_seq:NNN #1#2#3
  {
    \seq_clear_new:N #1
    \int_step_inline:nnnn {1} {1} {\int_min:nn {\seq_count:N #2} {\seq_count:N #3}} {
      \seq_put_left:Nx #1 {\int_eval:n {\seq_item:Nn #2 {1} / \seq_item:Nn #3 {1} + 0}}
    }
    \fun_trim:N #1
  }
  %Some function produce \l__tc_seq from \l__ta_seq and \l__tb_seq, 

\begin{document}

  \begin{align*}
      A&=\seq_use:Nn \l__ta_seq {+}\\
      B&=\seq_use:Nn \l__tb_seq {+}\\
      C&=\seq_use:Nn \l__tc_seq {+}\\%Before the process
    \int_until_do:nn {
        \seq_count:N \l__tb_seq <= 1%Yes, I need it to be one or zero.
    }
    {
        \fun_seq:NNN \l__tc_seq \l__ta_seq \l__tb_seq
        \seq_set_eq:NN \l__ta_seq \l__tb_seq
        \seq_set_eq:NN \l__tb_seq \l__tc_seq
        A&=\seq_use:Nn \l__ta_seq {+}\\
        B&=\seq_use:Nn \l__tb_seq {+}\\
        C&=\seq_use:Nn \l__tc_seq {+}\\
    }
  \end{align*}
\end{document}

You can see that the issue is I need the variables being local so I can do the recursion.

Update 2 Apologize for adding off-topic update. I realize that there is still an issue in the problem I meet. I know now that I need to store the result inside a token list. But in the actual situation I meet, I need to apply a complicated function to the sequence \l__ta_seq rather than use it directly. Here is a M(no)WE:

\ExplSyntaxOn
  \seq_set_from_clist:Nn \l__ta_seq {1,2,3}
  \cs_new_nopar:Npn \fun_something:N #1 {
    \seq_map_inline:Nn #1 {##1} 
  }%In the actual situation, this should be a complicated function generating some math symbols (saying, a polynomial) from the sequence #1

  \tl_clear:N \l_results_tl
  \tl_put_right:Nx \l_results_tl {\fun_something:N \l__ta_seq}
  %Let's ignore the recursion to make things simple.
\ExplSyntaxOff

So the issue is, the above code will cause errors Use of \??? doesn't match its definition. and ! LaTeX Error: A sequence was misused. But I have no idea what's wrong.

By the way, although modifying the function \fun_something:N may be a reasonable solution, but the actual function is too complicated and I want to avoid modifying it.

4
  • 2
    Alignment cells form groups; you need global assignments (and global variables).
    – egreg
    Feb 12, 2023 at 8:56
  • 1
    @egreg Thanks! But I need to use this construction in a recursion and hence the variables cannot be global. Any way to avoid this?
    – Gau-Syu
    Feb 12, 2023 at 9:29
  • Do the manipulation outside of the align* environment and save the result inside a token list. Then use that token list inside the align environment
    – Udi Fogiel
    Feb 12, 2023 at 15:29
  • We need a M(not)WE...
    – projetmbc
    Feb 12, 2023 at 16:01

1 Answer 1

3

Your question is not fully clear, as you you don't explicitly say your final goal. Does this solve your problem?

\documentclass{article}

\usepackage{amsmath}

\begin{document}
    
    \ExplSyntaxOn
    
    \seq_set_from_clist:Nn \l__ta_seq {1,2,3}
    \seq_set_from_clist:Nn \l__tb_seq {a,b,c}
    \seq_set_from_clist:Nn \l__tc_seq {x,y} % you had a typo here...
    \tl_set:Nn \l_tempa_tl { \begin{align*} }
    \tl_put_right:Nx \l_tempa_tl
      {
        A&=\seq_use:Nn \l__ta_seq {+}\\
        B&=\seq_use:Nn \l__tb_seq {+}\\
        C&=\seq_use:Nn \l__tc_seq {+}\\
      }
    \seq_set_eq:NN \l__ta_seq \l__tb_seq
    \seq_set_eq:NN \l__tb_seq \l__tc_seq
    \tl_put_right:Nx \l_tempa_tl
    {
        A&=\seq_use:Nn \l__ta_seq {+}\\
        B&=\seq_use:Nn \l__tb_seq {+}\\
        C&=\seq_use:Nn \l__tc_seq {+}\\
      }
    \tl_put_right:Nn \l_tempa_tl { \end{align*} }
    
        \l_tempa_tl
    
\end{document}
3
  • Thanks! This does solve my problem! By putting everything into a token list, the grouping caused by alignment & is avoided.
    – Gau-Syu
    Feb 12, 2023 at 19:41
  • Actually, this still does not solve my goal, although it does work for the exam in question. May I ask in addition how to solve the following issue? If instead of \seq_use:Nn \l__ta_seq {+} (which works well), I use some function \fun_bla:N \l__ta_seq, it seems this will cause errors. Here the function \fun_bla:N` eats the sequence \l__ta_seq and prints something based on the information given by \l__ta_seq (a vector, or a polynomial, for example).
    – Gau-Syu
    Feb 13, 2023 at 4:55
  • @Gau-Syu, that update deserve a new question. I can say in short that \seq_map_inline:Nn is a protected thus no expanding with the x argument specifier. Please as a new question with a full description of the problem. If you don't mind to post your complicated function and the whole code related to it, it would be the best way to solve your problem.
    – Udi Fogiel
    Feb 13, 2023 at 10:37

You must log in to answer this question.

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