How to use data structures in TikZ?
I don't know about expl3
but it is possible within TikZ, or rather, within pgfkeys
. Since there is an l3keys
submodule of LaTeX3, I assume basically the same idea works there. You can create your data structure in much the same way as in Perl:
\pgfkeys{
/names/.cd,
make name/.style = {
#1/color/.initial = black
},
make name/.list = {Katie, Frank, Laura},
Frank/color = blue
}
Then you can extract the color of a name via, say:
\newcommand\getcolor[1]{%
\pgfkeysgetvalue{/names/#1/color}%
}
which is fully expandable and can be used wherever you need the color of a name. Of course, for this example there is no need for the single subkey Frank/color
(you could just set Frank/.initial = blue
) but if you want more properties, you can add them by giving them their own named subkeys as well.
This ultimately does not answer to the question:
Is it possible to implement this in
expl3
?
thus it is marked community wiki.
Besides the interesting aspect of dealing with expl3
, this job is perfectly doable just with the standard syntax of TikZ foreach in combination with xstring
:
\documentclass{article}
\usepackage{tikz} % won't work just with pgffor
\setlength{\parindent}{0cm}
\usepackage{xstring}
\begin{document}
\foreach \name [count=\i from 0]in{Katie,Frank=>blue,Laura=>red}{
\IfSubStr{\name}{=>}{% true
\StrCut{\name}{=>}\xname\namecol
\i: \textcolor{\namecol}{\xname}\par%
}{% false
\i: \name\par%
}
}
\end{document}
This provides:
The idea behind is more or less like this:
- look for the separator string (
=>
in the example); - if present cut the string into two parts (name and color) and use them;
- if not present, use the standard syntax.
Of course, it is possible to:
- change separator string;
- do much more complicated things (i.e. adding more properties: just nest some conditionals or, in a simpler manner, define one separator string per property).