# Looping over charts and names simultaneously

I want to use a common index to loop over two separate comma-separated lists at once, one which is a list of abbreviations and one a list of names. I want to use the abbreviations to import charts as pdfs, and the names to label them.

This is what I have so far:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{graphicx}

\def\namelist{"New York", "Illinois","Texas"}
\def\abrevlist{NY,IL,TX}

\begin{document}

\foreach \x in \abrevlist {%
\newpage
\section*{\x}  % <-- I want to use the state name here

\begin{figure}[h!]
\includegraphics[width=.5\textwidth]{../charts/summary/\x _compare.pdf}
\caption{\x}  % <-- I want to use the state name here
\end{figure}

}
\end{document}


This imports the figures as I want, but does nothing with the state names. I would like to replace the section and captions with, for example, "New York" in place of "NYC".

Obviously, one could create an index \i and loop over that to index the abbreviation or name as necessary. But I can't make that work. Any suggestions? Thanks!

If you do not need both lists elsewhere, you can just combine them in a single list:

\documentclass[12pt]{article}
%\usepackage{tikz}
\usepackage{pgffor}
\usepackage{graphicx}

\def\namelist{New York/NY,Illinois/IL,Texas/TX}

\begin{document}

\foreach \x/\y in \namelist {%
\newpage
\section*{\x}  % <-- I want to use the state name here

\begin{figure}[h!]
%        \includegraphics[width=.5\textwidth]{../charts/summary/\x _compare.pdf}
\texttt{../charts/summary/\y-compare.pdf}
\caption{\x}  % <-- I want to use the state name here
\end{figure}

}
\end{document}


(The \texttt part shamelessly copied from @egreg)

It is not necessary to load the whole tikz, pgffor is enough for this.

\documentclass[12pt]{article}
\usepackage{tikz}

\def\namelist{"New York", "Illinois","Texas"}
\def\abrevlist{NY,IL,TX}

\begin{document}

\foreach \X [count=\Y starting from 0] in \abrevlist {%
\newpage\pgfmathsetmacro{\mystate}{{\namelist}[\Y]}
\section*{\mystate}  % <-- I want to use the state name here

\begin{figure}[h!]
%\includegraphics[width=.5\textwidth]{../charts/summary/\X _compare.pdf}
\caption{\X}  % <-- I want to use the state name here
\end{figure}

}
\end{document}

• Nice, but you removed the line that references the filename for the figure. Can you edit to retain that? Will your \X still work ok there? – squipbar May 21 '19 at 21:13
• @squipbar Good catch. I removed it because I do not have your graphics files.Now it should work if you uncomment it. (There are reasons why \X might be better than \x, even though here it does not matter.) – user121799 May 21 '19 at 21:18
• This still gives me an error. I get: ! Package PGF Math Error: Unknown function New' (in '{New York,Illinois,Texas}[0]'). – squipbar May 21 '19 at 21:54
• @squipbar Do you run the precise code from above or a version in which you removed the " from \namelist? The error message suggests that you are using something like \def\namelist{{New York},{Illinois},{Texas}} or \def\namelist{New York,Illinois,Texas}. Also the number of { is important in \pgfmathsetmacro{\mystate}{{\namelist}[\Y]}, i.e. \pgfmathsetmacro{\mystate}{\namelist[\Y]} won't work. – user121799 May 21 '19 at 21:57
• Yes, I was removing the ". But only because I was getting an error when I included them! With them, I get ! Missing number, treated as zero. <to be read again> N l.18 }~. So it looks like this solution is indexing the "N" somehow, rather than the whole fist entry, "New York" (evidence: if I change the first state to Illinois, that N becomes an I). – squipbar May 22 '19 at 14:39

Here's my proposal.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\maketwoprongedlist}{m m}
{
\prop_new:c { g_squipbar_list_#1_plist }
\seq_new:c { g_squipbar_list_#1_seq }
\clist_map_inline:nn { #2 }
{
\__squipbar_list_entry:nnn {#1} ##1
}
}

\cs_new_protected:Nn \__squipbar_list_entry:nnn
{
\seq_gput_right:cn { g_squipbar_list_#1_seq } { #2 }
\prop_gput:cnn { g_squipbar_list_#1_plist } { short@#2 } { #2 }
\prop_gput:cnn { g_squipbar_list_#1_plist } { long@#2 } { #3 }
}

\NewDocumentCommand{\usetwoprongedlist}{m +m}
{
\cs_set_protected:Nn \__squipbar_list_entry:nn { #2 }
\seq_map_inline:cn { g_squipbar_list_#1_seq }
{
\__squipbar_list_entry:nn
{ \prop_item:cn { g_squipbar_list_#1_plist } { short@##1 } }
{ \prop_item:cn { g_squipbar_list_#1_plist } { long@##1 } }
}
}

\ExplSyntaxOff

\maketwoprongedlist{states}{
{NY}{New York},
{IL}{Illinois},
{TX}{Texas}
}

\begin{document}

\usetwoprongedlist{states}{%
\newpage
\section*{#2}  % <-- I want to use the state name here

\begin{figure}[h!]
\centering
%\includegraphics[width=.5\textwidth]{../charts/summary/\x _compare.pdf}
\texttt{../charts/summary/#1-compare.pdf}
\caption{#2}  % <-- I want to use the state name here
\end{figure}
}

\end{document}


Since I don't have your pictures, I emulated them by just printing the file name.

## How does this work?

First of all, I define a list giving it a name. The items are comma separated and should consist of {<abbreviation>}{<full name>}. The first part is also used for indexing and so it should consist of characters only (but this might be overcome, in case of need).

Each part is then stored as an item in a property list, indexed as short@<abbreviation> and long@<abbreviation>. Also a sequence is maintained containing the abbreviations, for later usage; the order will be that of input.

The \usetwoprongedlist command has two arguments: the first is the list to be processed; the second argument is a template, just like the main argument to \foreach; the difference is that #1 and #2 are used to denote the short and long versions of the current item.

A scratch function is defined and then the sequence indexing the list's items is mapped, calling the scratch function with arguments

\prop_item:cn {<list name>} { short@<current item> }


and

\prop_item:cn {<list name>} { long@<current item> }
`

respectively. Et voilà.