3

I build a list by a macro to store some values. I wonder if there is method to get value by its index number from the list. The following code shows my intention.

Code example:

\documentclass{article}
\def\mylist{a,bc,efg,hijk,lmn,} %define a macro to stroe a series of values.
\newcommand\test[2]{...}% #1 for index number; #2 is a list.
\test{2}{\mylist} % "bc" expected.
\test{5}{\mylist} % "lmn" expected.
\test{6}{\mylist} % get nothing.
\end{document}

1 Answer 1

5

For example with expl3:

\documentclass{article}
\ExplSyntaxOn
\seq_new:N\mylist
\seq_set_from_clist:Nn\mylist{a,bc,efg,hijk,lmn,}
\newcommand\test[2]{\seq_item:Nn#2{#1}}
\ExplSyntaxOff
\begin{document}
\test{2}{\mylist} % "bc" expected.
\test{5}{\mylist} % "lmn" expected.
\test{6}{\mylist} % get nothing.
\end{document}
2
  • Many thanks! The content of \mylist may change at any time/at any place. Must it be put between \ExplSyntaxOn and \ExplSyntaxOff?
    – lyl
    Mar 21, 2022 at 9:38
  • you can always write some user command similar to the \test command which works outside too. Mar 21, 2022 at 9:43

You must log in to answer this question.

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