Wednesday, 7 August 2013

How can I use `pgffor` to iterate through a database of keys and properly retrieve them

How can I use `pgffor` to iterate through a database of keys and properly
retrieve them

I would like to create a command that stores a database. I then want to
iterate over the database and print out the results to the document. This
MWE shows two different approaches I've taken. But neither works.
\documentclass{article}
\usepackage{pgffor,pgfkeys}
\pgfkeys{/ae/breakfast/menu/.cd,
fruit/.initial = grape fruit,
bread/.initial = English muffin,
eggs/.initial = hard boiled,
}
\def\allrecords{%%'
{ fruit=apple,
bread=bagel,
eggs=scrambled
},
{ fruit=orange,
bread=toast,
eggs=fried
}
}
\def\aeget#1{\pgfkeysvalueof{/ae/breakfast/menu/#1}}
\def\whatIordered{ I ordered \aeget{fruit}, \aeget{bread}, and \aeget{eggs}.}
\pagestyle{empty}
\begin{document}
\whatIordered
%<approach 1>% this fails: "whitespace" getting in the way
%%\foreach \x in \allrecords
{\pgfkeys{/ae/breakfast/menu/.cd,\x}\whatIordered\newline}
%<approach 2>% this also fails: keys are misread
%%\foreach \x in \allrecords {\foreach \y in \x
{\pgfkeys{/ae/breakfast/menu/\y}} \whatIordered\newline}
\end{document}
Is there a means of using pgfkeys and then iterating over a database?

No comments:

Post a Comment