# Coloring all occurrences of some specific characters

I have been wondering for ages how to do something that is absolutely crucial to the document I am writing. (I usually use XeLaTeX, but I don’t mind changing if required.)

I would like to have a list of characters (presumably Japanese characters, but maybe other ones as well) that would be typeset in a specific way. For instance, put all the ‘u’ and ‘W’ of the document in red.

In my case, the goal is to highlight all the characters (of a text) that are among a set of characters children should learn.

Now, I know I could define a macro or a new command for every character and then replace all its occurrences with that macro, but that would be a titanic job because I expect the number of different characters to be around 200.

Does anyone have an idea ? Thank you very much.

• Welcome! Just have your macro take an argument and then feed the characters to the macro. If they are to be consistently formatted, you want one macro which you use to format the 200 characters. You don't need - or want - 200 macros. – cfr Jan 27 '16 at 3:21

This is precisely one of the things that XeTeX makes excitingly easy to achieve. See the section Character classes in its documentation (texdoc xetex in your TeXLive/MikTeX installation or http://texdoc.net/texmf-dist/doc/xetex/xetexref/xetex-reference.pdf).

Here is an example, but you definitely should check the XeTeX reference for more, especially as you mention Japanese characters, thus you may have to add lines on the model below with "0" replaced by "1", "2", and "3".

\documentclass{article}
\usepackage{color}

\XeTeXinterchartokenstate = 1
\newXeTeXintercharclass \mycharclassRed
\XeTeXcharclass \u \mycharclassRed
\XeTeXcharclass \W \mycharclassRed
% adding the more frequent lowercase w:
\XeTeXcharclass \w \mycharclassRed

\XeTeXinterchartoks 0 \mycharclassRed   = {\bgroup\color{red}}
\XeTeXinterchartoks 255 \mycharclassRed = {\bgroup\color{red}}

\XeTeXinterchartoks \mycharclassRed 0   = {\egroup}
\XeTeXinterchartoks \mycharclassRed 255 = {\egroup}

\begin{document}

I have been wondering for ages how to do something that is absolutely crucial
to the document I am writing. (I usually use Xe\LaTeX, but I don’t mind
changing if required.)

I would like to have a list of characters (presumably Japanese characters, but
maybe other ones as well) that would be typeset in a specific way. For
instance, put all the u' and  W' of the document in red.

In my case, the goal is to highlight all the characters (of a text) that are
among a set of characters children should learn.

Now, I know I could define a macro or a new command for every character and
then replace all its occurrences with that macro, but that would be a titanic
job because I expect the number of different characters to be around 200.

Does anyone have an idea ? Thank you very much.

\end{document}

% Local Variables:
% TeX-engine: xetex
% End:


The lowercase w was also added to the u and W for highlighting in this example.

• Thank you! You trick allows me not to use LuaLaTeX, which I’m not very keen on – Zozor Jan 27 '16 at 11:46

Here's a LuaLaTeX-based solution. It defines a Lua function, named color_chars, that renders in red all instances of characters contained in a pre-defined set of characters. The code uses the function unicode.utf8.gsub instead of the more basic string.gsub, so that it can handle characters that aren't ASCII-encoded. The code take care not to operate on TeX and LaTeX macros.

The LaTeX macros\ColorMeOn and \ColorMeOff, respectively, enable and disable the operation of color_chars on the input stream.

In the following example, all instances of q and Q are automatically rendered in red if \ColorMeOn has been executed.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode,xcolor}
\begin{luacode}
function color_chars ( s )
s = unicode.utf8.gsub ( s , "(\\?)([%a%@]+)" , function( back, text )
if back=="" then
text = unicode.utf8.gsub (text, "[qQ]",
"\\textcolor{red}{%0}" )
end
return back .. text
end)
return s
end
\end{luacode}

"process_input_buffer", color_chars, "ColorMe")}}
\newcommand\ColorMeOff{\directlua{luatexbase.remove_from_callback(
"process_input_buffer", "ColorMe")}}

\begin{document}
\ColorMeOn
quick Quiet

\ColorMeOff
quick Quiet
\end{document}

• I have error ! Missing number, treated as zero. <to be read again> \relax l.140 \newcatcodetable\catcodetable@initex – Salim Bou Jan 27 '16 at 5:45
• @salimbou - Which TeX distribution and which versions of LuaTeX and of the LaTeX format do you have on your system? (I have MacTeX2015, LuaTeX 0.80, and LaTeX2e <2015/10/01> patch level 2 on my system.) – Mico Jan 27 '16 at 5:47
• @salimbou - Do you by any chance execute \ColorMeOn before \begin{document}? That would definitely cause problems... – Mico Jan 27 '16 at 6:03
• This is what shows in log file: This is LuaTeX, Version beta-0.80.0 (TeX Live 2015/W32TeX) (rev 5238) (format=lualatex 2015.12.21) – Salim Bou Jan 27 '16 at 6:12
• I've just tested, that works like a charm ! Thank you so much! – Zozor Jan 27 '16 at 6:44

You can use newunicodechar:

\documentclass{article}
\usepackage{xeCJK}
\usepackage{newunicodechar}
\usepackage{xcolor}

\newunicodechar{の}{\textcolor{red}{の}}

\begin{document}

\end{document}


Text taken from the page about Tokyo on Japanese Wikipedia. I have no idea of what the text means, nor why the replacement character pops in.

• @egreg This solution could be used to re-assign a key to a completely different character, and works with LuaLaTeX and friends? – A Feldman Jan 27 '16 at 16:20
• @AFeldman Yes, but it refuses to redefine a character in the ASCII range (32-127). or problems would arise with control sequences. – egreg Jan 27 '16 at 16:23

Just have your macro take an argument and then feed the characters to the macro. If they are to be consistently formatted, you want one macro which you use to format the 200 characters. You don't need - or want - 200 macros.

For example,

\documentclass{article}
\usepackage{xcolor}
\newcommand*\learnme[1]{\textcolor{red}{#1}}
\begin{document}
ABC\learnme{W}XY\learnme{U}VZ.
\end{document}


• Thanks for your answer ! I know, but this solution would imply to replace every W by \learnme{W}, every U by \learnme{U}` and so on. When you’ve got only U, W and 1 sentence, that’s quite a small problem. But with a set of hundreds and hundreds of characters, constantly changing from text to text, and long texts, that becomes more tricky. – Zozor Jan 27 '16 at 6:19