Coloring all occurrences of some specific characters

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.

enter image description here

% !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}

\newcommand\ColorMeOn{\directlua{luatexbase.add_to_callback(
    "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}

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.

enter image description here


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:

enter image description here

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

Tags:

Color

Xetex