# Inline Mapping Bug?

The enclosed document creates a sequence variable and then uses inline mapping to attach a sequence number to each item. The inline map then outputs the result and places it into a token list variable for future use. That list variable is then printed.

The sequence numbers are correct for the immediate output but fail to be duplicated in the list variable. Is this a bug in the inline map function or am I failing to understand my code?

\documentclass[12pt]{article}
\RequirePackage{expl3}
\RequirePackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_my_seq
\int_new:N \l_my_count
\tl_new:N  \l_my_out
\ExplSyntaxOff

\begin{document}
\noindent
\ExplSyntaxOn

\seq_put_right:Nn \l_my_seq {A}
\seq_put_right:Nn \l_my_seq {B}
\seq_put_right:Nn \l_my_seq {C}
\seq_put_right:Nn \l_my_seq {D}

\int_zero:N \l_my_count                 % Initialize counter
\tl_clear:N \l_my_out                   % Initialize output string

Sequence~Input:~
\seq_map_inline:Nn { \l_my_seq } {      % Loop through items in sequence
\int_incr:N \l_my_count             % Increment counter
\int_use:N  \l_my_count -#1~        % Output result (count-item)
\tl_put_right:Nn \l_my_out {\int_use:N \l_my_count -#1~}  % store result
}
\\
List~Output:~ \l_my_out                  % Output stored result
\ExplSyntaxOff
\end{document}


The output produced by this document is:

Sequence Input: 1-A 2-B 3-C 4-D
List Output: 4-A 4-B 4-C 4-D


Both functions \seq_map_inline:Nn and \tl_map_inline:Nn exhibit similar behavior.

There is no bug. If you add

\tl_show:N \l_my_out


just before \ExplSyntaxOff you can see

> \l_my_out=macro:
->\int_use:N \l_my_count -A \int_use:N \l_my_count -B \int_use:N \l_my_count
-C \int_use:N \l_my_count -D .


You are not storing in \l_my_out the current value, but only the instructions to print the current value. This, when you use the token list, is 4.

You can either use

\tl_put_right:No \l_my_out {\int_use:N \l_my_count -#1~}


because \int_use:N requires only one expansion step, or, with full expansion,

\tl_put_right:Nx \l_my_out {\int_use:N \l_my_count \exp_not:n {-#1~} }


where you better control what gets expanded or not. I'd prefer the latter way.

By the way, the recommended names for variables end with the variable type, so

\seq_new:N \l_my_seq
\int_new:N \l_my_int
\tl_new:N  \l_my_tl


is a better nomenclature. But the name should be made of a prefix and a description; therefore something like

\seq_new:N \l_roman_storage_seq
\int_new:N \l_roman_step_int
\tl_new:N  \l_roman_out_tl


is even better.

• I favour the x-type version: o-type expansion is a bit more conceptually tricky (as you need to know what happens on exactly one expansion). – Joseph Wright Jun 11 '13 at 6:20

You need

    \tl_put_right:No \l_my_out {\int_use:N \l_my_count -#1~}  % store result
%


Where the o causes the argument to be expanded before being added to your output list, otherwise you store a sequence of use commands which will all use the final value.

• David, Thank you for your timely and accurate response. Still in learning mode for LaTeX3... – Roman Jun 10 '13 at 20:23