2

I'm trying to adapt this egreg's answer but I'm facing a very strange phenomenon.

As shown by the following MCE, the similar lines that are supposed to be written to the .aux file:

\int_gset:Nn \g_integer_int {#1}
\seq_put_right:Nn \g_integers_seq {#1}

result on the expected one for the 1st one but, in the 2nd one, \g_integers_seq is changed in \s__seq!

Here is the .aux file's content:

\relax 
\ExplSyntaxOn
\int_gset:Nn \g_integer_int {1}
\seq_put_right:Nn \s__seq {1}
\ExplSyntaxOff
\ExplSyntaxOn
\int_gset:Nn \g_integer_int {2}
\seq_put_right:Nn \s__seq {2}
\ExplSyntaxOff

Do you see what's going on?

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g_integers_seq
\int_new:N \g_integer_int

\NewDocumentCommand{\NewInteger}{m}
{
  \iow_now:cx { @auxout }
   {
    \token_to_str:N \ExplSyntaxOn
    ^^J
    \int_gset:Nn \g_integer_int {#1}
    ^^J
    \seq_put_right:Nn \g_integers_seq {#1}
    ^^J
    \token_to_str:N \ExplSyntaxOff
  }
}

\ExplSyntaxOff
\begin{document}
\NewInteger{1}
\NewInteger{2}
\end{document}
  • Because \g_integers_seq is not a protected command. – egreg May 8 '19 at 21:33
2

\g_integers_seq is not a protected command and doesn't survive x expansion.

You simply do \iow_now:cn:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g_integers_seq
\int_new:N \g_integer_int

\NewDocumentCommand{\NewInteger}{m}
{
  \iow_now:cn { @auxout }
   {
    \ExplSyntaxOn
    ^^J
    \int_gset:Nn \g_integer_int {#1}
    ^^J
    \seq_put_right:Nn \g_integers_seq {#1}
    ^^J
    \ExplSyntaxOff
  }
}

\ExplSyntaxOff
\begin{document}
\NewInteger{1}
\NewInteger{2}
\end{document}

The .aux file will be

\relax 
\ExplSyntaxOn 
\int_gset:Nn \g_integer_int {1}
\seq_put_right:Nn \g_integers_seq {1}
\ExplSyntaxOff 
\ExplSyntaxOn 
\int_gset:Nn \g_integer_int {2}
\seq_put_right:Nn \g_integers_seq {2}
\ExplSyntaxOff 

In the code you quote, \iow_now:cx is necessary for doing the computation through \int_eval:n.

If you need to do computations, you need to protect \g_integers_seq from expansion with \exp_not:N or \token_to_str:N.

| improve this answer | |
  • OK but why \g_integer_int does survive x expansion? And where \s__seq does come from? – Denis Bitouzé May 8 '19 at 21:45
  • 1
    @DenisBitouzé Because it's an unexpandable token defined with \countdef. – egreg May 8 '19 at 21:48
  • Do you mean any \g_..._intis an unexpandable token defined with \countdef? – Denis Bitouzé May 8 '19 at 21:50
  • 1
    @DenisBitouzé Yes. Currently the LaTeX2e \newcount macro is used, for compatibility with the allocation mechanism. – egreg May 8 '19 at 21:53
  • 1
    @DenisBitouzé It's the internal implementation of sequence variables. – egreg May 9 '19 at 6:16

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.