2

I want to color specific character (or word) in my document with red, I know that XeTeX can achieve this using \newXeTeXintercharclass, \XeTeXinterchartoks.

I find this answer Coloring all occurrences of some specific characters which look like what I need, but errors arise when compiling the code

! Too many }'s. \egroup l.20 I have been wondering for ages how to do something that is absolutely c... You've closed more groups than you opened. Such booboos are generally harmless, so keep going.

Not working example

\documentclass{article}
\usepackage{xcolor}

\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}
1
  • 1
    The most recent version of XeTeX has raised the number of classes, so 255 should become 4095
    – egreg
    Commented Feb 26, 2017 at 13:57

1 Answer 1

5

The newest versions of xetex has more charclasses. You now need to use 4095 instead of 255 for the boundary:

\documentclass{article}
\usepackage{xcolor}

\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 4095 \mycharclassRed = {\bgroup\color{red}}

\XeTeXinterchartoks \mycharclassRed 0   = {\egroup}
\XeTeXinterchartoks \mycharclassRed 4095 = {\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}

enter image description here

2
  • Thanks Ulrike Ficher, what characters represented by 0 and 4095?
    – Salim Bou
    Commented Feb 26, 2017 at 15:01
  • 0 is where everything is by default until you change it. 4095 is the "word boundary". Commented Feb 26, 2017 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .