# Customized list of elements using etoolbox

I need to have several lists with access by index (array-like). My minimum non-working example follows:

\documentclass{minimal}
\usepackage{etoolbox}

\newcounter{listAcounter}

\stepcounter{#2}%
\csdef{#1\the#2}{#3}
}

\newcommand\getFromList[2]{%
\csuse{#1#2}
}

\begin{document}

This is element 2 from list A: \getFromList{listA}{2}.

\end{document}


I get from compiling with pdflatex:

! You can't use the letter l' after \the.
<argument> listA\the l
istAcounter


How can I fix this code? Why is this wrong?

• Welcome to TeX.SX! Perhaps \csdef{#1\csuse{the#2}}{#3} will work. – egreg Feb 24 '15 at 14:04

When you type \the#2 you have an already formed token, so this will not be merged into a single command name such as \thelistAcounter. You have to build the name yourself:

\documentclass{article}
\usepackage{etoolbox}

\newcounter{listAcounter}

\stepcounter{#2}%
\csdef{#1\csuse{the#2}}{#3}
}

\newcommand\getFromList[2]{%
\csuse{#1#2}%
}

\begin{document}

This is element 2 from list A: \getFromList{listA}{2}.

\end{document}


Note the % after \csuse{#1#2}.

An easier implementation with expl3, that avoids defining a counter.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\newList}{m}
{
\seq_new:c { l_kees_list_#1_seq }
}
{
\seq_put_right:cn { l_kees_list_#1_seq } { #2 }
}
\NewDocumentCommand{\getFromList}{mm}
{
\seq_item:cn { l_kees_list_#1_seq } { #2 }
}
\ExplSyntaxOff

\begin{document}

\newList{listA}
`