如何通过索引从列表中获取值

如何通过索引从列表中获取值

我通过宏创建一个列表来存储一些值。我想知道是否有方法可以通过列表中的索引号获取值。以下代码显示了我的意图。

代码示例:

\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

例如 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}

相关内容