# Coloring combining characters without changing color of a base character

## Problem

I am trying to find out is there a good way to paint combining characters without changing the color of the base character they are combined with. My actual problem case is to make some Hebrew inflection charts with colored prefixes and suffixes. Problem area there is to being able to color vowel points, cantillation and other diacritical marks without changing the color of the base consonant. As a clarification, in following image from Wikipedia, consonants are in black, vowels in red and cantillation in blue.

As an illustration what I would like to be able to produce, I quickly photoshopped following example.

## Search for solution

In my search so far, I have found that there has been discussion about this question in XeTeX mailing list in 2007. Back then there was not perfect solution. However a workaround with two overlapping words with different colors was proposed. Also there was noted that coloring solution for ArabTeX could be ported for Hebrew.

Unfortunately the workaround is quite laborious solution as you have to write every word twice, and also it doubles the chances to write something wrong. As what comes to the idea of porting ArabTeX, I am far too inexperienced with TeX that I could give that a try.

So I am hoping that there has been some development with this problem since 2007 with XeTeX or this can be achieved gracefully with some other tools.

Also please note me if you happen to know that this can be achieved word processors such as Word, OpenOffice.org Writer etc, as I am not aware if they can handle this either. (I know that Word can paint all diacritics, but I am not aware if it can paint just some diacritics.)

• Without knowing for sure, I think that LuaTeX / ConTeXt could do that. I saw a presentation where something similar was done with Arabic. That is, you wouldn't need to typeset anything twice, just define that you'd want something colored. – morbusg Mar 9 '11 at 17:42
• Do you mean ligatures or Unicode combining characters? For combining characters, c{\color{red}̧} (there should be an invisible combining cedilla between the two closing braces) works in both XeLaTeX and LuaLaTeX. Ligatures, on the other hand combine to a single symbol in the font and I don't think there is a way to color the parts individually. – Caramdir Mar 10 '11 at 0:40
• Further experimentation shows however that this approach has some positioning issues. – Caramdir Mar 10 '11 at 0:57
• @Caramdir: Actually the cedilla is not invisible here (DejaVu fonts in Firefox on Ubuntu). There's a little cedilla underneath the \rbrace. It looks very strange – kahen Mar 11 '11 at 19:42
• @kahen: With the same setup I get a cedilla between the braces. We seem to use different font renderers (yours is of course more correct than mine because the cedilla is a combining mark). – Philipp Mar 11 '11 at 19:49

In PDFTeX/XeTeX colouring is done by inserting pdfliteral nodes around coloured items, these nodes would then interfere with mark positioning in this case, something like:

<base><start-color><mark><stop-color>


LuaTeX can use an alternate mechanism thanks to its "attribute" registers; attributes is a way to annotate input without interfering with pdfliterals and likes and can be employed for many things including colouring. But attributes is a low level mechanism and need to be employed by higher level packages.

In ConTeXt, attributes are used out of box, so it just works:

\definefontfeature[hebrew][arabic][script=hebr]
\definefont[hebrew][name:sblhebrew*hebrew]

\starttext
\textdir TRT
\hebrew
\color[red]{א}\color[blue]{֣}\color[green]{֚}ב\color[blue]{ָ}\color[green]{ג}֦ד\color[green]{֘}
\stoptext


In LuaLaTeX you need luacolor to use the attributes mechanism,

\documentclass{minimal}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{luacolor}

\newfontface\hebrew[Script=Hebrew]{SBL Hebrew}
\begin{document}
\textdir TRT
\hebrew
\textcolor{red}{א}\textcolor{blue}{֣}\textcolor{green}{֚}ב\textcolor{blue}{ָ}\textcolor{green}{ג}֦ד\textcolor{green}{֘}
\end{document}


P.S. I don't have Arial here and the only font I've with those marks is SBL Hebrew, so I used it for testing.

• Is there a way it could be done in html/php? – Mike Oct 16 '17 at 11:50
• @Mike: Use spans with different colors, doe not work in Chrome though. – Khaled Hosny Nov 1 '17 at 20:02
• @KhaledHosny can you show a working example with XeTeX, from this link, as I read it, it can't be done with the XeLaTeX compiler. I also posted a seperat question on this link. – imdk4242 Aug 10 '19 at 13:41

For completeness sake, here is an answer using ConTeXt's font colour schemes feature (using Arabic, but should apply to any scripts):

First write a font “goodies” file containing categories of glyphs names (glyph name are font dependant, so you've to check the font you are using) that should share the same colour (it is just a lua script):

-- save as 'amiri.lfg'
return {
name = "Amiri",
version = "1.00",
comment = "Goodies that complement the Amiri font.",
author = "Khaled Hosny",
colorschemes = {
default = {
[1] = { -- category 1
"uni064E", "uni064B",
},
[2] = { -- category 2
"uni064F", "uni064C",
},
[3] = { -- category 3, etc.
"uni0650", "uni064D",
},
}
}
}


Update: With ConTeXt 2012.03.02 it is now possible to use Unicode character numbers instead of glyph names, so the the lfg file will not be font dependant (using glyph names is still good to access un-encoded glyphs, which is font dependant by definition).

-- save as 'amiri.lfg'
return {
name = "Amiri",
version = "1.00",
comment = "Goodies that complement the Amiri font.",
author = "Khaled Hosny",
colorschemes = {
default = {
[1] = { -- category 1
0x064E, 0x064B,
},
[2] = { -- category 2
0x064F, 0x064C,
},
[3] = { -- category 3, etc.
0x0650, 0x064D,
},
}
}
}


Then you can use it from your ConTeXt document, using usual font and colour conventions:

% overload ‘arabic‘ font feature set to load the goodies
\definefontfeature[arabic][arabic]
[goodies=amiri,       % name of the goodies file
colorscheme=default] % name of the scheme we defined

% define color scheme 1, categories 1, 2 and 3
\definecolor[colorscheme:1:1][r=1]
\definecolor[colorscheme:1:2][g=1]
\definecolor[colorscheme:1:3][b=1]

% color scheme 2
\definecolor[colorscheme:2:1][c=.55]
\definecolor[colorscheme:2:2][m=.55]
\definecolor[colorscheme:2:3][y=.55]

\setupalign[r2l]

\starttext
\definedfont[name:amiri*arabic at 36pt]

\setfontcolorscheme[1]
ضَرَبَ ضُرِبَ ضَرْبًا

\setfontcolorscheme[2]
ضَرَبَ ضُرِبَ ضَرْبًا
\stoptext


I will here sum up a solution hinted at in a mailing list post linked to in the OP's question (the link in the question is dead; working link here: XeTeX mailing list link, 2007 July, 6989).

It is possible to achieve this in XeLaTeX without the use of any additional packages if you have access to a font editor software. However, there is some manual work involved and it is probably too tedious if you have more than a handful of cases.

The trick is to make a duplicate of the base letter and make it invisible, then use negative kerning to overlay the invisible base + colored mark with the normal (black) base.

The procedure is as follows (exact steps depend on the font editor):

1. In the font editor, load your font and make a duplicate of the base letter. Make sure that the duplicate preserves the exact metrics (width and sidebearings), and also keeps the anchors in the same positions, but delete the stroke. Assign the duplicate a code in the PUA or define it as a stylistic alternative to make it accessible in a layout engine / word processor.
2. Still inside the font editor, note the x-height and the width of the base character.
3. Export the font and install it on your system.
4. In the XeLaTeX-file, type something like this: {\color{red} INVISIBLE BASE + MARK}\kern-1.42ex NORMAL BASE. You will need to adjust the value for kerning, which you can calculate from the width of the base / x-height.

I have successfully used this procedure with the font editor Glyphs and XeLaTeX to make colored bases with black diacritics (so the inverse of the OP's question).

To exemplify Caramdir's comment, the following minimal example produces base and combining characters of different color when compiled with XeLaTeX or LuaLaTeX:

\documentclass{minimal}
\usepackage{fontspec}
\usepackage{xcolor}
\setmainfont[Script=Hebrew]{Arial}
\begin{document}
\textcolor{red}{א}\textcolor{blue}{֣}\textcolor{green}{֚}ב\textcolor{blue}{ָ}\textcolor{green}{ג}֦ד\textcolor{green}{֘}
\end{document}

• I think this solution does not actually combine the characters. It just places them individually to the paper and it causes positioning issues. To prove my point, I noticed that when colored in this way the text runs from left to right. However when not colored the text runs from right to left, as it should in Hebrew. (Thanks for the complete example though.) – ojs Mar 11 '11 at 20:32
• This solution do not run properly in XeLaTeX, and something tells me that it can't be done (the proper way) with this compiler tex.stackexchange.com/a/239031/192642 – imdk4242 Aug 10 '19 at 13:38

One possible solution would be to exploit the babel Hebrew support. Recall that cp1255, for instance, has the following declaration:

\DeclareInputText{200}{\hebqamats}


Using \renewcommand, you could redefine \hebqamats to include coloring commands prior to the insertion of the actual glyph. In order to avoid infinite loops, you'd of course have to refer to the glyph by its cp1255 character code (200).