尝试为某些特定字符的每个出现着色时出现错误(XeLaTeX)

尝试为某些特定字符的每个出现着色时出现错误(XeLaTeX)

我想用红色为文档中的特定字符(或单词)着色,我知道可以使用,XeTeX来实现\newXeTeXintercharclass\XeTeXinterchartoks

我找到了这个答案为某些特定字符的所有出现位置着色这看起来像我需要的,但编译代码时出现错误

!太多 }。 \egroup l.20 我一直在想如何做一件绝对正确的事情……您关闭的群组比打开的群组多。这样的错误通常无害,所以请继续。

不起作用的示例

\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

xetex 的最新版本具有更多字符类。现在您需要使用 4095 而不是 255 作为边界:

\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}

在此处输入图片描述

相关内容