是否可以使用循环或包为一段文本中的字母 a、e、i、o、u、ya 赋予不同的背景颜色?例如,为 a 赋予红色背景,为 e 赋予蓝色背景等。而不更改文本中其他字母的背景。
答案1
适应这个答案,这里有一种方法可以使用相同的 XeTeX 扩展来放入背景颜色。我只做了“a”和“e”,但显然可以扩展。
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\XeTeXinterchartokenstate = 1
\newXeTeXintercharclass \tta \XeTeXcharclass `\a \tta
\newXeTeXintercharclass \tte \XeTeXcharclass `\e \tte
%
\setbox2=\hbox{a}
\XeTeXinterchartoks 0 \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tta \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tte \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks 255 \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\setbox2=\hbox{e}
\XeTeXinterchartoks 0 \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tta \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tte \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks 255 \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
The awful weary green fox jumped over the lazy dog.
\end{document}
阅读 XeTeX 参考来了解详细信息\XeTeXinterchartoks
。
答案2
结合两个问题的答案可以得出一个简单的解决方案:
Leo Liu 的回答:-soul:使用 \selectcolormodel 时 xcolor 突出显示出现问题
egreg 的回答是:-在 LaTeX 文档中为所有元音添加不同的颜色
\documentclass{article}
\usepackage{xparse,xcolor}
\usepackage[normalem]{ulem} % use normalem to protect \emph
\newcommand\hl[1]{\bgroup\markoverwith
{\textcolor{#1}{\rule[-.5ex]{2pt}{2.5ex}}}\ULon}
\ExplSyntaxOn
\NewDocumentCommand{\colorize}{mm}
{
\cs_set:cpn { maryjane_color_#1: } { \hl{#2}{#1} }
}
\tl_new:N \l_maryjane_text_tl
\NewDocumentCommand{\changecolors}{ O{aeiou} m }
{
\tl_set:Nn \l_maryjane_text_tl { #2 }
\tl_map_inline:nn { #1 }
{
\tl_replace_all:Nnn \l_maryjane_text_tl { ##1 } { \use:c { maryjane_color_##1: } }
}
\l_maryjane_text_tl
}
\ExplSyntaxOff
\colorize{a}{red}
\colorize{e}{blue}
\colorize{i}{green}
\colorize{o}{yellow}
\colorize{u}{brown!30}
\begin{document}
\changecolors{The quick brown fox jumped over the lazy dog}
\changecolors[a]{The quick brown fox jumped over the lazy dog}
\end{document}