# Edit-proof, Consistent Use of Colour

I'm sorry if this is a vague question.

I'm writing something (I'm not at liberty to share yet) that involves a lot of complicated ideas and I'm experimenting with the use of coloured text and equations throughout the LaTeX document to help explain things. I've found that staying consistent is increasingly difficult as time goes by.

What I'd like is a way to tell LaTeX to, say, run through the (appropriately visible) hues of the rainbow throughout the use of colour in the document. This needs to be in such a way that I can reuse previous colours in a consistent manner relatively immune edits.

I hope that makes sense.

I have no idea how to go about doing this.

Just imagine a command for a Mathematics paper in LaTaX where you just put something before the \begin{document} thing and henceforth use something like \RainbowColour[x]{X} to colour the text/equation X with an optional label "x" for that colour, so that LaTeX would distribute the colours accordingly and the colour with label "x" can be reused if needed.

Suppose I have this:

using

\documentclass[a4paper, fleqn]{amsart}

\usepackage{color}

\begin{document}

\textcolor[rgb]{1,0,0}{Far far away,} behind the word mountains, far from the countries \textcolor[rgb]{1,0.41,0.13}{Vokalia and Consonantia,} there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.

A small river named Duden flows by their place and supplies it with the \textcolor[rgb]{1,1,0}{necessary regelialia}. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.

Even the \textcolor[rgb]{0,1,0}{all-powerful Pointing} has no control about the blind texts it is an almost \textcolor[rgb]{0,0,1}{unorthographic life} One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.

The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little \textcolor[rgb]{0.5,0,0.5}{Blind Text} didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.

\end{document}


Suppose, after this, that I want to colour "about the blind texts" and for emphasis highlight "World of Grammar" the same way. Then I would have to change all the colours that are currently there by manipulating the number in \textcolor[rgb]{1,0.41,0.13}{Vokalia and Consonantia,} for instance to make the rainbow smoother. This gets labour intensive after a while, especially with a large and complicated document. Let's say I've pulled it off like so:

using

\documentclass[a4paper, fleqn]{amsart}

\usepackage{color}

\begin{document}

\textcolor[rgb]{1,0,0}{Far far away,} behind the word mountains, far from the countries \textcolor[rgb]{1,0.41,0.13}{Vokalia and Consonantia,} there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.

A small river named Duden flows by their place and supplies it with the \textcolor[rgb]{1,1,0}{necessary regelialia}. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.

Even the \textcolor[rgb]{0,1,0}{all-powerful Pointing} has no control \textcolor[rgb]{0,0.58,0}{about the blind texts} it is an almost \textcolor[rgb]{0,0,1}{unorthographic life} One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far \textcolor[rgb]{0,0.58,0}{World of Grammar}.

The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little \textcolor[rgb]{0.5,0,0.5}{Blind Text} didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.

\end{document}


But wait! That was the wrong thing to highlight! Damn it! Now I have to change everything once again!

And so on . . .

• Since colors are defined by 3 (rgb) or 4 (cmyk) parameters, it seems to me that cycling through these parameters, and remembering colors by their parameter values would be the easiest approach. Without knowing more about how/when you want to change colors, I'm not sure what I can add... Further, once you decide on the maximum resolution of the color gradations, you can even convert the 3 or 4 parameters into a single scalar that uniquely defines a color, based on a conversion algorithm (e.g., scalar = 256*256*r + 256*g + b). – Steven B. Segletes Dec 4 '14 at 15:29
• You could create a comma delimited list of colors and refer to them by index. This can be done using xparse or a tikz array. – John Kormylo Dec 4 '14 at 16:09
• Can you give an example of the code you would like to be able to write given somebody implements the macros for you? Like a use case and desired output? – Bordaigorl Dec 6 '14 at 17:28
• I suppose so (for the desired output), @Bordaigorl, but not right now as the document I'm working on is on a different computer. Just imagine a command for a Mathematics paper in LaTaX where you just put something before the \begin{document} thing and henceforth use something like \RainbowColour[x]{X} to colour the text/equation X with an optional label "x" for that colour, so that LaTeX would distribute the colours accordingly and the colour with label "x" can be reused if needed. – Shaun Dec 6 '14 at 17:37
• @Peter's answer seems to be the easiest way to get this but I got the impression you do not know the total number of distinct colors you may need in the document and that the association label-color should be automatic as an interpolation between two (or more) colors based on the total #of distinct labels? – Bordaigorl Dec 6 '14 at 18:10

I am interpreting "Edit proof" as "so that if we add more labels the colors are adjusted automatically in a consistent way".

Here I'll define a macro \colorize{label}{text} that will assign a distinct color to each distinct label and typeset text using that color. It can be easily adapted to use \color instead of \textcolor if you need to set the color globally or in equations etc.

It does not need to know the labels in advance, nor the total number of labels. It can be adapted to generate colors from any specific gradient.

\documentclass{article}

\usepackage{xcolor}

\makeatletter

\newcount\colorlabels
\colorlabels=0\relax
\newcount\colorize@curr
\AtEndDocument{\write\@auxout{\protect\total@colors{\the\colorlabels}}}
\def\total@colors#1{\global\def\totalcolors{#1}}
\total@colors{1}

\newcommand{\colorize}[2]{%
\@ifundefined{\string\color@colorize#1}{%
\colorize@curr=\colorlabels\relax%
\multiply\colorize@curr by 451%
\divide\colorize@curr by \totalcolors\relax%
\definecolor{colorize#1}{wave}{\the\colorize@curr}%
}{}%
\textcolor{colorize#1}{#2}%
}

\makeatother

\begin{document}

\colorize{fst}{Far far away,} behind the word mountains, far from the countries \colorize{other}{Vokalia and Consonantia,} there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.

A small river named Duden flows by their place and supplies it with the \colorize{lbl3}{necessary regelialia}. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.

Even the \colorize{lbl4}{all-powerful Pointing} has no control \colorize{important}{about the blind texts} it is an almost \colorize{lbl5}{unorthographic life} One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far \colorize{important}{World of Grammar}.

The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little \colorize{lbl6}{Blind Text} didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.

\end{document}


Note how the two parts marked with the label important are both colored with the same color. Also, the gradient is uniformly distributed to labels, so if you add a new label, all the colors will be shifted a bit to leave room for the newcomer.

### How it works

The idea is to associate to each label a unique color in the visible light spectrum (by using the wave color model of xcolor); this can be generalised to different gradients. To decide which color we just associate an incremental number, stored in \colorlabels, with each label LBL and compute the color (\colorlabels*451/tot)+362 (see docs of the wave model of xcolor to understand this expression); this color is saved with the name colorizeLBL, which is the color name used to color the text marked with the label LBL. To compute the total number of labels we need to save the last value \colorlabels generated into the aux file so that at a second run of latex we can get the value in \totalcolors.

A limitation is that there are only 451 valid values for a color in the wave model, so if you have more labels than that you need to redefine the way colors are generated.

• That's excellent, thank you! I'll give it a few more days to see how things go, but the bounty and stuff is as good as yours :) – Shaun Dec 6 '14 at 19:29
• I edited it to demonstrate how to obtain a solution for your MWE – Bordaigorl Dec 6 '14 at 19:47
• Okay, okay; it's yours :) – Shaun Dec 6 '14 at 19:59
• Hehe, I just thought it was a good question =) – Bordaigorl Dec 6 '14 at 20:35