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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
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 at 3:21
up vote 11 down vote accepted

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.

share|improve this answer
    
Thank you! You trick allows me not to use LuaLaTeX, which I’m not very keen on – Zarthur Jan 27 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.

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}
share|improve this answer
    
I have error ! Missing number, treated as zero. <to be read again> \relax l.140 \newcatcodetable\catcodetable@initex – salim bou Jan 27 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 at 5:47
    
@salimbou - Do you by any chance execute \ColorMeOn before \begin{document}? That would definitely cause problems... – Mico Jan 27 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 at 6:12
    
I've just tested, that works like a charm ! Thank you so much! – Zarthur Jan 27 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.

enter image description here

share|improve this answer
    
You guys are all fabulous ! thanks – Zarthur Jan 27 at 11:45
    
@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 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 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}

Red Letter Days?

share|improve this answer
1  
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. – Zarthur Jan 27 at 6:19

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.