4

I have created a helper macro that formats a sequence of entries (passed as a comma-separated list) by typesetting them one after another, but with a zero-based index on top of the actual entry:

\NewDocumentCommand{\image}{ m }{
    \ensuremath{
        \seq_set_from_clist:Nn \l_tmpa_seq { #1 }

        \arraycolsep=0.1em
        \def\arraystretch{0.6}
        \begin{array}[b]{ r \prg_replicate:nn{ \seq_count:N \l_tmpa_seq }{l~} l}
            &
            \int_step_inline:nnn {0}{\seq_count:N \l_tmpa_seq - 1}{ 
                \color{gray}{\scriptscriptstyle{##1}} &
            }
            \\
            {[} & \seq_use:Nnnn \l_tmpa_seq {,&}{,&}{,&} & {]}
        \end{array}
    }
}

This works as intended. Now I want to get a way to prevent the elements from being typeset in math mode, when using a different macro. My idea was to define something like this

\NewDocumentCommand{\sequence}{ m }{
    \seq_set_from_clist:Nn \l_tmpa_seq { #1 }
    \seq_map_inline:Nn \l_tmpa_seq {\textrm{##1}}
    \image{\seq_use:Nnnn \l_tmpa_seq {,}{,}{,}}
}

aka: my idea is to make \sequence{a,b} to behave as if I had written \image{\textrm{a}, \textrm{b}} (the latter already working as intended).

However, first of all \seq_map_inline doesn't transform the the sequence in-place but rather outputs each element after having processed it. Furthermore, calling the \image macro like this produces an error TeX capacity exceeded - presumably due to some kind of endless loop / recursion.

How would I have to change my code to get the following behavior for \sequence:

  1. Wrap all elements inside the passed list into \textrm{...}
  2. Pass the transformed sequence as a comma-separated list to the \image macro

Full MWE:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\image}{ m }{
    \ensuremath{
        \seq_set_from_clist:Nn \l_tmpa_seq { #1 }

        \arraycolsep=0.1em
        \def\arraystretch{0.6}
        \begin{array}[b]{ r \prg_replicate:nn{ \seq_count:N \l_tmpa_seq }{l~} l}
            &
            \int_step_inline:nnn {0}{\seq_count:N \l_tmpa_seq - 1}{ 
                \color{gray}{\scriptscriptstyle{##1}} &
            }
            \\
            {[} & \seq_use:Nnnn \l_tmpa_seq {,&}{,&}{,&} & {]}
        \end{array}
    }
}

\NewDocumentCommand{\sequence}{ m }{
    \seq_set_from_clist:Nn \l_tmpa_seq { #1 }
    \seq_map_inline:Nn \l_tmpa_seq {\textrm{##1}}
    \image{\seq_use:Nnnn \l_tmpa_seq {,}{,}{,}}
}

\ExplSyntaxOff



\begin{document}
    \image{1,2,3,4}

    This is how it should look like: \image{\textrm{a},\textrm{b}}

    And this errors: \sequence{a,b}
\end{document}
3
  • 1
    For your sequence manipulation, use \seq_set_map:NNn \l_tmpa_seq \l_tmpa_seq { \textrm {##1} } instead of \seq_map_inline:Nn.
    – Skillmon
    Mar 21, 2023 at 11:22
  • 1
    For the second request, by fully expanding your \seq_use:Nnnn before \image reads its argument: \exp_args:Ne \image { \seq_use:Nn \l_tmpa_seq { , } }.
    – Skillmon
    Mar 21, 2023 at 11:25
  • @Skillmon yes, that does exactly what I need. Thank you!
    – Raven
    Mar 21, 2023 at 11:39

1 Answer 1

6

I'd not use array for this job, but \overset.

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\image}{ O{} m }
 {
  \ensuremath
   {
    \seq_set_from_clist:Nn \l_tmpa_seq { #2 }
    [\,
    \seq_indexed_map_inline:Nn \l_tmpa_seq 
     {
      \int_compare:nF { ##1 = 1 } { , }
      \overset{\mathcolor{gray}{\scriptscriptstyle\int_eval:n { ##1 - 1 }} } { \mathstrut #1{##2} }
     }
    \,]
   }
 }

\ExplSyntaxOff

\begin{document}

\image{1,2,3,4}

\image[\mathrm]{a,b}

\end{document}

enter image description here

If you prefer the \image and \sequence syntax:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\image}{ m }
 {
  \ensuremath
   {
    \raven_image_generic:nn { \use:n } { #1 }
   }
 }

\NewDocumentCommand{\sequence}{m}
 {
  \ensuremath
   {
    \raven_image_generic:nn { \mathrm } { #1 }
   }
 }

\cs_new_protected:Nn \raven_image_generic:nn
 {
  \seq_set_from_clist:Nn \l_tmpa_seq { #2 }
  [\,
  \seq_indexed_map_inline:Nn \l_tmpa_seq 
   {
    \int_compare:nF { ##1 = 1 } { , }
    \overset{\mathcolor{gray}{\scriptscriptstyle\int_eval:n { ##1 - 1 }} } { \mathstrut #1{##2} }
   }
  \,]
 }

\ExplSyntaxOff

\begin{document}

\image{1,2,3,4} \image{a,b}

\sequence{a,b}

\end{document}

enter image description here

2
  • Side-note: \mathcolor seems to be a very recent addition to xcolor and doesn't seem to exist in the version contained in TexLive 2022 yet.
    – Raven
    Mar 21, 2023 at 11:58
  • @Raven It is in TL 2022, but not in TL 2021. Anyway, TL 2023 was released 2023-03-19
    – egreg
    Mar 21, 2023 at 16:07

You must log in to answer this question.

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