14

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.

1
  • 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
    Commented Jan 27, 2016 at 3:21

4 Answers 4

10

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.

1
  • Thank you! You trick allows me not to use LuaLaTeX, which I’m not very keen on
    – user96829
    Commented Jan 27, 2016 at 11:46
13

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

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

2
  • @egreg This solution could be used to re-assign a key to a completely different character, and works with LuaLaTeX and friends?
    – A Feldman
    Commented Jan 27, 2016 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
    Commented Jan 27, 2016 at 16:23
0

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?

1
  • 2
    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.
    – user96829
    Commented Jan 27, 2016 at 6:19

You must log in to answer this question.