In my book I need to use certain colors for certain chapters. One way to understand this is how to efficiently color a thumb index. I would like to make it easy to setup the colors in a data array(not a latex typographical array) and easily access the values from the array.

In a normal programming language you would do something like

var ThumbColors = {Red, Blue, Green, ...}
SetThumbColor(ThumbColors[k]) (where k maybe the current chapter or whatever)

Code like this allows one to separate the code from the data and hence makes it extremely easy to change the colors without having to find every instance of SetThumbColor. e.g., it is very bad to do this


as it requires one to change every instance, who knows where, to change the colors.

So, I need some way to implement the first case.

\NewCommand{\ThumbColors}{{red, green, blue, ...}}

OR, even better, some way to use a dictionary(key-value pairs) in an easy way:

\NewCommand{\ThumbColors}{{chapter1 = red, chapter2 = green, chapter3 = blue, ...}}

The main thing is it should be quick to type. It's very easy to do in a programming language and i don't want to have to do 15 lines of code just to do something similar to 1 or 2 lines(else why not just hard code it in the first place?)

  • Have a look at the latex3 bundle. You can use the modul clist. Jul 14, 2012 at 14:14
  • 1
    @MarcoDaniel Looks more like a property list to me :-)
    – Joseph Wright
    Jul 14, 2012 at 14:37
  • 4
    @AbstractDissonance The 'classical' way to do this in TeX is to use a csname-based approach of the form \csname my@array@<index>\endcsname. There is a performance implication depending on array size (due to hash table collisions). How big an array are we talking? See also Global key-value dictionary.
    – Joseph Wright
    Jul 14, 2012 at 14:40
  • @JosephWright: Or maybe a combination ;-) Jul 14, 2012 at 14:56
  • @JosephWright Well, I guess it won't be to large(50+ keys though at least) but will need to be nested. So a key have a value that is another dictionary. I think I'm just going to implement it in lua and wrap it with some tex macros. Jul 14, 2012 at 15:05

2 Answers 2


The following implements the 'color array' in the 'classical way'. But it also supports a further array level.



      \csname thbcol@\number#1.1\endcsname
    \csname thbcol@\number#1.\number#2\endcsname

  {cyan, magenta, yellow},


  \subsection{Cyan (first section color)}
  \section{Black (default)}

The result:

enter image description here

The color specifications are stored in a two-dimensional array. The indices are based on 1 (like section and chapter numbers). Thus the declared colors are stored in the following macros:

The colors are addressed by the first and second number. If the color is not found, then then 1 as second number is tried, see the section without subsections (second number is zero) or section 3.4, where no color was declared. Otherwise black is used as default (see section 5).


I've resorted to storing the data in a lua table and creating TeX macros to access the data

Suppose your data is stored in a lua data table as

mycolortable = {chapter1color = "red", ...}

and you want to use the values to set some color


which then can be used like


With these "helper" macros one only needs to change the lua data table to change the colors without having to mess with TeX.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .