[Tex/LaTex] Data structures for TeX

packagesprogramming

I often find myself wanting to use standard data structures in TeX. For example, lists or maps. Each time this need comes up, I reinvent the wheel. For some reason, I find myself using stacks fairly frequently.

Is there a standard package (either plain TeX or LaTeX) that offers a convenient interface to data structures? They would be most useful if the structures could be combined so that you could have lists of maps, for example.

Best Answer

One of the ideas of expl3 is to provide a number of standard programming tools, and data structures are included in this. Currently, the structured data types available are sequences, stacks and property lists. Sequences and stacks have the same underlying structure: they are different ways of looking at the same thing. So we might have

\seq_new:N \l_my_seq
\seq_put_right:Nn \l_my_seq { Some-tokens } % Sequence-like access
\seq_push:Nn \l_my_seq { More-tokens } % Stack-like access, puts to left

Property lists are a key-value type data type

\prop_new:N \l_my_prop
\prop_put:Nnn \l_my_prop {  key-name } { value }

Property lists are 'unordered', whereas sequences/stacks are ordered. In both cases there are functions available to map to each item in the stored data, for example \seq_map_function:NN.

New data types can be added to expl3 as needed: this depends on the requirements of the LaTeX3 Project but also of others. (Note of course that I'm on the LaTeX3 Project!)

An alternative set of packages providing some similar ideas to expl3 including a number of data-related structures is the combination of etoolbox and etextools. These two packages provide functions for mapping to lists and so on.

You might also look at the datatool package, which can be used to construct tables in LaTeX and access the data in a database-like fashion.

One other note. LuaTeX provides access to the native Lua data storage methods, which if you are doing a LuaTeX-only project would of course be the first place to look.

Related Question