Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

SetThumbColor("red")

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, ...}}
\SetThumbColor{...}

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

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

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?)

share|improve this question
    
Have a look at the latex3 bundle. You can use the modul clist. –  Marco Daniel Jul 14 '12 at 14:14
1  
@MarcoDaniel Looks more like a property list to me :-) –  Joseph Wright Jul 14 '12 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 '12 at 14:40
    
@JosephWright: Or maybe a combination ;-) –  Marco Daniel Jul 14 '12 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. –  AbstractDissonance Jul 14 '12 at 15:05
add comment

2 Answers

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

\documentclass{article}
\usepackage{color}

\usepackage{kvsetkeys}
\usepackage{ltxcmds}

\makeatletter
\newcounter{thbcol@A}
\newcounter{thbcol@B}[thbcol@A]
\newcommand*{\theThumbColor}{%
  thbcol@\number\value{thbcol@A}.\number\value{thbcol@B}%
}
\newcommand*{\DeclareThumbColors}[1]{%
  \setcounter{thbcol@A}{0}%
  \comma@parse{#1}{%
    \stepcounter{thbcol@A}%
    \expandafter\comma@parse\expandafter{\comma@entry}{%
      \stepcounter{thbcol@B}%
      \expandafter\let\csname\theThumbColor\endcsname\comma@entry
      \@gobble
    }%
    \@gobble
  }%
}
\newcommand*{\UseThumbColor}[2]{%
  \ltx@ifundefined{thbcol@\number#1.\number#2}{%
    \ltx@ifundefined{thbcol@\number#1.1}{%
      black%
    }{%
      \csname thbcol@\number#1.1\endcsname
    }%
  }{%
    \csname thbcol@\number#1.\number#2\endcsname
  }%
}

\DeclareThumbColors{
  red,
  green,
  {cyan, magenta, yellow},
  blue,
}

    \pagestyle{empty}

\begin{document}
  \newcommand*{\Test}{%
    Color:
    \textcolor{%
      \UseThumbColor{\value{section}}{\value{subsection}}%
    }{%
      \UseThumbColor{\value{section}}{\value{subsection}}%
    }%
  }
  \section{Red}
  \Test
  \subsection{Red}
  \Test
  \subsection{Red}
  \Test
  \section{Green}
  \Test
  \section{Cyan}
  \Test
  \subsection{Cyan}
  \Test
  \subsection{Magenta}
  \Test
  \subsection{Yellow}
  \Test
  \subsection{Cyan (first section color)}
  \Test
  \section{Blue}
  \Test
  \section{Black (default)}
  \Test
\end{document}

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:

  • red: \thbcol@1.1
  • green: \thbcol@2.1
  • cyan: \thbcol@3.1
    magenta: \thbcol@3.2
    yellow: \thbcol@3.3
  • blue: \thbcol@4.1

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).

share|improve this answer
add comment
up vote 1 down vote accepted

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

\def\setchcolor#1{
    \textcolor{\directlua{mycolortable["#1"]}}
}

which then can be used like

\setchcolor{chapter1color}

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.