Looping over charts and names simultaneously
\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}
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à.
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.