# Accessing a list at a certain index

I want to define the following command, which is to render an arbitrary number of subfigures. The argument is a nested list:

\multifig{{{fig_1, T0D0, TODO}, {fig_2, T0D0, TODO}}}


Is the following possible?

\newcommand*{\multifig}[1]{
\begin{figure}
\foreach \x in #1 {
\begin{subfigure}{\somewidth\textwidth}
% here I need the element at index 1 of the list \x. how?
\customfig{\x at 1}
\end{subfigure}
}
\end{figure}


Thank you.

• Do you need to use pgffor or would another solution be accepted? – TeXnician Jun 2 '17 at 14:22
• I'm happy with any way to loop over a list of lists and then access the inner list by index. – Gerry Jun 2 '17 at 14:23
• What do fig_1, T0D0 and TODO represent? Perhaps you want to access all three items. Can you specify more clearly what \customfig should do? – egreg Jun 2 '17 at 20:43
• fig_1 is the name of a figure. T0D0 and TODO are captions. I want to access all of them, but they represent different things. So I need to access each one by index explicitly. – Gerry Jun 3 '17 at 10:36

The following code gives you access to a specific item of a (sub)list using expl3.

In its current state it will produce a figure with subfigures and the first item of the sublist printed (change \__gerry_customfig to alter that behavior). Please note that I have set the subfigures to a fixed width by now (I do not know your \somewidth).

\documentclass{article}
\usepackage{expl3,xparse}
\usepackage{subcaption}

\ExplSyntaxOn
\clist_new:N \l__gerry_mycl
\cs_new:Npn \__gerry_customfig:n #1 {
\detokenize{#1}
}
\NewDocumentCommand{\multifig}{m}{
\begin{figure}
\clist_set:Nn \l__gerry_mycl {#1}
\clist_map_inline:Nn \l__gerry_mycl {
\begin{subfigure}{.3\textwidth}
\clist_set:Nn \l_tmpa_clist {##1}
\exp_args:Nx \__gerry_customfig:n {\clist_item:Nn \l_tmpa_clist {1}}
\end{subfigure}
}
\end{figure}
}
\ExplSyntaxOff

\begin{document}
\multifig{{fig_1, T0D0, TODO}, {fig_2, T0D0, TODO}}
\end{document}